0

I want to display the output from the user input.

Hi Guys, I need to display the output from the user input but it seem the output give me an error.

 <label>First Name: </label>
    <input id="fname"></input>
    <br>
    <button id="testing_lang" value="1" onclick="return chk()">
        Test Output
    </button>
    <br>
    <p id="query_result">Result will display here</p>


   <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   <script>
      function chk()
      {
        var firstName = document.getElementById('fname').value;
        $.ajax({
          type: 'post',
          url: 'test.php',
          data: firstName,
          cache:false,
          success: function(html)
                {
                  $('#query_result').html(html);

                }
         });
        return false;
      }
    </script>

This is the code from my test.php I created

$firstName=$_POST['firstName'];
echo "Response: " .$firstName;

This is the error of the output

Notice: Undefined index: firstName in C:\xampp\htdocs\cops\pages\test.php on line 2 Response:

Jack_N_Gel
  • 5
  • 1
  • 4

1 Answers1

1

change

data: firstName,

to

data: {firstName : firstName}, 

you need to set index along with the value

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
guradio
  • 15,524
  • 4
  • 36
  • 57