简体   繁体   English

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

[英]JS - Check if form is complete filled out

I would like some help with my form. 我需要一些表格帮助。 Currently, when the user submits the form, the form gets self-submitted (into main.php). 当前,当用户提交表单时,表单会自行提交(进入main.php)。 I would like to perform a pre-check before submitting to see if the form is completely filled out. 我想在提交之前进行预检查,以查看表单是否完整填写。

Here is my HTML for the form: 这是表单的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>

and my PHP: 和我的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);
                    }
                }

Note: I am using a modal for the form as seen in https://www.w3schools.com/howto/howto_css_modals.asp 注意:我正在使用形式的模态,如https://www.w3schools.com/howto/howto_css_modals.asp所示

<input required></input>

您可以使用输入标签的“必需”属性

Hi if you can use third party libraries I suggest to use the parsleyJs. 嗨,如果您可以使用第三方库,我建议您使用parsleyJs。 With that you can use the 这样您就可以使用

$('form').parsley().validate() to check if the form is valid. $('form').parsley().validate()检查表单是否有效。

And if you want to check that everything is filled you need to use the required tag and also you can use other types and tags https://www.w3schools.com/html/html_form_input_types.asp 如果要检查是否已填充所有内容,则需要使用required标记,还可以使用其他类型和标记https://www.w3schools.com/html/html_form_input_types.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM