0

What is wrong:

$('#click01').click(function(){
    $a = 14;
    $.ajax({
        url  : 'msg.php',
        type : 'post',
        data : {a:$a}
    });
    window.location = 'msg.php';
});

msg.php contains:

var_dump($_POST['a']); // NULL, but expected: 14
George
  • 36,413
  • 9
  • 66
  • 103
qadenza
  • 9,025
  • 18
  • 73
  • 126
  • 1
    Why that `window.location`? That would reload the page _before_ the ajax call completes. – moonwave99 Jan 02 '14 at 11:01
  • 1
    Changing the window.location will cause the page to reload but not post the value in 'a'. Instead you want to add a 'success' function to your ajax call to get the response and then put that into your page. – Rob Baillie Jan 02 '14 at 11:01
  • Variables in Javascript need not start with `$`. – Rahul Desai Jan 02 '14 at 11:02
  • Comment out the `window.location` line. If you wish to redirect to the page then do a form submit. Read up on making AJAX calls using [jQuery.ajax](api.jquery.com/jQuery.ajax/). – Vikram Deshmukh Jan 02 '14 at 11:02
  • @srvikram13, Five answers but I still cannot get value of `a` insisde `msg.php`. `var_dump($_POST['a']);` is still `NULL`. – qadenza Jan 02 '14 at 13:05
  • 5 Answers and no thanks! Still, if you've not got your answer then I can *almost* understand that. If you have tried a 'success' or 'done' style version, can you update your question to include *exactly* what you have done? – Rob Baillie Jan 02 '14 at 16:31

5 Answers5

0

Try this

$('#click01').click(function(){
    var a = 14;
    $.ajax({
       url  : 'msg.php',
       type : 'POST',
       data : {a:a},
       complete: function (data) {
           console.log( data ); // this will tell you the output of var_dump.
           window.location.href = 'msg.php';
       }
    });
    // window.location = 'msg.php';
});

also this line window.location.href = 'msg.php'; in function complete will actually redirect you to the msg.php, so if you dont want any redirects remove those lines..

zzlalani
  • 22,960
  • 16
  • 44
  • 73
0
$('#click01').click(function(){
        var  a = 14;//not $a since this is javascript
        $.ajax({
         url  : 'msg.php',
         type : 'post',
         data : {'a':a}
    });
    window.location = 'msg.php';
    });
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0
$('#click01').click(function(){
    var a = 14
    $.ajax({
           type: "POST",
           url: "msg.php",
           data: {a:a},
           beforeSend: function () {
           // do action
           },
           success: function(html){
                window.location = 'msg.php';        
           }
    });
});
sas
  • 2,563
  • 1
  • 20
  • 28
0

Rather than set the window.location you want to set a 'success' function on the ajax call so that when it finishes it takes the response and puts it into your page.

E.g. something like:

 $('#click01').click(function(){
        $a = 14;
        $.ajax({
         url  : 'msg.php',
         type : 'post',
         data : {a:$a},
         success: function(data, textStatus, jqXHR) {
           $('#placeToPutTheResponse').append( data );
         }
    });
  });

The above assumes that you have added an HTML node with the id="placeToPutTheResponse"

It is worth reading this other post on SO for a decent overview: jQuery Ajax POST example with PHP

It uses done rather than success, and slightly different syntax, but it's a great overview.

Community
  • 1
  • 1
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
0
$(document).on('click','#click01',function () {
    var a = 14;
    $.ajax({
        url: 'msg.php',
        type: 'post',
        data: {a: a},
        success: function(e){
            console.log(e);
        },
        error: function(e) {
            console.log(e);
        }
    });
});
box.around
  • 69
  • 1
  • 8