简体   繁体   English

如何显示错误并不允许以PHP形式提交两个输入

[英]How to have show error and not allow two inputs to be submitted in form PHP

One my form I have the option to choose from a select box, or to manually enter the information. 在我的表格中,我可以选择一个选择框,也可以手动输入信息。

I want to prevent user from entering both. 我想防止用户同时输入两者。 They should either choose from the select OR enter manually if their selection is not already there. 他们应该从选择中进行选择,或者如果尚无选择,则手动输入。

html code HTML代码

<div class="row">
                <div class="form-group col-sm-4">
                  <div class="row">
                    <div class="col-sm-3">
                        <label for="selectComplvl" class="regions" >Region</label></div>
                    <div class="col-sm-8">
                      <select class="form-control regionbox" id="region" value="" name="region">
                        <option>---Select---</option>
                        <?php
                          $region_sql = "SELECT * FROM `regions`";
                          $region_res = mysqli_query($con, $region_sql);
                          if(mysqli_num_rows($region_res) > 0){
                             while($region_row = mysqli_fetch_array($region_res)){
                                  echo "<option value='".$region_row[id]."'>".$region_row[region]."</option>";
                              } 
                          }

                        ?>

                      </select>
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                </div>

                <div class="form-group col-sm-4">
                  <div class="row">
                    <div class="col-sm-4">
                        <label for="feisEntered" class="feis">Feis Entered</label></div>
                    <div class="col-sm-8">
                      <select class="form-control feisbox" id="feisEntered" name="feisEntered">
                        <option>First choose a Region</option>

                      </select>
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                </div>

                <div class="form-group col-sm-4">
                  <div class="row">
                    <div class="col-sm-3">
                        <label for="feisEntered" class="enterfeis">Or Enter</label></div>
                    <div class="col-sm-8">
                      <input id="inputfeis" type="text" name="feisEntered" class="form-control" placeholder="Feis name - MM/DD/YYYY">
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                </div>
            </div>

And my query: 而我的查询:

if(isset($_POST['submit'])){  
    $dancer=$_POST['dancer'];
    $dancer = mysqli_real_escape_string($con,$dancer);
    $level=$_POST['level'];
    $level = mysqli_real_escape_string($con,$level);
    $feisEntered=$_POST['feisEntered'];
    $feisEntered = mysqli_real_escape_string($con,$feisEntered);
    $danceName=$_POST['danceName'];
    $danceName = mysqli_real_escape_string($con,$danceName);
    $compNum=$_POST['compNum'];
    $compNum = mysqli_real_escape_string($con,$compNum);
    $competitor = $_POST['competitor'];
    $competitor = mysqli_real_escape_string($con,$competitor);
    $danceScore = $_POST['danceScore'];
    $danceScore = mysqli_real_escape_string($con,$danceScore);
    $placement = $_POST['placement'];
    $placement = mysqli_real_escape_string($con,$placement);
    $firstScore = $_POST['firstScore'];
    $firstScore = mysqli_real_escape_string($con,$firstScore);
    $secondScore = $_POST['secondScore'];
    $secondScore = mysqli_real_escape_string($con,$secondScore);
    $thirdScore = $_POST['thirdScore'];
    $thirdScore = mysqli_real_escape_string($con,$thirdScore);
    $judge = $_POST['judge'];
    $judge = mysqli_real_escape_string($con,$judge);
    $comments = $_POST['comments'];
    $comments = mysqli_real_escape_string($con,$comments);


$query = "INSERT INTO reports (user_id, dancer_id1, dancer_name, competition_level1, feis_entered, dance_name1, competition_number1, number_competitors1, dancer_score1, dancer_placement1, firstpl_score1, 2ndpl_score1, 3rdpl_score1, judge_name1, judge_comment1) VALUES ('$id','$dancerID', '$dancer', '$level', '$feisEntered', '$danceName', '$compNum', '$competitor', '$danceScore', '$placement', '$firstScore', '$secondScore', '$thirdScore', '$judge', '$comments')";

     $result = mysqli_query($con, $query);
}

So what I want to happen is a error message appears if they fill out both saying "Please choose only one". 因此,我想发生的是,如果他们都填写“请选择一个”,则会出现一条错误消息。 That way only one enters into database. 这样,只有一个人进入数据库。

In your HTML document, you should use Javascript to validate the user input before submitting the form to the PHP script. 在HTML文档中,在将表单提交给PHP脚本之前,应使用Javascript验证用户输入。 Then, in the PHP script, you should double-check that values were not received for both the select and the input (this could happen if the user does not have Javascript enabled, or they somehow bypass the validation) and determine what you want to do in that case (select one over the other, or exit with an error). 然后,在PHP脚本中,您应仔细检查选择和输入是否都没有收到值(如果用户未启用Javascript或以某种方式绕过了验证,则可能会发生这种情况),然后确定要执行的操作在这种情况下,请执行此操作(选择一个,或退出并出现错误)。

Never rely on just JS to validate input. 切勿仅依靠JS来验证输入。 Use JS to implement the behavior you speak of and then handle all possible outcomes in PHP. 使用JS来实现您所说的行为,然后在PHP中处理所有可能的结果。 If someone is fiddling with your JS and bypasses the behavior you implement, you need to be able to handle that in your PHP code as well to make sure DB doesnt get unexpected input and break everything. 如果有人不喜欢您的JS并绕过您实现的行为,则还需要能够在PHP代码中进行处理,以确保DB不会收到意外输入并破坏所有内容。 Just like Justin said above, you could select one or the other, or throw an error. 就像以上Justin所说的那样,您可以选择其中一个,也可以抛出错误。 I recommend throwing an error and telling the user to make sure they have JS enabled. 我建议抛出一个错误,并告诉用户确保已启用JS。

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

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