将控制从jquery转移到php

时间:2015-04-04 15:56:30

标签: php jquery html

我使用jquery注册表单。但是在输入电子邮件和密码并按下提交按钮后,控件并没有像我预期的那样转移到php。我不完全了解jquery,只想在注册页面上添加此功能。 怎么办呢。

<?php
include "config.php";

if( isset($_POST['submit']) ){
$email = $_POST['email'];
            $pass    = $_POST['password'];


            if( $email=="" || $pass==''){
                echo "Fields Left Blank","Some Fields were left blank. Please fill up all fields.";
                exit;
            }
            if( !$LS->validEmail($email) ){
                echo "E-Mail Is Not Valid", "The E-Mail you gave is not valid";
                exit;
            }


        $createAccount = $LS->register($email, $pass);


            if($createAccount === "exists"){
                echo "User Exists.";
            }elseif($createAccount === true){

                header("Location: /rootTesting/dashboardInitial.php", true, 302);
            die();
            }
    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easing.1.3.js.js">    </script>

<script type="text/javascript"> 
$(function() 
{
$("ul li:first").show();


var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i 
var ck_password =  /^[A-Za-z0-9!@#$%^&*()_]{3,20}$/;


$('#email').keyup(function()
{
var email=$(this).val();
if (!ck_email.test(email)) 
{
 $(this).next().show().html("Enter valid email");
}
else
{ 
$(this).next().hide();
$("li").next("li.password").slideDown({duration: 'slow',easing: 'easeOutElastic'});
}

});



$('#password').keyup(function()
{
var password=$(this).val();
if (!ck_password.test(password)) 
{
 $(this).next().show().html("");
}
else
{
$(this).next().hide();
$("li").next("li.submit").slideDown({duration: 'slow',easing: 'easeOutElastic'});
}
});

$('#submit').click(function()
{
var email=$("#email").val();
var password=$("#password").val();
if(ck_email.test(email) && ck_password.test(password) )
{
$("#form").show().html("<h1>Thank you!</h1>");
}
else
{

}
return false;

}); 
})
</script>
</head>

 <body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<ul>
<li class="email">
<label>Email: </label><br/> 
<input type="text" name="email" id="email" />
    </li>

<li class="password">
<label>Password: </label><br/> 
<input type="password" name="password" id="password" />
</li>

<li class="submit"> 
<input type="submit" value=" Register " id='submit'/>
</li>
</ul>
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

只需在提交按钮中添加name属性,并确保将其设为submit。所以它变成了:

<input type="submit" value=" Register " id='submit' name='submit'/>

PHP按name而不是id发送数据。

答案 1 :(得分:0)

尝试

$('form[name=foo]').submit(function(){
    var email=$("#email").val();
    var password=$("#password").val();
    if(!ck_email.test(email) || !ck_password.test(password) ) {
        return false;
        // errors found
    }
}); 

<form method="post" name="foo">
<ul>
<li class="email">
<label>Email: </label><br/> 
<input type="text" name="email" id="email" />
</li>
<li class="password">
<label>Password: </label><br/> 
<input type="password" name="password" id="password" />
</li>
<li class="submit"> 
<input type="submit" value=" Register " id='submit'/>
</li>
</ul>
</form>

答案 2 :(得分:0)

您错过了该按钮的名称。

由于您正在检查$ _POST ['submit'],您需要将按钮HTML修改为

<input type="submit" name="submit" value="Register" id="submit"/>