Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!