阿贾克斯PHP + MySQL的登记表教程 [英] Ajax PHP + MySQL registration form tutorial

查看:159
本文介绍了阿贾克斯PHP + MySQL的登记表教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在为止我有 HTML

Until now I have HTML

<form id="account_reg" action="reg.php" method="post">
    <div id="response"></div>
    <div class="input">
      <label>Login</>
      <input name="login" type="text" class="required" id="login" size="30"/>
    </div>

    <div class="input">
    <label>Password</>
    <input name="password" type="text" class="required" id="password" size="30"/>
    </div>

    <div class="input">
    <label>Repeat Password</>
    <input name="rpassword" type="text" class="required" id="rpassword" size="30"/>
    </div>

    <div class="input">
    <label>Email</>
    <input name="email" type="text" class="required" id="email" size="30"/>
    </div>

    <div class="button">
    <input type="submit" name="send" id="send" value="Send"/>
    </div>

    <div class="input">
    <input type="hidden" name="honeypot" id="honeypot" value="http://"/>
    <input type="hidden" name="humancheck" id="humancheck" class="clear" value=""/>
    </div>
</form>

我的PHP 我有一些验证服务器端。 `

MY PHP I have some validation server side. `

include("dop/config.php"); //includ Db connect

    $login = ($_POST['login']);
    $password = ($_POST['password']);
    $rpassword = ($_POST['rpassword']);
    $email = ($_POST['email']);
    $humancheck = $_POST['humancheck'];
    $honeypot = $_POST['honeypot']; 
if ($honeypot == 'http://' && empty($humancheck)) { 
$error_message = '';
$reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-Za-Z.](2,4)$/";
if(!preg_match($reg_exp, $email)){
$error_message .="<p>A Valid email is required</p>";
}
if(empty($login)){
$error_message .="<p>enter login</p>";
}
if(empty($password)){
$error_message .="<p>Enter password</p>";
}
if(empty($rpassword)){
$error_message .="<p>Enter password again</p>";
}
if($password != $rpassword){
$error_message .="<p>password mut match</p>";
}
} 
else {
$return['error'] = true;
$return['msg'] = "<h3>There was a problem with your submission. Please try again.</h3>";    
    echo json_encode($return);
} 

我的JS 验证。

My JS With Validation.

$('#send').click(function(e)
    {
        e.preventDefault();
        var valid = '';
        var required =' is required.';
        var login = $('#account_reg #login').val();
        var password = $('#account_reg #password').val();
        var rpassword = $('#account_reg #rpassword').val();
        var email = $('#account_reg #email').val();
        var honeypot = $('#account_reg #honeypot').val();
        var humancheck = $('#account_reg #humancheck').val();

        if(login = ''){
            valid ='<p> Your Name '+ required +'</p>';
        }
        if(password='' || company.length<=6){
            valid +='<p> Password '+ required +'</p>';
        }
        if(rpassword != password){
            valid +='<p> password must match </p>';
        }
        if(!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))
        {
            valid +='<p> Your Email' + required +'</p>';
        }


        if (honeypot != 'http://') {
            valid += '<p>Spambots are not allowed.</p>';    
        }

        if (humancheck != '') {
            valid += '<p>A human user' + required + '</p>'; 
        }
        if(valid !=''){
            $('#account_reg #response').removeClass().addClass("error")
                .html('<strong> Please Correct the errors Below </strong>' + valid).fadeIn('fast');
        }
        else{
            //$('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');
            var formData = $('#account_reg').serialize();
            $('#send').val("Please wait...");
            submitForm(formData);
        }
    });
function submitForm(formData) {

$.post('reg2.php',formData, function(data){
    //console.log(data);
    $('#send').val("Send");
    if (data === '1') {
        alert('ok!');
    } else {
        alert('wrong shit');
    }
});
};

我力还做了MySQL的一部分,随着插入,如果账户已经被注册了检查。

I dint do yet the MySQL part With Inserting and checking if account is already registered.

这是我的第一个问题是点击的重定向我的reg.php和我有数据插入数据库的问题。

The first problem that I have is on Click is redirect me on reg.php and I am having problems with inserting the data into the DB.

如果有人知道一个很好的教程创建登记表或任何建议。 我不够好于阿贾克斯和JSON调用。

If anybody Knows a good tutorial for creating registration form or any suggestion. I am not good enough in Ajax and Json calls.

推荐答案

该表单提交,这就是为什么你能reg.php。从我看到你所提交的数据与自定义函数反正所以最快捷的方式来解决,这将是改变提交输入到一个普通按钮。因此,

The form is submitted, that is why you get to reg.php. From what I see you are submitting the data with a custom function anyway so the quickest way to fix that would be to change the submit input into a regular button. So

<input type="submit" name="send" id="send" value="Send"/>

变为

<input type="button" name="send" id="send" value="Send"/>

我认为,它也可能帮助,如果你返回false,当你发现在验证表单时功能的错误。

I think that it might also help if you return false when you find an error in the function that validates the form when.

A面的问题:我觉得奇怪的是,你选择了与值的蜜罐领域上(http://)。通常这些字段为空..有什么具体的理由这样做的?

A side question: I find it strange that you chose to have a honeypot field with a value (http://). Usually those fields are empty.. Is there any specific reason for doing so?

这篇关于阿贾克斯PHP + MySQL的登记表教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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