I'd like run INSERT
query by ajax. That query need value from php session but js request doesn't see that value. Whay it's happend ? What should i do ?
Some code below:
JS:
$('div#content').on( "submit" , "form" , function ( event ){
event.preventDefault();
var href = "ajaxRequest.php" + $(this).attr("action");
var method = $(this).attr("method");
var values = $(this).serializeArray();
$.ajax({
url : href,
data : values,
type : method,
dataType : "html",
cache : false,
success : function ( content ){
alert( content );
}
});
});
PHP:
public function addBoard( $params = null )
{
$ID_user = $_SESSION[ 'user_id' ];
$board_model = $params[ 'board_model' ];
$query = "INSERT INTO `".prefix."boards` (
`ID_user` ,
`board_model`
)
VALUES (
'$ID_user', '$board_model');";
if( $this->SQL->dbquery( $query ) )
echo 'Added';
else
echo 'Failed';
}
write session_start();
at top of your PHP
page.
For the server, an ajax request is not different from a regular page request. Session behaves in the same way as it behaves in page request.
All you need to do is start the session in your php file which responds to the ajax request.
you may need to start session on top of page. It seems here that you defined that function in ajax file but not called. So have you called that function in ajax file