【问题标题】:JS - Check if form is complete filled outJS - 检查表格是否填写完整
【发布时间】:2018-01-11 22:26:59
【问题描述】:

我需要一些有关我的表格的帮助。目前,当用户提交表单时,表单会自行提交(进入 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 中所见的表单模式

【问题讨论】:

  • 您可以通过在所有输入字段中添加 html 标签“required”来强制所有字段。但是,如果您必须使用 javascript,您可以尝试以下操作:stackoverflow.com/questions/3279628/…
  • 您的 html 标记无效 ~ h3 标记对跨越表单(一个在外面,另一个在里面)

标签: javascript php forms


【解决方案1】:
<input required></input>

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多