我的报告创建无效的php表单验证

时间:2015-11-22 05:51:17

标签: javascript php forms validation submit

我目前正在尝试验证我的表单以检查任何空字段以及正则表达式,但问题是当我提示激活时验证不起作用。它在我的其他页面上运行良好,我似乎无法找到问题。

PHP PAGE:CreateMonthlyFBForm(非工作)

<?php
include "dbFunctions.php";
session_start();

if (!isset($_SESSION['user_id'])) {
    echo "You do not have access to this page.<br/>";
    echo "Please <a href='login.php'>login first</a>.";
    exit;
} elseif ($_SESSION['department'] == "CEP") {
    echo "Your account is not authorised to use the webpage.";
    echo "Please return to <a href='index.php'>index</a>";
    exit;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <link href="stylesheets/style.css" rel="stylesheet" type="text/css"/>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
        <script>
            $(function() {
                $('.datepick').datepicker();
            });
        </script>
        <script type="text/javascript">
            var RE_REFNO = /^[0-9/]+$/;
            var RE_MTNNYR = /^[0-9]{2}[/][0-9]{4}$/;
            var RE_SUBINS = /^[a-zA-Z]+$/;
            function validate(form)
            {
                var refno = form.refno.value;
                var mthnyr = form.mthnyr.value;
                var subins = form.subins.value;
                //var date = form.date.value;
                //var time = form.time.value;
                //var RO = form.RO.value;
                //var Designation = form.Designation.value;
                //var Contact = form.Contact.value;
                //var priority = form.priority.value;
                //var no1 = form.no1.value;
                //var no2 = form.no2.value;
                //var no3 = form.no3.value;
                //var no4 = form.no4.value;
                //var no5 = form.no5.value;
                //var no6 = form.no6.value;
                //var no7 = form.no7.value;
                //var no8 = form.no8.value;
                //var fb1 = form.fb1.value;
                //var fb2 = form.fb2.value;
                //var fb3 = form.fb4.value;
                //var fb4 = form.fb4.value;
                //var fb5 = form.fb5.value;
                var errors = [];
                if (!RE_REFNO.test(refno))
                {
                    errors[errors.length] = "Reference number must not contain letters and symbols other than '/'.";
                }
                if (refno == "") {
                    errors[errors.length] = "Reference number cannot be blank.";
                }
                if (!RE_MTHNYR.test(mthnyr))
                {
                    errors[errors.length] = "Month and Year must be written in this format (MM/YYYY)";
                }
                if (mthnyr == "") {
                    errors[errors.length] = "Month and Year cannot be blank.";
                }
                if (!RE_SUBINS.test(subins))
                {
                    errors[errors.length] = "Submitting Insititude does not have any numbers or symbols. Please recheck and re-enter.";
                }
                if (subins == "") {
                    errors[errors.length] = "Submitting Insititude cannot be blank.";
                }
                if (errors.length > 0) {
                    //alert("something is wrong");  
                    var msg = "Report creation Invalid.";
                    for (var i = 0; i < errors.length; i++) {
                        msg += "\n" + errors[i];
                    }
                    alert(msg);
                    return false;
                }
                return true;
            }
        </script>
        <title>RP Incident & Crisis Management System | Create CEP Monthly Feedback Form</title>
    </head>
    <body>
        <a href="index.php"><h1>RP Incident & Crisis Management System</h1></a>
        <img src="images/header_rplogo.png" style="position: absolute; top: 0; right: 0; border: 0;" height="80" width="220"/>
        <hr>
        <?php if ($_SESSION['department'] == "BCM") { ?>
            <table border="1" width="75%" align="center">
                <tr align="center">
                    <td width="13%"><a href="index.php">Home</a></td>
                    <td width="13%"><a href="search.php">Search for reports</a></td>
                    <td width="13%"><a href="create.php">Create report</a></td>
                    <td width="13%"><a href="edit.php">Edit reports</a></td>
                    <td width="13%"><a href="logout.php">Log out</a></td>
                </tr>
            </table>
        <?php } ?>
        <?php if ($_SESSION['department'] == "CEP") { ?>
            <table border="1" width="75%" align="center">
                <tr align="center">
                    <td width="13%"><a href="index.php">Home</a></td>
                    <td width="13%"><a href="search.php">Search for reports</a></td>
                    <td width="13%"><a href="create.php">Create report</a></td>
                    <td width="13%"><a href="edit.php">Edit reports</a></td>
                    <td width="13%"><a href="logout.php">Log out</a></td>
                </tr>
            </table>
        <?php } ?>
        <br>
        <h2>CEP MONTHLY FEEDBACK FORM</h2>
        <form method="post" action="doMonthlyFBForm.php" class="formLayout" onSubmit="return validate(this)">
            <table align="center">
                <tr>
                    <td><label for="refno" for="refno"><b>Reference Number:<br><i>(Institution/Submitting Department/Running Number)</i></b></label></td>
                    <td><input type="text" size="28" name="refno"></td>
                </tr>
                <tr>
                    <td><label for="mthnyr" for="mthnyr"><b>Month/Year:</b></label></td>
                    <td><input type="text" size="28" name="mthnyr"></td>
                </tr>
                <tr>
                    <td><label>To:</label></td>
                    <td>HED, MOE</td>
                </tr>
                <tr>
                    <td><label>Email:</label></td>
                    <td><b>Gan_Beng_Tat@moe.gov.sg</b></td>
                </tr>
                <tr>
                    <td><label for="subins" for="subins">Submitting Institution</label></td>
                    <td><input type="text" size="28" name="subins"></td>
                </tr>
                <tr>
                    <td><label for="date" for="date">Date of report submission</label></td>
                    <td><input type="text" size="28" class="datepick" id="datepicker1" name="date"></td>
                </tr>
                <tr>
                    <td><label for="time" for="time">Time of report submission</label></td>
                    <td><input type="text" size="28" name="time"></td>
                </tr>
                <tr>
                    <td><label for="Name" for="Name">Reporting Officer Name:</label></td>
                    <td><input type="text" size="28" name="RO"></textarea></td>
                </tr>
                <tr>
                    <td><label for="Designation" for="Designation">Reporting Officer Designation:</label></td>
                    <td><input type="text" size="28" name="Designation"></textarea></td>
                </tr>
                <tr>
                    <td><label for="Contact" for="Contact">Reporting Officer Contact No:</label></td>
                    <td><input type="text" size="28" name="Contact"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2"><h3><u>Details of Specific Incident</h3></u></td>
                </tr>
                <tr>
                    <td><label><b>Priority:</b></label></td>
                    <td><input type="radio" name="priority" value="Immediate"/>Immediate
                        <input type="radio" name="priority" value="Urgent"/>Urgent
                        <input type="radio" name="priority" value="Routine"/>Routine (Please tick)</td>
                </tr>
                <tr>
                    <td><label for="no1" for="no1">Nature of Incident:</label></td>
                    <td><textarea id="no1" rows="3" size ="20" cols="35" name="no1"></textarea></td>
                </tr>
                <tr>
                    <td><label for="date" for="date">Date of Incident:</label></td>
                    <td><input type="text" size="28" class="datepick" id="datepicker2" name="no2"></td>
                </tr>

                <tr>
                    <td><label for="no2" for="no2">Time of Incident:</label></td>
                    <td><input type="text" size="28" name="no3"></textarea></td>
                </tr>
                <tr>
                    <td><label for="no3" for="no3">Location of Incident:</label></td>
                    <td><textarea id="no3" rows="1" size ="20" cols="35" name="no4"></textarea></td>
                </tr>
                <tr>
                    <td><label for="no4" for="no4">Particulars of Parties </br> Involved (if available):</label></td>
                    <td><textarea id="no4" rows="1" size ="20" cols="35" name="no5"></textarea></td>
                </tr>
                <tr>
                    <td><label for="no5" for="no5">Details of incident:</label></td>
                    <td><textarea id="no5" rows="5" size ="20" cols="35" name="no6"></textarea></td>
                </tr>
                <tr>
                    <td><label for="no6" for="no6">Action taken:</label></td>
                    <td><textarea id="no6" rows="2" size ="20" cols="35" name="no7"></textarea></td>
                </tr>
                <tr>
                    <td><label for="no7" for="no7">Remarks: (e.g. any decision/support required from MHA, any problems encountered/challenges faced when taking action, any lessons learnt, etc)</label></td>
                    <td><textarea id="no7" rows="5" size ="20" cols="35" name="no8"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2"><h3><u>Feedback (if any)</h3></u></td>
                </tr>
                <tr>
                    <td><label for="fb1" for="fb1">Feedback:</label></td>
                    <td><textarea id="fb1" rows="3" size ="20" cols="35" name="fb1"></textarea></td>
                </tr>
                <tr>
                    <td><label for="fb2" for="fb2">Feedback from?:</label></td>
                    <td><textarea id="fb2" rows="1" size ="20" cols="35" name="fb2"></textarea></td>
                </tr>
                <tr>
                    <td><label for="fb3" for="fb3">Details of Feedback:</label></td>
                    <td><textarea id="fb3" rows="3" size ="20" cols="35" name="fb3"></textarea></td>
                </tr>
                <tr>
                    <td><label for="fb4" for="fb4">Action taken:</label></td>
                    <td><textarea id="fb4" rows="3" size ="20" cols="35" name="fb4"></textarea></td>
                </tr>
                <tr>
                    <td><label for="fb5" for="fb5">Remarks:</label></td>
                    <td><textarea id="fb5" rows="3" size ="20" cols="35" name="fb5"></textarea></td>
                </tr>
                <tr>
                        <td></td>
                        <td colspan="2"><input type="submit" value="Create Report" name="submit"/></td>
                </tr>
            </table>
        </form>
    </body>
</html>

现在发生的事情是,我只想尝试检查前3个字段,但每次点击提交时都会进入下一页。

PHP PAGE:注册(工作)

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link href="stylesheets/style.css" rel="stylesheet" type="text/css"/>
        <title>RP Incident & Crisis Management System | Register</title>
        <script type="text/javascript">
            var RE_NAME = /^[a-zA-Z]+$/;
            var RE_EMAIL = /^[A-Za-z0-9]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
            var RE_USERNAME = /^[a-zA-Z0-9]+$/;
            var RE_PASSWORD = /^[A-Za-z\d]{8,30}$/;
            function validate(form)
            {
                var name = form.name.value;
                var email = form.email.value;
                var username = form.username.value;
                var password = form.password.value;
                var confirmPassword = form.confirmPassword.value;
                var department = form.department.value;
                var errors = [];
                if (!RE_NAME.test(name))
                {
                    errors[errors.length] = "Name is invalid. It must not contain numbers and symbols.";
                }

                if (!RE_EMAIL.test(email))
                {
                    errors[errors.length] = "Email is invalid. Please check if you've mispelled your email or forgotten the '@' symbol.";
                }

                if (!RE_USERNAME.test(username))
                {
                    errors[errors.length] = "Username Invalid. Username must not contain symbols.";
                }

                if (department == "") {
                    errors[errors.length] = "Please select a department.";
                }

                if (password != confirmPassword) {
                    errors[errors.length] = "Passwords do not match.";
                    form.password.value = "";
                    form.confirmPassword.value = "";
                } else {
                    if (!RE_PASSWORD.test(password)) {
                        errors[errors.length] = "You must enter a valid password.";
                    }
                }
                if (errors.length > 0) {
                    //alert("something is wrong");
                    var msg = "Registration Invalid.";
                    for (var i = 0; i < errors.length; i++) {
                        msg += "\n" + errors[i];
                    }
                    alert(msg);
                    return false;
                }
                return true;
            }
        </script>
    </head>
    <body>
        <a href="index.php"><h1>RP Incident & Crisis Management System</h1></a>
        <img src="images/header_rplogo.png" style="position: absolute; top: 0; right: 0; border: 0;" height="80" width="220"/>
        <hr>    
        <h2>REGISTER</h2>
        <form method="post" action="doRegister.php" class="formLayout" onSubmit="return validate(this);">
            <table align="center">
                <tr>
                    <td><label><b>Name:</b></label></td>
                    <td><input type="text" size="28" name="name"/></td>
                </tr>
                <tr>
                    <td><label><b>Username:</b></label></td>
                    <td><input type="text" size="28" name="username"/></td>
                </tr>    
                <tr>
                    <td><label><b>Password:</b></label></td>
                    <td><input type="password" size="28" name="password"/></td>
                </tr>
                <tr>
                    <td><label><b>Confirm<br> Password:</b></label></td>
                    <td><input type="password" size="28" name="confirmPassword"/></td>
                </tr>
                <tr>
                    <td><label><b>Email:</b></label></td>
                    <td><input type="text" size="40" name="email"/></td>
                </tr>
                <tr>
                    <td><label><b>Department:</b></label></td>
                    <td><select name="department">
                            <option value="">Select your department</option>
                            <option value="BCM">BCM Taskforce</option>
                            <option value="CEP">CEP Taskforce</option>
                            <option value="Flu">Flu Taskforce</option>
                            <option value="ECC">ECC</option>
                            <option value="CC">Crisis Comms</option>
                        </select></td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="2"><input type="submit" value="Register" name="submit"/></td>
                </tr>
                <input type="hidden" name="role" value="member"/>
            </table>
            <br>
            <a href="login.php"><i>Have an account?</i></a>
        </form> 
    </body>
</html>

这是一个有效的工作。但我似乎无法让这个功能在我的另一页上运行。

对不起,我还是新手,坦率地说整个编码事情非常糟糕,而且我只是试图将我的任务转交给这个模块的毕业生和gtfo。

1 个答案:

答案 0 :(得分:0)

如果您查看控制台,您会发现发生了错误,导致代码中途错误,这是由于RE_MTHNYR变量拼写失误造成的:

<script type="text/javascript">
var RE_REFNO = /^[0-9/]+$/;
// Spelling of ---> RE_MTNNYR
// Should be -----> RE_MTHNYR
// Notice double "NN" instead of "HN"
var RE_MTNNYR = /^[0-9]{2}[/][0-9]{4}$/;

var RE_SUBINS = /^[a-zA-Z]+$/;
function validate(form) {
    ...........etc.
    }
    // This is the name used here "RE_MTHNYR"
    if (!RE_MTHNYR.test(mthnyr)) {