I'm trying to send data to my local DB server but I keep getting 400 Bad request error when I try to send it.
var studentEmail = "[email protected]";
var dataString = '&questionNumber='+ temp + '&answer='+ value + '&email='+ studentEmail;
$.ajax({
type: "POST",
dataType:'json',
url: "js/dbcon.php",
data: JSON.stringify(dataString),
processData: false,
contentType: "application/json; charset=utf-8"
});
and this is the php file
<?php
$connection = mysql_connect("127.0.0.1", "root", "root"); // Establishing Connection with Server..
$db = mysql_select_db("db", $connection); // Selecting Database
//Fetching Values from URL
$questionNumber=$_POST['questionNumber'];
$answer=$_POST['answer'];
$email=$_POST['email'];
//Insert query
$query = mysql_query("INSERT INTO answers (questionNumber,studentAnswer,studentEmail) VALUES ($questionNumber,$answer,$email)");
echo "succesfully posted";
mysql_close($connection); // Connection Closed
?>
can anyone see what I'm doing wrong?
Thanks in advance!
I'm trying to send data to my local DB server but I keep getting 400 Bad request error when I try to send it.
var studentEmail = "[email protected]";
var dataString = '&questionNumber='+ temp + '&answer='+ value + '&email='+ studentEmail;
$.ajax({
type: "POST",
dataType:'json',
url: "js/dbcon.php",
data: JSON.stringify(dataString),
processData: false,
contentType: "application/json; charset=utf-8"
});
and this is the php file
<?php
$connection = mysql_connect("127.0.0.1", "root", "root"); // Establishing Connection with Server..
$db = mysql_select_db("db", $connection); // Selecting Database
//Fetching Values from URL
$questionNumber=$_POST['questionNumber'];
$answer=$_POST['answer'];
$email=$_POST['email'];
//Insert query
$query = mysql_query("INSERT INTO answers (questionNumber,studentAnswer,studentEmail) VALUES ($questionNumber,$answer,$email)");
echo "succesfully posted";
mysql_close($connection); // Connection Closed
?>
can anyone see what I'm doing wrong?
Thanks in advance!
Share Improve this question edited Feb 23, 2017 at 12:44 faradji asked Feb 23, 2017 at 12:42 faradjifaradji 671 gold badge2 silver badges7 bronze badges 7- ps if I change POST to GET it works but doesnt insert the data to my DB – faradji Commented Feb 23, 2017 at 12:42
- You're sure that dbcon.php is in the js folder? Seems rather strange... – chishiki Commented Feb 23, 2017 at 12:44
-
You must escape the string
@
is not valid inside a URL. – evolutionxbox Commented Feb 23, 2017 at 12:44 -
This is not good: You are not sending key-value pairs, so you will not get any data from
$_POST
on the server, you are not sending back json so your ajax call will never finish successfully, you have an sql injection problem and you use a deprecated mysql api. – jeroen Commented Feb 23, 2017 at 12:46 - I made sure the php file is in the js folder – faradji Commented Feb 23, 2017 at 12:47
4 Answers
Reset to default 2JSON.stringify()
method is used to turn a javascript object
into json string.
So dataString
variable must be a javascript object
:
var data ={questionNumber:temp ,answer: value ,email:studentEmail};
AJAX
$.ajax({
type: "POST",
dataType:'json',
url: "js/dbcon.php",
data: JSON.stringify(data),
processData: false,
contentType: "application/json; charset=utf-8"
});
if you change the post to get you have to replace $_POST with $_GET into your php file.
There is an easier way to pass data that will work correctly for both POST and GET requests
var studentEmail = "[email protected]";
$.ajax({
type: "POST",
dataType:'json',
url: "js/dbcon.php",
data: {questionNumber:temp, answer:value, email: studentEmail},
processData: false,
});
Now jQuery does all the hard work and converts the object full of data to whatever format is required for a POST or GET request
You can send the ajax
request this way:
var studentEmail = "[email protected]";
$.ajax({
type: "POST",
dataType:'json',
url: "js/dbcon.php",
data: ({'questionNumber':temp,'answer':value, 'email':studentEmail }),
processData: false,
contentType: "application/json; charset=utf-8"
});
Also the PHP file needs to return a json
string as well.
echo "succesfully posted";
is no valid json
answer.
Return something like this:
$arr = array('success' => true, 'answer' => "succesfully posted");
echo json_encode($arr);
See also here: http://php/manual/de/function.json-encode.php
You should validate the input data, before inserting into the database.