1

我是 PHP 新手,在数据库中注册用户时遇到问题。我想首先使用 javascript 验证表单,然后执行表单操作,这会将其重定向到另一个包含 PHP 代码和 SQL 查询的页面。但问题是它只验证用户名和电子邮件。javascript不检查其他字段。 注册.php

<script type = "text/javascript"> 
function initthis() {
    document.getElementById("signup").disabled = true;
}

function dis_enable_submit() {
    if (document.getElementById("t&d").checked == 1) {
        document.getElementById("signup").disabled = false;
        document.getElementById("signup").className = "enabled";
    } else {
        document.getElementById("signup").disabled = true;
        document.getElementById("signup").className = "disabled";

    }
}
window.onload = initthis;

function validateForm() {
    var a = document.forms["register"]["duser"].value;
    var b = document.forms["register"]["demail"].value;
    var c = document.forms["register"]["dcemail"].value;
    var d = document.forms["register"]["dpwd"].value;
    var e = document.forms["register"]["dcpwd"].value;

    if (a == null || a == "") {
        document.getElementById("duser").style.background = "#FDFCDC";
        return false;
    }

    var atpos = b.indexOf("@");
    var dotpos = b.lastIndexOf(".");
    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= b.length) {
        document.getElementById("demail").style.background = "#FDFCDC";
        return false;
    } else {
        document.getElementById("tick").src = "images/tick.gif";
    }
    if (c == null || c == "") {
        document.getElementById("dcemail").style.background = "#FDFCDC";
        alert("Email doesn't match");
        return false;
    }

    if (c != b) {
        alert("Email doesn't match");
        return false;
    }

    if (d == null || d == "") {
        document.getElementById("dpwd").style.background = "#FDFCDC";
        return false;
    }

    if (e == null || e == "") {
        document.getElementById("dcpwd").style.background = "#FDFCDC";
        alert("Password doesn't match");
        return false;
    }


    if (e != d) {
        alert("Password doesn't match");
        return false;
    }


}

这是要验证的表格

<form name="register" onsubmit="return validateForm();" method="post" action="getdata.php">
    <table border="0">
        <tr>
            <td colspan="2" style="background:url('images/register.jpg') no-repeat; height:50px; width: 350px;"></td>
        </tr>
        <tr>
            <td>
                <label>*Username:</label>
            </td>
            <td>
                <input name="duser" id="duser" value="" type="text" spellcheck="false" size="20" />
            </td>
        </tr>
        <tr>
            <td>
                <label>*Email:</label>
            </td>
            <td>
                <input name="demail" id="demail" value="" type="text" size="20" />
            </td>
        </tr>
        <tr>
            <td>
                <label>*Confirm Email:</label>
            </td>
            <td>
                <input name="dcemail" id="dcemail" value="" type="text" size="20" />
            </td>
        </tr>
        <tr>
            <td>
                <label>*Password:</label>
            </td>
            <td>
                <input name="dpwd" id="dpwd" value="" type="password" size="20" />
            </td>
        </tr>
        <tr>
            <td>
                <label>*Confirm Password:</label>
            </td>
            <td>
                <input name="dcpwd" id="dcpwd" value="" type="password" size="20" />
            </td>
        </tr>
        <tr>
            <td>
                <label>*Gender:</label>
            </td>
            <td>
                <input type="radio" name="gender" id="gender" value="1">Male
                <input type="radio" name="gender" value="0">Female</td>
        </tr>
        <tr>
            <td>
                <label>Birthday:</label>
            </td>
            <td>
                <input name="bday" id="bday" type="date" size="20" />
            </td>
        </tr>
        <tr>
            <td align="middle">
                <input id="signup" class="button" type="submit" value="Sign Up" style="font-size:15px;" />
            </td>
        </tr>
    </table>
</form>

获取数据.php

<?php
$_POST[duser];
$_POST[demail];
$_POST[dpwd];
$_POST[gender];

$con=mysqli_connect("localhost","root","root","MyDB");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO Users (UserName, Email, Password, Gender)
VALUES ('$_POST[duser]', '$_POST[demail]','$_POST[dpwd]',$_POST[gender] )");

mysqli_close($con);


header("Location: successful.php");
exit();

?>
4

3 回答 3

1

仅供参考:在寻求帮助或简单地解释您想要做什么时,请更具描述性,您将得到快速响应,而不是从互联网上复制代码并想知道为什么它不起作用。我看到你有 dis_enable_submit() 函数,但我看不到你在哪里调用它。如果你有这么长的文件,你也可以使用 jsfiddle.net/‎。

于 2013-08-13T11:52:47.030 回答
0

它没有运行您的validateForm()函数,因为您没有在任何地方调用该函数。

基于现有代码的建议:

<input onclick="return validateform()" id="signup" class="button" type="submit" value="Sign Up" style="font-size:15px;" />

这应该在您按下按钮时运行验证,并且只有在该函数返回 TRUE 时才完成提交。

因此,您还必须return true;在函数末尾添加 a ,这样如果它通过了所有测试,即不执行 areturn false;它会return true,这不是默认设置。

于 2013-08-13T11:58:09.467 回答
0

这是一些示例代码。

<head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
            <script>
            $.validator.setDefaults({
            submitHandler: function() { alert("submitted!"); }
         });

         $().ready(function() {
             // validate the comment form when it is submitted
             $("#commentForm").validate();

         });
        </script>

    </head>



    <form id="commentForm" class="formular" method="post" action="" style="width:600px">
                <p class="name">
                    <input value="" class="text-input" type="text" name="fullname" id="fullname" required />
                    <label for="fullname">Name</label>
                </p>
                <p class="email">
                    <input value="" class="text-input" type="text" name="email" id="email" required />
                    <label for="email">E-mail</label>
                </p>
                <p class="web">
                    <input type="text" name="web" id="web" />
                    <label for="web">Website</label>
                </p>
                <p class="text">
                    <textarea name="text" class="text-input" id="comment" required></textarea>
                </p>
                <p class="submit">
                    <input type="submit" id="submit" value="Send" />
                </p>
            </form>
于 2013-08-13T11:56:42.470 回答