How can I fix this undefined index error? Jquery Ajax to PHP

耗尽温柔 提交于 2019-12-09 19:00:25

问题


I'm using Jquery, Ajax and PHP to try and send a variable to be written in a mysql database. The Ajax request is being made but the variable is not being picked up by the php. I don't know why this is the case.

Using firebug and console.log() I can see that a POST has been made to write_results.php

If I check the Response it says

Notice: Undefined index: testscore in E:\write_results.php on line 2

Here is my PHP

<?php 
  $testscore=$_POST['testscore'];  //get testscore from Ajax 
  include 'DB.php';
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con); 
     if (isset($_POST['testscore'])) {  
       $addClient  = "INSERT INTO variables (`id` ,`name`) VALUES (NULL,'$testscore')";  
       mysql_query($addClient) or die(mysql_error());  
       }

?>  

Here is my ajax script

<script type="text/javascript">
$(document).ready(function() {  
testscore ="tryagain"; //testvalue to enter into the mysql database
  $.ajax({  
    type: "POST",  
    url: "write_results.php",  
    data: testscore,      
    success: function(){  
      $('#box2').html("success");
    } 
  })
}); 
</script>

My questions

  1. Why isn't $testscore receiving a value from the ajax script?
  2. How can I fix this?

回答1:


You're not telling the JS how to send your POST parameters. Change your JS to:

data: { 'testscore':testscore },

This is the equivalent of "testscore=" + testcore in key=value form. It tells JS and PHP that you want the variable testscore to be mapped to the key "testscore", which you'll then pick up with $_POST['testscore']

Edit: See http://api.jquery.com/jQuery.ajax/, "Sending Data to the Server"




回答2:


In your php code you get the $testscore value form $_POST['testscore']. $_POST is a super global array and testscore is the index here. This $_POST array's indexes come from the field names of a form you post. In your case you are using ajax to pass data to the php page. You can pass by either GET method or POST method. Since you are passing by POST as you have specified type:POST in your ajax code, you will be able to use $_POST varibale in your php file. But you are not specifying the array indexes in your ajax code which will hold the values.

testscore ="tryagain"; //It will only assign the value to the javascript variable

You need to provide the key value pair. You can do it in wither way:

testscore="testscore=tryagain"; //In your php code, the testscore will be the array index and tryagain will be its value.

You can also send the key value pair to PHP file in JSON format as below:

testscore={'testscore':'tryagain'}; 

If you have multiple values(e.g. two) to send you can do as below:

testscore={'testscore':'tryagain','index2':'value2'}

And in your PHP code you can get this as below, if using post as type in ajax:

$testscore1=$_POST['testscore']; //It will assign tryagain
$testscore2=$_POST['index2'];    //It will assign value 2



回答3:


Try using data: "testscore=" + testscore, The data needs to be formatted as a query string.



来源:https://stackoverflow.com/questions/8046482/how-can-i-fix-this-undefined-index-error-jquery-ajax-to-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!