I have problem with ajax and php.I have next code
$("form").submit(function() {
var poruka = $("#poruka").val();
$.ajax({
type: "GET",
url: "/ajax/gather/send/"+ poruka + "/'.$id.'",
success: function(){
loaduj('.$id.');
alert("Your message is "+poruka);
},
error: function(){
alert("fail");
}
});
});
“poruka” is the message and it’s taken from form
<form action='javascript:;'>
<input type='text' name='poruka' id='poruka' placeholder='Poruka..' style='width:100%;'>
<input type='button' name='button' hidden>
</form>
And when i try to send to php it won’t insert that into database :
Php code :
$text = $_POST['text'];
$id = $_POST['id'];
if($text != ""){
mysql_query("INSERT INTO messages (text,id2) VALUES ('$text','$id')") or die(mysql_error());
}
And it can’t work..How can i fix it ? Thanks
You use POST instead of GET. In your AJAX form you specify that you use GET, but in PHP you use
$_POSTsuperglobal.Change
$_POSTinto$_GETin your PHP code and it should work 🙂Also if you do not specify method in your tag, the default is GET.
From experience, in case if the data from the form is not written/updated in/to the database use
after the MySQL query and also open the developer tools on your browser and inspect in the network panel. Developer tools can be opened using F12 and with CTRL+SHIFT+I if your F12 is not working due to the coffee spilled on it. Network panel shows the data being sent, the method used and every other detail of a network call. Also there you can see the response of your server side script which in this case would return a mysql error message because you echo’ed it out.