Reputation: 4893
My script does not seem to be working. Every time i try to use the form i've get the error with readystate=0, and status=0. Any help will be appreciated. I've got the following form:
<form action="" method="post" >
<input type="text" name="name" id="tekst"/><br/>
<input type="text" name="name" id="autor"/><br/>
<input type="submit" name="<?php echo $id; ?>" value="Send" id="submit"/>
</form>
<div class="response"></div>
the values from input fields are then processed by the following code:
$("input#submit").click(function(){
var id = $("input#submit").attr("name");
var autor = $('input#autor').val();
var tresc = $('input#tekst').val();
$.ajax({
type: "POST",
url: "add_comment.php",
data:
{id: id,
autor: autor,
tresc: tresc},
success: function(data){
$(".response").text(data);
},
error:function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
}
});
});
Here is my add_comment.php:
<?php
$id = $_POST['id'];
$autor= $_POST['autor'];
$tresc = $_POST['tresc'];
if(isset($id) && isset($autor) && isset($tresc)){
include("db/connection.php");
$zapytanie= "INSERT INTO komentarze(id_ref, autor, tresc) values ('$id', '$autor', '$tresc')";
$wynik = $db->query($zapytanie);
echo "Added";
}else{
echo "Problem";
}
?>
EDIT: id_ref is not auto_increment field. The script is running on the localhost
Upvotes: 0
Views: 204
Reputation: 19882
<form action="add_comment.php" method="post" >
<input type="text" name="tekst" id="tekst"/><br/>
<input type="text" name="autor" id="autor"/><br/>
<input type="submit" name="button" value="Send" id="submit"/>
</form>
And in php it should be like this
$id = $_POST['button'];
$autor= $_POST['autor'];
$tresc = $_POST['tekst'];
It should work properly now. Previously you were making a mistake that in the submit button field you were passing a php variable which could contain anything and you dont know if it is dynamic. So you weren't be able to get it by using $_REQUEST['id'] because it could have been some thing like this
$id = 1; or $id = 'dynamic string' ;
Upvotes: 0
Reputation: 6003
First of all, your form isn't correct...name="name", class=".response"??
<form action="" method="post" >
<input type="text" name="tekst" id="tekst"/><br/>
<input type="text" name="autor" id="autor"/><br/>
<input type="submit" name="id" value="Send" id="submit"/>
</form>
<div class="response"></div>
Your PHP File should be"
<?php
$id = $_POST['id'];
$autor= $_POST['autor'];
$tresc = $_POST['tekst'];
if(isset($id) && isset($autor) && isset($tresc)){
include("db/connection.php");
$zapytanie= "INSERT INTO komentarze(id_ref, autor, tresc) values ('$id', '$autor', '$tresc')";
$wynik = $db->query($zapytanie);
echo "Added";
}else{
echo "Problem";
}
?>
Upvotes: 1