I have some jQuery/HTML where I accept a single item of user input (text) and want to pass it to PHP, to be stored for later use in a session variable. I've looked at many pages on this type of action, and found the closest accepted answer to what I think I need is the one here Set Session variable using javascript . The following code is broadly based on that, but it seems that the ajax is doing nothing. I've got console.logs all the way through, and at each possible path through the ajax statement (done, fail and always) but none show up. The console log shows:
(document).ready
Take new list name from input
ListA1
passName: ListA1
:
Object {listNamePOST: "ListA1"}
passName function end
returned from passName()
about to echo $_SESSION['listName'] :
end
Which misses out 6. success or 6. fail and 7. always. And the line console.log(< ?php echo $_SESSION['listName'] ?>); doesn't even show a blank line.
Where am I going wrong, please?
Here is test.php
<?php session_start(); ?>
<form method="post" name="newlistform">
<label for="list_name">Input:</label>
<input style="position: relative; " name="list_name" type="text" required />
<span class="button" id="makeIt">Go</span>
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
/* ƒ: Pass listName out to SESSION variable */
function passName ( listName ) {
console.log('4. passName: ', listName);
var params = {listNamePOST: listName};
console.log("5. :");
console.log(params);
jQuery.ajax({
url: "storesession.php",
type: "POST",
data: params,
dataType : "json",
success: function(data, textStatus, xhr) {
console.log("6. success");
console.log(data);
},
error: function(xhr, textStatus, errorThrown) {
console.log("6. fail");
}
});
console.log('8. passName function end');
}
$(document).ready(function () {
console.log('1. (document).ready');
$( '#makeIt.button' ).click(function () {
console.log('2. Take new list name from input');
var listName = $( 'input[name=list_name]' ).val();
console.log('3.', listName);
passName ( listName );
console.log('9. returned from passName()');
console.log("10. about to echo $_SESSION['listName'] :");
console.log(<?php echo $_SESSION['listName'] ?>);
console.log('12. end');
});
})
</script>
and here is storesession.php
<?php
$_SESSION['listName'] = $_POST['listNamePOST'];
?>
(I'll sanitize the user input later.)
Edit: Just removed 'test.php' at the end of each of the console log lines. I copied them in inadvertently.
2nd edit: For information - This code is an extract of a larger php file which includes another $.ajax call, to get some data from a MySQL db, and that is working.
3rd edit: Changed 'done:' back to 'success:', 'fail:' back to 'error:' and removed 'always:'. This now adds 6. success as the last line, so @FrancescoMM and @Kevin put me right, though I don't understand why it didn't work when I had that originally.
As '6. success' appears at the end of the log, I'm going to try async: false and report back:- OK, that fixed the sequence problem, but the console log statement between log statements 10. and 12. still does not appear. No 'undefined' or even a blank line.
So the problem is now reduced to getting listName into a session variable.
4th edit: Now solved. When I moved the session_start statement from test.php to storesession.php and found it didn't work, I moved it back, but not as the very first statement. I since found http://www.yourhowto.net/how-to-check-and-create-php-session-variables/ so moved the statement to the very beginning, and now the user data is being passed across to the session variable and shows up correctly at the end of the log between 10. and 12.