简体   繁体   English

通过ajax进行表单验证

[英]form validation through ajax

I have form validation and insert query through ajax. 我有表单验证,并通过ajax插入查询。 It is running correctly. 它运行正常。 But If email exists then it should go on another page. 但是,如果存在电子邮件,则应该在另一页上。

My index.php is: 我的index.php是:

<div id="wrap"> <!--wrap start-->
    <br />
    <h1>Check the Email if Already Exist</h1>
     <form action="" method="post" id="mainform">
    <table id="table_data">
        <tr>
            <td>First Name</td>
            <td><input name="fname" type="text" size="30"></td>
            <td><span class="fname_val validation"></span></td>
        </tr>
        <tr>
            <td>Last Name</td>
            <td><input name="lname" type="text" size="30"></td>
              <td><span class="lname_val validation"></span></td>
        </tr>
        <tr>
            <td>Email</td>
            <td><input name="email" type="text" size="30"></td>
            <td><span class="email_val validation"></span></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input name="register" type="button" value="Register"> <span class="loading"></span></td>
            <td></td>
        </tr>
    </table>
   </form>
 </div> <!--wrap end-->

script is: 脚本是:

 <script>
 jQuery(function($) {
var val_holder;
$("form input[name='register']").click(function() { // triggred click
    /************** form validation **************/
    val_holder      = 0;
    var fname       = jQuery.trim($("form input[name='fname']").val()); // first name field
    var lname       = jQuery.trim($("form input[name='lname']").val()); // last name field
    var email       = jQuery.trim($("form input[name='email']").val()); // email field
    var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; // reg ex email check

    if(val_holder == 1) {
        return false;
    }
    val_holder = 0;
    /************** form validation end **************/
    /************** start: email exist function and etc. **************/
    $("span.loading").html("<img src='images/ajax_fb_loader.gif'>");
    $("span.validation").html("");
    var datastring = 'fname='+ fname +'&lname='+ lname +'&email='+ email; // get data in the form manual
    //var datastring = $('form#mainform').serialize(); // or use serialize
    $.ajax({
                type: "POST", // type
                url: "check_email.php", // request file the 'check_email.php'
                data: datastring, // post the data
                success: function(responseText) { // get the response
                    if(responseText == 1) { // if the response is 1
                        $("span.email_val").html("<img src='images/invalid.png'> Email are already exist.");
                        $("span.loading").html("");
                    } else { // else blank response
                        if(responseText == "") {
                            $("span.loading").html("<img src='images/correct.png'> You are registred.");
                            $("span.validation").html("");
                            $("form input[type='text']").val(''); // optional: empty the field after registration
                        }
                    }
                } // end success
    }); // ajax end
    /************** end: email exist function and etc. **************/
}); // click end
}); // jquery end

</script>

check_email.php check_email.php

<?php
require_once("database.php"); // require the db connection
/* catch the post data from ajax */
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$query = mysql_query("SELECT `email` FROM `users` WHERE `email` = '$email'");
if(mysql_num_rows($query) == 1) { // if return 1, email exist.
echo '1';
} 
else { // else not, insert to the table
$query = mysql_query("INSERT INTO `users` (`first_name` ,`last_name` ,`email`) VALUES ('$fname', '$lname', '$email')");

echo "<script type='text/javascript'>window.location='payment.php'  </script>";                 

}
?>

At the check_email.php, if email not exist then page should go on payment.php. 在check_email.php上,如果电子邮件不存在,则页面应转到Payment.php上。 But it is still on same page and payment.php, there is some more query. 但是它仍然在同一页面和payment.php上,还有更多查询。

Use 采用

window.location.href = 'payment.php'

to redirect your browser to another php file, or any file at that 将您的浏览器重定向到另一个php文件或该位置的任何文件

I see you already use window.location.href = 'payment.php'"; in ajax response. So you have two choices : 我看到您已经在ajax响应中使用window.location.href = 'payment.php'"; ;,因此您有两种选择:

Interpret this response ajax in your html like : 像这样在HTML中解释此响应ajax:

success: function(responseText) { // get the response
       if(responseText == "1") { // if the response is 1
             $("span.email_val").html("<img src='images/invalid.png'> Email are already exist.");
             $("span.loading").html("");
        } else { // else blank response
             $("span.loading").html(responseText);
         }
  } // end success

with (don't forget the href ) : 与(不要忘记href ):

echo "<script type='text/javascript'>window.location.href = 'payment.php&email=" . $email;</script>";      

OR you can directly put the redirection in success function like : 或者,您可以将重定向直接放在成功函数中,例如:

success: function(responseText) { // get the response
       if(responseText == "1") { // if the response is 1
             $("span.email_val").html("<img src='images/invalid.png'> Email are already exist.");
             $("span.loading").html("");
        } else { // else blank response
             window.location.href = 'payment.php&email = ' + email;
        }
  } // end success

Moreover, be careful to SQL Injection. 此外,请注意SQL注入。

Now, if i call your check_email.php with a POST parameter : email = foo'.'DROP DATABASE; 现在,如果我使用POST参数调用您的check_email.phpemail = foo'.'DROP DATABASE; , i am able to DROP all your database. ,我可以删除所有数据库。

Check this link to prevent of sql injection : this is essential. 检查此链接以防止sql注入:这是必不可少的。

check_email.php is not returning blank response if e-mail not exits. 如果没有退出电子邮件,check_email.php不会返回空白响应。 It is returning <script type='text/javascript'>window.location='payment.php'</script> so you need to change if(responseText == "") to if(responseText == "<script type='text/javascript'>window.location='payment.php'</script>") or just simply remove if statement. 它会返回<script type='text/javascript'>window.location='payment.php'</script>因此您需要将if(responseText == "")更改为if(responseText == "<script type='text/javascript'>window.location='payment.php'</script>")或者只是删除if语句。

Other option is removing 其他选项正在删除

echo "<script type='text/javascript'>window.location='payment.php'</script>";

and using 和使用

window.location.replace('payment.php')

in ajax call 在ajax电话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM