簡體   English   中英

JS-檢查表格是否填寫完整

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

我需要一些表格幫助。 當前,當用戶提交表單時,表單會自行提交(進入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所示

<input required></input>

您可以使用輸入標簽的“必需”屬性

嗨,如果您可以使用第三方庫,我建議您使用parsleyJs。 這樣您就可以使用

$('form').parsley().validate()檢查表單是否有效。

如果要檢查是否已填充所有內容,則需要使用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