jQuery验证 - 具有附加数据的远程功能

时间:2015-10-05 10:38:19

标签: php jquery forms validation

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

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

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

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

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

表格

<form name="review" id="review" method="post" action="/db_processing/reviews/process-reviews.php">
    <input type="hidden" value="<?php echo($results['company_id']) ?>" name="company_id" />
    <input type="hidden" value="<?php echo($_SESSION["ID"]) ?>" name="account_number" />
    <p class="cs-threequarter">
        <b>Comments:</b><br>
        <textarea name="comments" style="width:95%; height: 150px"></textarea>
    </p>
    <p class="cs-quarter">
        <b>Rating:</b>
            <span class="star-rating">
                <input type="radio" name="rating" value="1"><i></i>
                <input type="radio" name="rating" value="2"><i></i>
                <input type="radio" name="rating" value="3"><i></i>
                <input type="radio" name="rating" value="4"><i></i>
                <input type="radio" name="rating" value="5"><i></i>
            </span>
    </p>
    <p><input class="cs-btn cs-red" name="submit" type="submit" value="Submit Review!"></p>
    <div class="cs-container"></div>
    <div class="cs-error-note" id="cs-error-note3"></div>
</form>

<script src="/js/validation/reviewval.js"></script>

jQuery验证脚本

$(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

<?php
    require('../../../private_html/db_connection/connection.php');
    $conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);    
    if(isset($_POST['account_number'])) {
        $account_number = $_POST['account_number'];
        $compid = $_POST['company_id'];
        $query = $conn->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';
            }
    }
?>

1 个答案:

答案 0 :(得分:1)

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

$(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

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

<input type="hidden" value="<?php echo($results['company_id']) ?>" name="company_id" />

id分配给此输入,因为您在id验证脚本中使用return $("#company_id").val();选择器获取该值,因此它将

<input type="hidden" value="<?php echo($results['company_id']) ?>" name="company_id" id="company_id" />

最后用PHP

在查询中的变量周围加上引号',其余的都很好并正常工作。

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