0

Cannot get a inside msg.php

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

msg.php

var_dump($_POST['a']); // NULL, but i need `14`
5
  • How do you know it is NULL? Commented Jan 2, 2014 at 13:24
  • try to print $_REQUEST Commented Jan 2, 2014 at 13:25
  • @putvande, using var_dump - NULL is written on display Commented Jan 2, 2014 at 13:25
  • it surely sending 14 from ajax Commented Jan 2, 2014 at 13:25
  • try using echo or print instead of var_dump Commented Jan 2, 2014 at 13:26

4 Answers 4

4

You are getting null because after the AJAX call is done you send the user to msg.php where $_POST is empty again.

When you do :

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

You will see it works and you get 14.

I see no good reason to first use AJAX to POST something and then on success send the user to that same page.

Sign up to request clarification or add additional context in comments.

Comments

1

You have a bad concet of ajax request.

$('#click01').click(function(){
  var a = 14;
  $.ajax({
    url : 'msg.php',
    type : 'post',
    data : {a:a}
    }).done(function(data) {
      // data= var_dump($_POST['a'])
      alert(data)
    })
});

In var data you have the result of ajax request, but if you redirect to msg.php no have the variable

Comments

1

Are you sure that you are checking the ajax return, and not the return of the window.location='msg.php'; ?

Try this:

$('#click01').click(function(){
  var a = 14;
  $.ajax({
    url : 'msg.php',
    type : 'POST',
    data : {'a':a},
    success: function(r) { alert(r); }
    });
});

Comments

0

// Instead of var_dump($_POST['a']) at msg.php use:- // extract($_POST); echo $a;

$('document').ready(function(){ var a = 14;

    $.ajax({
      url  : "msg.php",
      type : 'post',
      data : {a:a}
      }).done(function(data) {
          alert(data);

      });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.