jquery ajax 结果消息
我对 php 脚本进行了 ajax 调用,当用户提交数据字符串作为条目时,该脚本会使用新记录更新我的 MySQL 数据库。 PHP 的一部分是 if/else,它将检查条目是否重复。我可以阻止重复项更新数据库,但我不知道如何发回一条“错误”消息,提示“该记录已存在”...这是我的 jquery: [脚本] $(function() {
$(".entry_button").click(function()
{
var element = $(this);
var boxval = $("#entry").val();
var dataString = 'entry='+ boxval;
if(boxval=='') {
alert("Please Enter Some Text");
} else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle"> <span class="loading">Broifying It...</span>');
$.ajax({
type: "POST",
url: "update_data.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").prepend(html);
$("ol#update li").slideDown("slow");
document.getElementById('entry').value='';
$("#flash").hide();
}
});
}
return false;
});
});
[/script]
这是我的更新数据库的 php 脚本 (update_data.php):
[php]
include('db.php');
if(isset($_POST['entry']))
{
$date_created = date('Y-m-d H:i:s');
$entry = $_POST['entry'];
$query = "SELECT COUNT(*) AS count FROM table WHERE entry = '$entry'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row['count'] == 0) {
mysql_query("insert into table (entry,date_created) values ('$entry','$date_created')");
$sql_in= mysql_query("SELECT entry,id FROM table order by id desc");
$r=mysql_fetch_array($sql_in);
$newentry=$r['newentry'];
$id=$r['id'];
?>
<li class="entry bar<?php echo $id; ?>">
<div class="thebro"><span><?php echo $newentry; ?></span></div>
<div class="hr"></div>
<div class="utility-left"><span class="share"><a href="#" title="Share">share</a></span> | <span class="time">days ago</span></div>
<div class="utility-right"><span class="comments">COMMENTS</span> | <span class="like">LIKE</span></div>
</li>
<?php
} else {
$error = "Sorry, that record already exists";
}
}
[/php]
我希望能够从 php 脚本中吐出 $error
变量,并将其发送回ajax 调用并显示在我的 HTML 中的元素中,我已经在 google 中搜索了 ajax 错误消息,但它们都与标头错误有关,而不是验证错误消息,除非有人想重做。上面的 jquery 块,以便将其作为 json 数据发送。
任何想法都很棒!
I have a ajax call to a php script that updates my MySQL DB with a new record when the user submits a data string as an entry. Part of the PHP is an if/else in place that will check to see if the entry is a duplicate. I can stop the duplicate from updating the database but I don't know how to send back an "error" message that says "That record already exists"...here is my jquery:
[script]
$(function() {
$(".entry_button").click(function()
{
var element = $(this);
var boxval = $("#entry").val();
var dataString = 'entry='+ boxval;
if(boxval=='') {
alert("Please Enter Some Text");
} else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="images/loading.gif" align="absmiddle"> <span class="loading">Broifying It...</span>');
$.ajax({
type: "POST",
url: "update_data.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").prepend(html);
$("ol#update li").slideDown("slow");
document.getElementById('entry').value='';
$("#flash").hide();
}
});
}
return false;
});
});
[/script]
and here is my php script that updates the DB (update_data.php):
[php]
include('db.php');
if(isset($_POST['entry']))
{
$date_created = date('Y-m-d H:i:s');
$entry = $_POST['entry'];
$query = "SELECT COUNT(*) AS count FROM table WHERE entry = '$entry'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row['count'] == 0) {
mysql_query("insert into table (entry,date_created) values ('$entry','$date_created')");
$sql_in= mysql_query("SELECT entry,id FROM table order by id desc");
$r=mysql_fetch_array($sql_in);
$newentry=$r['newentry'];
$id=$r['id'];
?>
<li class="entry bar<?php echo $id; ?>">
<div class="thebro"><span><?php echo $newentry; ?></span></div>
<div class="hr"></div>
<div class="utility-left"><span class="share"><a href="#" title="Share">share</a></span> | <span class="time">days ago</span></div>
<div class="utility-right"><span class="comments">COMMENTS</span> | <span class="like">LIKE</span></div>
</li>
<?php
} else {
$error = "Sorry, that record already exists";
}
}
[/php]
I want to be able to spit that $error
variable from the php script to be sent back in the ajax call and be displayed in a element in my HTML. I've googled the crap out of ajax error messages but they all have to do with header errors, not validation error messages. I also am not using json_encode, unless someone wants to redo that jquery chunk above so that it is sent as json data.
Any ideas would be awesome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 PHP 中产生错误,我希望它由 jquery ajax 对象的 error 方法处理,而不是通过发送正常的 HTTP 200 响应并在响应文本上标记一些错误来“伪造”它。
当 PHP 返回 HTTP 500 时,ajax 错误处理程序不会捕获“responseText”。错误处理程序接收 XHR 对象、“错误”状态和“内部服务器错误”错误消息。
我试图操纵错误消息..
但这没有用。
起作用的是:
这将使 PHP 返回 HTTP 500,并在额外的标头中包含您的自定义详细信息。
要从 ajax 错误处理程序访问自定义详细信息:
If an error is produced in PHP, I want it to be handled by the error method of the jquery ajax object, and not 'fake it' by sending a normal HTTP 200 response with some error marking the responseText.
When a HTTP 500 is returned from PHP, no 'responseText' is captured by the ajax error handler. The error handler receives the XHR object, a status of 'error', and an error message of 'Internal Server Error'.
I tried to manipulate the error message..
But that didn't work.
What did work is this:
This will get PHP to return HTTP 500, with your custom details in an extra header.
To access the custom details from the ajax error handler:
只需在您的 phpfile 中回显它并使用:
来自 ajaxrequest 对象的响应文本会拾取回显的所有内容...不知道 jquery 中的等效项
Just echo it in your phpfile and use:
The responsetext from your ajaxrequest object picks up everything thats echoed... Dunno the equivalent in jquery tho
回显 PHP 中的错误并退出。然后,在你的 success: function 开始时,做这样的事情:
我建议使用子字符串方法,因为它允许你创建各种不同的错误消息,只要它们都以“Sorry”开头。对于调试很有用。
Echo the error in PHP and exit. Then, at the beginning of your success: function, do something like this:
I suggest using the substring approach because it allows you to create a variety of different error messages as long as they all begin with "Sorry". Useful for debugging.