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

191
Visualizações
Why ajax doesn't send my JS variable to PHP?

I'm trying to send JS variables to a PHP script (which is included in the page I try to make ajax work)

index.php

include 'PHP/display.php';
<button id="click" onclick="show();">Click !</button>

<script>
  function show(){
    var str = "Yes";
    $.ajax({
      method : 'post',
      url : 'PHP/display.php',
      data: {
        str : str
      },
      success: function(data) {
        console.log(data);
      }
    });
  }
</script>

display.php

<?php
  echo isset($_POST['str']) ? $_POST['str'] : "No";
?>

This keeps displaying "No", but the console shows me the "Yes" I want

This code is my first try with ajax, that's why I try to keep it simple, but it doesn't work.

I've been looking for hours on StackOverflow and not one solution works for me.

Maybe i'm missing something, please help me :(

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can see the url property of the ajax call like the php include, but the difference is that it will be executed until you click on the button. PHP will be able to receive the content of data

You can use jQuery's text method to assign the response to the call to the paragraph as text.

function show() {
  var str = "Yes";
  $.ajax({
    method: 'post',
    url: 'display.php',
    data: {
      str: str
    },
    success: function(data) {
      $('#response').text(data);
    }
  });
}
<p id="response"></p>

display.php

<?php
echo isset($_POST['str']) ? $_POST['str'] : "No";
about 4 years ago · Juan Pablo Isaza 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