Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda