Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
JQuery variable sent with AJAX to PHP gets 'null' on var_dump();

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.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda