我正在使用 jQuery 验证图像提交表单。我需要它来检查数据库中是否存在给定提交网址的实例,以减少转发。我使用了以下代码:
$("#submit_form").validate({
rules: {
right_form_title: {
required: true,
maxlength: 105
},
right_form_url: {
required: true,
image: true,
gif: true,
remote: "/~lyons/imagecheck.php"
}
},
messages: {
right_form_url: {
remote: "That image has already been submitted under that URL."
}
}
});
});
图像检查.php:
<?php
//Database Information
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$title = mysql_real_escape_string($right_form_url);
$query = "SELECT filename FROM images WHERE filename='$title';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0) {
return false;
} else {
return true;
}
echo json_encode($output);
?>
问题是,现在,无论如何,所有内容都会像之前提交的那样返回。这有什么问题吗?寻找了一段时间后我几乎没有任何线索。
最佳答案
当你确实返回 true 和返回 false 时,这些值仅在 php 环境(服务器端)中。因此,您在 jquery 中获得的值是
echo json_encode($output)
这将始终评估为 false。
关于Jquery Validate - 使用远程查看 MySQL 数据库中是否存在某个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439775/