JS - 检查表单是否填写完整

时间:2017-08-04 05:46:15

标签: javascript php forms

我想帮助我的表格。目前,当用户提交表单时,表单会自行提交(进入main.php)。我想在提交之前进行预检查,看看表格是否已完全填写。

以下是表单的HTML:

<div id="reqForm" class="modal">
                <div class="modal-content">
                <div class="modal-header">
                    <span class="close">&times;</span>
                    <h2>Reimbersement Form</h2>
                </div>
                <div class="modal-body">
                    <div class="form">
                        <h2>Enter your information:</h2>
                        <br/>
                        <h3>
                        <form action="main" method="post" id="newRequest">
                            <label>Full Name:</label> <input type="text" name="name" value="<?php echo $name; ?>" disabled/><br />

                            <label>Reimberse Amount:</label> <input type="number" min="0" name="amount" /><br />
                            Customer Name: <select name="customerName" id="cN"><option value="" id='cPlSe' selected>--Select--</option><?php 
                            echo $optionC;
                            ?></select><br />Project Name: <select name="projectName" id="pN"><option value="" id='plSe' selected>Please select customer name </option><option value="">--Select--</option><?php
                            echo $optionP;
                            ?></select><br />
                            <label>Reason (max length 255 characters): </label><br /><textarea cols="40" rows="1" form="newRequest" name="reason"></textarea><br /> 
                            <input type="hidden" name="submitted"/>

                        </h3>
                    </div>
                </div>
                <div class="modal-footer"><br />
                    <input type="submit" id="submit"/>
                    </form>
                </div>
              </div>
            </div>

和我的PHP:

if(isset($_POST["submitted"])){
                    $name = $_SESSION["name"];
                    $amount = $_POST["amount"];
                    $projectName = $_POST["projectName"];
                    $customerName = $_POST["customerName"];
                    $reason = $_POST["reason"];
                    $d = array("name"=>$name,"amount"=>$amount,"projectName"=>$projectName,"customerName"=>$customerName,"reason"=>$reason);

                    foreach($d as $i) {
                        if("" == trim($i)) {
                            echo "Make sure you have filled out all the fields. Click <a href='main'>here</a> to go back.";
                            exit();
                        }
                    }


                    foreach($d as $i) {
                        mysqli_real_escape_string($connect, $i);
                    }
                    $name = $d["name"];
                    $amount = $d["amount"];
                    $projectName = $d["projectName"];
                    $customerName = $d["customerName"];
                    $reason = $d["reason"];

                    $query = "INSERT INTO `requests` (`Name`, `Amount`, `ProjectName`, `CustomerName`, `Reason`) VALUES " . "('" . $name . "', '" . $amount . "', '" . $projectName . "', '" . $customerName . "', '" . $reason."');";

                    if (mysqli_query($connect, $query)) {
                        echo "Success!";
                    } else {
                        echo "Error: " . $query . "<br>" . mysqli_error($connect);
                    }
                }

注意:我正在使用https://www.w3schools.com/howto/howto_css_modals.asp

中显示的表单模式

2 个答案:

答案 0 :(得分:1)

<input required></input>

您可以使用输入标记的“必需”attr

答案 1 :(得分:1)

如果您可以使用第三方库,我建议您使用parsleyJs。有了它,你可以使用

$('form').parsley().validate() 检查表格是否有效。

如果您想检查所有内容是否已填满,您需要使用required标记,还可以使用其他类型和标记 https://www.w3schools.com/html/html_form_input_types.asp