I have a form that I want to communicate with the server throw an AJAX post. The query that I did says that was an error on query but I don't see the problem.I tried different AJAX code but seems that the post works because it passes the if($_POST).
我有一个想要与服务器通信的表单,抛出一个AJAX帖子。我所做的查询在查询中是一个错误,但我没有看到问题。我尝试了不同的AJAX代码,但似乎post可以工作,因为它通过if($_POST)。
$('#submit').click(function(e) {
var url = "handler/dForm.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
contentType: 'application/json; charset=utf-8',
data: $("#idForm").serialize(), // serializes the form's elements.
success: function(data)
{
console.log(data); // show response from the php script.
},
error: function(xhr,textStatus,err)
{
console.log("readyState: " + xhr.readyState);
console.log("responseText: "+ xhr.responseText);
console.log("status: " + xhr.status);
console.log("text status: " + textStatus);
console.log("error: " + err);
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
The server part
服务器的一部分
require_once 'connect.php';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$subject = isset($_POST['Subject']) ? $_POST['Subject'] : '';
$message = isset($_POST['Message']) ? $_POST['Message'] : '';
$email = isset($_POST['Email']) ? $_POST['Email'] : '';
if (isset($_POST)) {
$email = $DBcon->real_escape_string($email);
$message = $DBcon->real_escape_string($message);
$subject = $DBcon->real_escape_string($subject);
$name = $DBcon->real_escape_string($name);
$query = "INSERT INTO customer(name,messaje,subject,email) VALUES('$name','$message','$subject','$email')";
if ($DBcon->query($query)) {
$msg = "<div class='alert alert-success'>
<span class='glyphicon glyphicon-info-sign'></span> successfully registered !
</div>";
}else {
$msg = "<div class='alert alert-danger'>
<span class='glyphicon glyphicon-info-sign'></span> error while registering !
</div>";
}
echo $msg;
$DBcon->close();
}else{
echo "err";
}
The HTML Form :
HTML表单:
<form id="idForm">
<input type="text" class="col-md-6 col-xs-12 name" name='name' placeholder='Nume *'/>
<input type="text" class="col-md-6 col-xs-12 Email" name='Email' placeholder='Email *'/>
<input type="text" class="col-md-12 col-xs-12 Subject" name='Subject' placeholder='Subiect'/>
<textarea type="text" class="col-md-12 col-xs-12 Message" name='Message' placeholder='Mesaj *'></textarea>
<div class="cBtn col-xs-12">
<ul>
<li class="clear"><a href="javascript:void(0)"><i class="fa fa-times"></i>Sterge campurile</a></li>
<li class="send"><a href="javascript:void(0)" id="submit"><i class="fa fa-share"></i>Trimite</a>
</li>
</ul>
</div>
</form>
1 个解决方案
#1
2
First problem that I see is if (isset($_POST))
It is always TRUE
. You have got to check if specific field is set ie. if (isset($_POST['name']))
.
我看到的第一个问题是if (isset($_POST)),它总是正确的。您必须检查特定字段是否设置为ie。如果收取($ _POST['名字']))。
Second is that row: $query = "INSERT INTO customer(name,messaje,subject,email) VALUES('$name','$message','$subject','$email')";
第二行:$query =“插入客户(姓名、messaje、subject、email)值(‘$name’、‘$message’、‘$subject’、‘$email’)”;
I think You misstyped message column. You have messaje.
我认为你写错了消息栏。你有messaje。
And as a tip for debugging use echo $DBcon->error;
if query()
returned FALSE.
作为调试使用的提示,使用echo $DBcon->错误;如果查询()返回FALSE。
#1
2
First problem that I see is if (isset($_POST))
It is always TRUE
. You have got to check if specific field is set ie. if (isset($_POST['name']))
.
我看到的第一个问题是if (isset($_POST)),它总是正确的。您必须检查特定字段是否设置为ie。如果收取($ _POST['名字']))。
Second is that row: $query = "INSERT INTO customer(name,messaje,subject,email) VALUES('$name','$message','$subject','$email')";
第二行:$query =“插入客户(姓名、messaje、subject、email)值(‘$name’、‘$message’、‘$subject’、‘$email’)”;
I think You misstyped message column. You have messaje.
我认为你写错了消息栏。你有messaje。
And as a tip for debugging use echo $DBcon->error;
if query()
returned FALSE.
作为调试使用的提示,使用echo $DBcon->错误;如果查询()返回FALSE。