jQueryvalidation – 具有附加数据的远程function

我在validation某些数据时遇到问题。

我想通过检查我的评论表中的company_id和登录用户account_number来检查是否有人已经审查过公司。

我目前拥有的代码似乎从未在评论表中找到任何内容,因此不会警告人们他们无法提交其他评论。

非常感谢您帮助实现这一目标。

这是我到目前为止的代码:

形成

<input type="hidden" value="" name="company_id" /> <input type="hidden" value="" name="account_number" />

Comments:

Rating:

jQueryvalidation脚本

 $(document).ready(function () { $('#review').validate({ errorLabelContainer: "#cs-error-note3", wrapper: "li", ignore: "not:hidden", rules: { comments: { required: true }, account_number: { required: true, remote: { url: "/db_processing/reviews/check-account.php", type: "post", data: { company_id: function() { return $("#company_id").val(); } }, } }, rating: { required: true } }, messages: { comments: { required: "Please enter some comments." }, account_number: { required: "You must be logged in to review.", remote: "You have already reviewed this company." }, rating: { required: "Please select a rating." } }, submitHandler: function(form) { form.submit(); } }); }); 

入住account.php

 prepare("SELECT account_number FROM reviews WHERE account_number =$account_number && company_id =$compid"); $query->execute(); $rows = $query->fetchAll(); $total_rows = count($rows); if( $total_rows > 0 ){ echo 'false'; } else { echo 'true'; } } ?> 

validation码工作正常,没有问题可以预期不必要的逗号, 。 删除它,并非所有浏览器都非常宽容。

 $(document).ready(function () { $('#review').validate({ errorLabelContainer: "#cs-error-note3", wrapper: "li" ignore: "not:hidden", rules: { comments: required: true }, account_number: { required: true, remote: { url: "/db_processing/reviews/check-account.php", type: "post", data: { company_id: function() { return $("#company_id").val(); } }, //<-----Remove this, it's unnecessary } }, rating: { required: true } }, messages: { comments: { required: "Please enter some comments." }, account_number: { required: "You must be logged in to review.", remote: "You have already reviewed this company." }, rating: { required: "Please select a rating." } }, submitHandler: function(form) { form.submit(); } }); }); 

HTML

问题出在这里,因为validation和查询都失败了。

  

为此输入分配id ,因为你在validation脚本中使用id选择器获取它的值return $("#company_id").val(); 所以它会

  

最后用PHP

把变量放在查询中的变量,rest是好的和工作。

 $query = $conn->prepare("SELECT account_number FROM reviews WHERE account_number = '$account_number' && company_id = '$compid'");