jQuery验证-带有附加数据的远程功能 [英] jQuery Validation - Remote function with additional data

查看:85
本文介绍了jQuery验证-带有附加数据的远程功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having issues with validating some data.

我想通过检查我的评论表中的company_id和已登录的用户account_number来检查是否有人对公司进行过评论.

I want to check if someone has reviewed a company before by checking for the company_id and the logged in users account_number in my reviews table.

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

The code I currently has doesn't ever seem to find anything in the reviews table so doesn't warn people they can't submit another review.

非常感谢您的帮助.

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

Here is the code I have so far:

表格

<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验证脚本

jQuery Validation Script

$(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();
        }
    });

});

Check-account.php

Check-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';
            }
    }
?>

推荐答案

验证代码运行正常,没有问题,不需要不必要的逗号,.删除它,并不是所有的浏览器都非常宽容.

Validation code working fine, there is no problem expect unnecessary comma ,. remove it, Not all browsers are very forgiving.

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

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

The problem is here, because of it validation and query both failing.

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

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

assign id to this input because you are fetching it's value with id selector in validation script here return $("#company_id").val(); so it will be

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

最后使用PHP

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

put quotes ' around variables inside query, rest is all good and working.

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

这篇关于jQuery验证-带有附加数据的远程功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆