1

As the title describes it, I cannot get the value from my JQuery variable into a PHP one.

My JQuery code is like this:

$(document).ready(function() {
  var mbscr = 0;

  function checkSize() {
    if ($("#orientation").css("width") == "200px") {
      var mbscr = 1
    } else { 
      var mbscr = 2 
    }

    $.ajax({
      type:"POST",
      url: './a/cmn.arr.php',
      data: { mbscr : mbscr },
      cache: false,
      success: function(data) {
        console.log(data);
      }
    });
    return false;
  };

  checkSize(mbscr);

  $(window).resize(checkSize);
});

The PHP one is:

if(isset($_POST['mbscr']) && !empty($_POST['mbscr'])) {
    $_get_mbscr = $_POST['mbscr'];
}

var_dump($_get_mbscr);

If I use alert() instead of console.log(), the JQuery parts does its job, it alerts me what it should. My problem is I always get a null value for var_dump(), even when I do var_dump($_POST['mbscr']).

Any help will be appreciated, thank you.

12
  • 1
    Mb problem in than line ' url: './a/cmn.arr.php',' check withoud dot. Commented Mar 27, 2017 at 11:54
  • what happens when you put this url in your browser? ./a/cmn.arr.php Commented Mar 27, 2017 at 11:54
  • The path is not the problem, I've tried doing it in index.php, and I got the same thing. Commented Mar 27, 2017 at 11:57
  • What will return var_dump($_POST) ? Commented Mar 27, 2017 at 12:03
  • 1
    What will happen if you send data manually? data: { mbscr: 2}, Commented Mar 27, 2017 at 12:17

1 Answer 1

1

You code should work. But if I understood right - you trying to see post data just while entering page? If you do so - you'll see array(0) because when you open page - request is GET. You can see real post result via network debugger (dev tools etc.)

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

18 Comments

No. that is a legal syntax for defining object properties. it might be identifier, number , or string , see
Absolutely it can be. But, if this variable contains value then value will be used as is
I get the same thing. I noticed a weird thing. In the firebug console, when I check POST response, the var_dump appears ok: array(1) { ["mbscr"]=> string(1) "2" } But what I see on the actual page is array(0) { }. Does this mean it's a server or client error? I'm not really that experienced. Thanks.
@Ciprian C Excuse me :) If I understood right - you trying to see post data just while entering page? If you do so - you'll see array(0) because when you open page - request is GET. You can see real post result via network debugger (dev tools etc.)
@CiprianC Can you please explain that line "the var_dump appears ok: array(1) { ["mbscr"]=> string(1) "2" } But what I see on the actual page is array(0) { }. " var_dump should outputs to the actual page
|

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.