Jquery表单验证和通过PHP脚本使用Mysql数据库检查值

时间:2022-09-26 07:46:56

I have a form which has a input textbox and submit button.

我有一个具有输入文本框和提交按钮的表单。

On submission of the form the textbox value should get pass to an php script and check the values whether it exists in the Mysql Database. If it exists then we need to show an alert box stating that "Entered Value is already exists, Try something new". If the value not exists the form can be submitted to the php script which is in the form action.

在提交表单时,文本框值应该传递给php脚本并检查它是否存在于Mysql数据库中。如果它存在,那么我们需要显示一个警告框,指出“已存在的值已存在,尝试新的事物”。如果该值不存在,则可以将表单提交到表单操作中的php脚本。

I tried with the jquery and the code is below:

我尝试使用jquery,代码如下:

 $(document).ready(function() {

        $("#form_add").submit(function () {
        var pval = $("#name").val(); 
        var datastring = 'pname'+pval;
        $.post("unique_valid.php", {pname:pval }, function (data){
                alert('duplicate');
            });
            return false;
        }); 
});


Problem with this code is It shows alert box on every case but its not allowing to submit the form if the values is not exists in the database.

此代码的问题是它在每个案例中显示警告框,但如果数据库中不存在值,则不允许提交表单。

Php code :

PHP代码:

$pname = $_POST['pname'];
if( $pname == $row['name']){
        echo "success";
    }else{
        echo "failure";
    }

Suggest the better solution for this problem.

为这个问题建议更好的解决方案。

1 个解决方案

#1


That's because you're alerting 'duplicate' no matter what the PHP output is. Try checking the value of data before alerting, like this:

这是因为无论PHP输出是什么,你都会警告'重复'。在发出警报之前尝试检查数据的值,如下所示:

$(document).ready(function() {

        $("#form_add").submit(function () {
        var pval = $("#name").val(); 
        var datastring = 'pname'+pval;
        $.post("unique_valid.php", {pname:pval }, 
                   function (data){
                        if(data == 'failure'){
                            alert('duplicate');
                        }else{
                            alert('not a duplicate'); 
                        }
                });
                return false;
        }); 
});

And I'm assuming your PHP code will actually be saving the record if it's not a duplicate (your code doesn't indicate as much, though)?

而且我假设你的PHP代码实际上会保存记录,如果它不是重复的(虽然你的代码没有表明那么多)?

#1


That's because you're alerting 'duplicate' no matter what the PHP output is. Try checking the value of data before alerting, like this:

这是因为无论PHP输出是什么,你都会警告'重复'。在发出警报之前尝试检查数据的值,如下所示:

$(document).ready(function() {

        $("#form_add").submit(function () {
        var pval = $("#name").val(); 
        var datastring = 'pname'+pval;
        $.post("unique_valid.php", {pname:pval }, 
                   function (data){
                        if(data == 'failure'){
                            alert('duplicate');
                        }else{
                            alert('not a duplicate'); 
                        }
                });
                return false;
        }); 
});

And I'm assuming your PHP code will actually be saving the record if it's not a duplicate (your code doesn't indicate as much, though)?

而且我假设你的PHP代码实际上会保存记录,如果它不是重复的(虽然你的代码没有表明那么多)?