尝试将值从php传递到jquery以获取我的注册表单

时间:2015-09-09 13:18:14

标签: javascript php jquery

我正在尝试将值从php传递回我的jquery,以便可以动态更改错误消息。任何帮助都将受到赞赏。下面的代码显示了我到目前为止所尝试的内容。

jquery页面

function validate(fname) {
    $.post('php/registration.php', {
        fname: fname
    }, function (data) {
        $('#fname_feedback').text(data.fname);

    });
}

$('#fname').focusin(function () {
    if ($('#fname').val() === '') {
        $('#fname_feedback').text('requried field');
    } else {
        validate($('#fname').val());
    }
}).blur(function () {
    $('#fname_feedback').text('');
    validate($('#fname').val());
}).keyup(function () {
    validate($('#fname').val());
});

php页面

<?php
    require '../init.php';
    $data = array("fname" => '$fname');
    if(isset($_POST['fname'])){
        $fname = $_POST['fname'];
    if (strlen($fname) < 5) {
        $fname = 'fname must be greater than five letters long';
    }
    echo json_encode($data);
    }
?>

登记表

<html lang="en">

<head>
     <meta charset="utf-8">
     <title>jQuery Tutorial</title>
</head>

<body>

<input type="text" id="fname" size ="25" placeholder="firstname" />
<span id="fname_feedback"></span></br></br>
<input type="text" id="lname" size ="25" placeholder="lastname" />
<span id="lname_feedback"></span></br></br>
<input type="text" id="username" size ="25" placeholder="username"/>
<span id="username_feedback"></span></br></br>
<input type="text" id="email" size ="25" placeholder="email"/>
<span id="email_feedback"></span></br></br>
<input type="text" id="email2" size ="25" placeholder="confirm email"/>
<span id="email2_feedback"></span></br></br>
<input type="text" id="password" size ="25" placeholder="password"/>
<span id="password_feedback"></span></br></br>
<input type="text" id="password2" size ="25" placeholder="confirm password"/>
<span id="password2_feedback"></span></br></br>
<input type="submit" id="reg" value="sign up">



<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/reg.js"></script>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

<html lang="en">

<head>
     <meta charset="utf-8">
     <title>jQuery Tutorial</title>
</head>

<body>

<form action="registration.php" method="POST" onsubmit="return CheckValidation()">
    <input type="text" id="fname" size ="25" placeholder="firstname"  onkeyup="FirstName()"/>
    <span id="fname_feedback"></span></br></br>
    <input type="text" id="lname" size ="25" placeholder="lastname" />
    <span id="lname_feedback"></span></br></br>
    <input type="text" id="username" size ="25" placeholder="username"/>
    <span id="username_feedback"></span></br></br>
    <input type="text" id="email" size ="25" placeholder="email"/>
    <span id="email_feedback"></span></br></br>
    <input type="text" id="email2" size ="25" placeholder="confirm email"/>
    <span id="email2_feedback"></span></br></br>
    <input type="text" id="password" size ="25" placeholder="password"/>
    <span id="password_feedback"></span></br></br>
    <input type="text" id="password2" size ="25" placeholder="confirm password"/>
    <span id="password2_feedback"></span></br></br>
    <input type="submit" id="reg" value="sign up">
</form>


<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/reg.js"></script>
//Create One JS File Here. Keep All Those Validation Here
<script type="text/javascript" src="js/FormJValidation.js"></script> 

<强> FormJValidation.js

$(function FirstName() {
      $("#fname").blur(function ()
      {
        var FName=$('#fname').val();
        if(strlen(FName) < 5)
          $('#fname_feedback').text('fname must be greater than five letters long');
      });
    });
    function CheckValidation()
    {
        var FName=$('#fname').val();
        var LName=$('#lname').val();
        var UserName=$('#username').val();
        var Email=$('#email').val();
        var ConfirmEmail=$('#email2').val();
        var Password=$('#password').val();
        var ConfirmPassword=$('#password2').val();
        if(FName=="")
        {
            $('#fname_feedback').text('requried field');
            return ErrorMessage;
        }
        if(strlen(FName) < 5)
        {
            $('#fname_feedback').text('fname must be greater than five letters long');
            return false;
        }
        if(LName=="")
        {
            $('#lname_feedback').text('requried field');
            return false;
        }
        if(UserName=="")
        {
            $('#username_feedback').text('requried field');
            return false;
        }
        if(Email=="")
        {
            $('#email_feedback').text('requried field');
            return false;
        }
        if(ConfirmEmail=="")
        {
            $('#email2_feedback').text('requried field');
            return false;
        }
        if(Password=="")
        {
            $('#password_feedback').text('requried field');
            return false;
        }
        if(ConfirmPassword=="")
        {
            $('#password2_feedback').text('requried field');
            return false;
        }
        if(ConfirmPassword!=Password)
        {
            $('#password2_feedback').text('Password & Confirm Password Not Matched');
            return false;
        }
    }