-1

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:

  1. (document).ready

  2. Take new list name from input

  3. ListA1

  4. passName: ListA1

  5. :

Object {listNamePOST: "ListA1"}

  1. passName function end

  2. returned from passName()

  3. about to echo $_SESSION['listName'] :

  4. 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.

Community
  • 1
  • 1
Roy Grubb
  • 93
  • 2
  • 11
  • 1
    Have you tried `success:function() {console.log("6. success");},` ? (instead of done?) – FrancescoMM Sep 30 '14 at 15:10
  • @FrancescoMM - Thanks. Yes I started with success: error: then found that didn't work (and will be deprecated) so switched. I just retried success and confirmed the result is the same. – Roy Grubb Oct 01 '14 at 03:16
  • 1
    `success: fn` and `error: fn` are not depreciated. `.success(fn)` and `.error(fn)` are. big difference. `done: fn` and `fail: fn` doesn't do anything. – Kevin B Oct 01 '14 at 04:41
  • @Kevin, thanks for setting me right on that. Maybe I'm misinterpreting this: http://api.jquery.com/jquery.ajax/ but it says ''Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.'' Anyway, as I said, I started by using success: and error: and it didn't work, and retried and it still didn't work. My problem must be somewhere else. – Roy Grubb Oct 01 '14 at 07:57
  • @FrancescoMM, Kevin, please see my 3rd edit. – Roy Grubb Oct 01 '14 at 08:19

2 Answers2

0

If it's really your complete storesession.php, you are missing session_start() in it.

Marek
  • 7,337
  • 1
  • 22
  • 33
  • Thanks @Marek. The first line in test.php is < ?php session_start(); ?> (here I've added a space after the < or it won't show). I just tried putting it in storesession.php as well, but no difference. Anyway, that shouldn't stop the ajax working and showing something in the console log. – Roy Grubb Oct 01 '14 at 03:24
0

Are you returning data in json format from the AJAX call? I had a similar problem and it was because of the data type, lose the dataType : "json" unless you are actually returning json, and see if that helps.

MattRogowski
  • 726
  • 2
  • 7
  • 22
  • Thanks @Matt - I had started with dataType: "text" and that didn't work, so I switched to json. At your suggestion, I just dropped the datatype line, but the result is the same. – Roy Grubb Oct 01 '14 at 03:21
  • Okay; open up the developer console and view the XHR request being made, see what headers get sent back, and if there is a response. – MattRogowski Oct 01 '14 at 08:46