0

我正在尝试在将用户详细信息发送到服务器之前确认我的密码字段。我曾尝试使用 jquery 和 php 来实现这一点,但我无法让它工作。我知道这应该相当简单,但它在不确认密码的情况下发送详细信息。

我的代码如下(为了清楚起见,我删除了一些 div)

  <form onSubmit="return validate()" id="register" method="post" action="../script/sendUserDetails.php">
    <p style=" font-size:1.8em; color:black;">Ready</p>
    <p style=" font-size:1.4em; color:black;">Enter your details</p>

        <label for="fname">First Name:&nbsp;</label><br/>
        <input autofocus name="fname" type="text" id="fname" 
         class="textEntry" required placeholder="First Name"/><br />
        <label for="lname">Last Name:&nbsp;</label><br/>
        <input name="lname" type="text" id="lname" class="textEntry" required placeholder="Last Name"/><br />
           <label for="email">Email:&nbsp;</label><br/>
        <input name="email" type="email" id="emailR" class="textEntry" required placeholder="Email"/><br />

   <p style="font-size:1.8em; color:black;">Steady</p>
    <p style="font-size:1.4em; color:black;">And your address</p>
        <label for="address1">Address1:&nbsp;</label><br/>
        <input name="address1" type="text" id="address1" class="textEntry" required placeholder="Address"/><br />
        <label for="address2">Address2:&nbsp;</label><br/>
        <input name="address2" type="text" id="address2" class="textEntry" placeholder="Address" /><br />
        <label for="town">Town:&nbsp;</label><br/>
        <input name="town" type="text" id="town" class="textEntry" required placeholder="Town"/><br />
        <label for="postcode">Post Code:&nbsp;</label><br/>
        <input name="postcode" type="text" id="postcode" class="textEntry" required placeholder="Post Code"/>
        <br />
    <label for="phone">Phone:&nbsp;</label><br/>
        <input name="phone" placeholder="Phone" type="text" id="phone" class="textEntry" required/><br />

<p style="font-size:1.8em; color:black;">Go</p>
    <p style="font-size:1.4em; color:black; ">Now a password</p>

         <label for="password">Password:&nbsp;</label><br/>
        <input name="password" placeholder="Password" type="password" id="passwordR" class="passwordEntry" 
          required/><br />
         <label for="confirm_password">Confirm Password:&nbsp;</label><br/>
        <input name="confirm_password" placeholder="Password" type="password" id="confirm_password" class="passwordEntry" 
          required/><br />

        <p ><input class="button-link" type="submit" id="cu" value="Create Account"/></p>
       </form>  

我的php代码是;

<?php
 include("dbconnect.php");
 $fname=$_POST['fname'];
 $lname=$_POST['lname'];
 $address1=$_POST['address1'];
 $address2=$_POST['address2'];
 $town=$_POST['town'];
 $postcode=$_POST['postcode'];
 $phone=$_POST['phone'];
 $email=$_POST['email'];
 $password=$_POST['password'];
 $query = "select email from members where email='$email'";
 $link = mysql_query($query);
 if (!$link) {
  die('query error');
 }
 $num=mysql_num_rows($link);
 if ($num>0){
  die('email already exists'); //email already taken
 }
 $query = "insert into members (fname, lname, address1, address2, town, postcode, phone, email,  
   password) values('$fname','$lname','$address1','$address2','$town','$postcode','$phone',
   '$email','$password')";
 $link = @ mysql_query($query);
 if (!$link) {
  die('table write error');
 }
 header("location:../members/members.master.php?page=regconfirm.php");
?>

以及我尝试使用的 jquery 代码(来自此站点);

function validate(){

    if(!document.getElementById("Password").value==document.getElementById("confirm_password").value)alert("Passwords do no match");
    return document.getElementById("Password").value==document.getElementById("confirm_password").value;
   return false;
    }

谢谢你的帮助。

4

2 回答 2

3

你的密码ID拼错了...

这里

id="passwordR"

应该

<input name="password" placeholder="Password" type="password" id="password" class="passwordEntry" required/>

所以你的条件在这里总是错误的,因为它无法获得密码的值

if(!document.getElementById("Password").value==document.getElementById("confirm_password").value)alert("Passwords do no match");

这应该是

 if(!document.getElementById("password").value==document.getElementById("confirm_password").value)alert("Passwords do no match");

注意: 如果您通过 id 获取元素...两者的 id 应该完全相同..

于 2012-12-14T11:47:43.787 回答
0

试试这个passwordR

    function validate(){
 var password = document.getElementById("PasswordR") ;
 var confpassword = document.getElementById("confirm_password") ;
  if(password.value != confpassword.value) {
 alert("Passwords do no match");

 return false;
 }
 return true;
  }
于 2012-12-14T11:51:43.977 回答