I have an ajax call to a php file that inserts records to the database. However, I dont see the values of the parameters that are being passed in the php file where I have the code for inserting the values into database. This is my ajax call
dataString = 'firstname=Jim&lastname=Smith';
$.ajax(
{
type: "POST",
url: "submit.php",
data: dataString,
});
This is how I read the value in my submit.php file
$first_name = mysql_escape_string($_POST('firstname'));
$last_name = mysql_escape_string($_POST('lastname'));
$query = "INSERT into USERS (first_name, last_name) VALUES ('$first_name','$last_name');
But I dont see any values in $first_name or $last_name and hence no record is getting inserted. If I statically defined $first_name = ‘Jim’ and $last_name = ‘Smith’ in the submit.php file, then I see the record being inserted in the database. Any idea what I am doing wrong?
$_POSTis an array, and thus you should access its content using$_POST['var'], not$_POST('var'). I suggest you enable error reporting, this will help you debug a lot better. Your code, for instance, would have caused a fatal error.