简体   繁体   English

在PHP中单击提交时如何重定向到另一页(如果设置了字段)或在同一页面上显示错误(如果字段为空)

[英]How to redirect to another page(if field is set) or show error on same page(if field is empty) on clicking submit in php

I have 4 fields, when page loads I don't want any error to show up. 我有4个字段,在页面加载时,我不想显示任何错误。 But if fields are left empty and submit is clicked then I want to show errors. 但是,如果将字段留空并单击提交,那么我想显示错误。

I want my page to load first. 我希望页面首先加载。 Then when "submit" is clicked, it checks that whether any of the fields are not set, if so then show these errors and prevent the page from getting redirected to other page(Here "slots.php"), and if everything is okay then redirect the page. 然后,当单击“提交”时,它将检查是否未设置任何字段,如果显示,则显示这些错误并阻止页面重定向到其他页面(此处为“ slots.php”),以及一切正常然后重定向页面。 This is the php code: 这是PHP代码:

<?php

      $error = "";

      if($_POST){

          if($_POST['parking_area']!="")
          {$_SESSION['parking_area'] = $_POST['parking_area'];}

          if($_POST['vehicle_no']!="")
          {$_SESSION['vehicle_no'] = $_POST['vehicle_no'];}

          if($_POST['time_from']!="")
          {$_SESSION['time_from'] = $_POST['time_from'];}

          if($_POST['time_to']!="")
          {$_SESSION['time_to'] = $_POST['time_to'];}




          if($_POST['parking_area'] == "select" || $_POST['parking_area'] == "" )
          {$error .= "<p>Please select the parking area</p>";}

          if(!$_POST['vehicle_no'])
          {$error .= "<p>Please enter the vehicle number</p>";}

          if(!$_POST['time_from'])
          {$error .= "<p>Please enter the in time</p>";}

          if(!$_POST['time_to'])
          {$_error .= "<p>Please enter the out time</p>";}

          if($error!=""){
              $error = "<div class='error'>".$error."</div>";
          }
      }

?> ?>

This is the html code: 这是html代码:

<div><?php echo $error;?></div>
              <div class="main-form">

                  <!--<div id="faltu"></div>-->

                  <!--action="slots.php"-->

                  <form method="POST" action="slots.php">

                      <div class="area main-form-components">
                          <select name="parking_area">
                              <option value="select">--Select--</option>
                              <option value="mahu-naka">Mahu Naka Parking</option>
                              <option value="collectorate">Collector Office Parking</option>
                              <option value="treasure-island">Treasure Island</option>
                              <option value="central-mall">Central Mall Parking</option>
                          </select>
                      </div>

                      <div class="time main-form-components">
                          <input type="time" name="time_from" value="09:30"> to 
                          <input type="time" name="time_to" value="17:30">
                      </div>


                      <div class="vnumber main-form-components">
                          <input type="text" name="vehicle_no">
                      </div>

                          <!--
                      <div class="buttons">
                          <div id="prev-button"><i class="fas fa-arrow-left"></i> Prev.</div>
                          <div id="next-button">Next <i class="fas fa-arrow-right"></i></div>
                      </div>
                          -->

                      <div>
                          <input type="submit">
                      </div>

                          <!--<div class="buttons">
                              <button><i class="fas fa-arrow-left"></i> Prev.</button>
                              <button>Next <i class="fas fa-arrow-right"></i></button>
                          </div>-->

                      </form>
                  </div>

Edit: I know how I can do it with javascript, but I am looking for a way to do this without using javascript, only with php if possible. 编辑:我知道我可以用javascript,但我正在寻找一种方法,而不使用javascript,如果可能的话,仅使用php。

This should work. 这应该工作。 I have both my html and php code in the same file as slots.php Once the values are posted we validate the value and if all values are there means we redirect to success page or we show error in same page. 我将我的html和php代码与slot.php放在同一个文件中。一旦将值发布,我们将验证该值,如果所有值都存在,则意味着我们将重定向到成功页面,或者在同一页面中显示错误。

<?php

$error = "";

if($_POST){

    if($_POST['parking_area']!=""){
        $_SESSION['parking_area'] = $_POST['parking_area'];
    }

    if($_POST['vehicle_no']!=""){
        $_SESSION['vehicle_no'] = $_POST['vehicle_no'];
    }

    if($_POST['time_from']!=""){
        $_SESSION['time_from'] = $_POST['time_from'];
    }

    if($_POST['time_to']!=""){
        $_SESSION['time_to'] = $_POST['time_to'];
    }


    if($_POST['parking_area'] == "select" || $_POST['parking_area'] == "" ){
        $error .= "<p>Please select the parking area</p>";
    }

    if(!$_POST['vehicle_no']){
        $error .= "<p>Please enter the vehicle number</p>";
    }

    if(!$_POST['time_from']){
        $error .= "<p>Please enter the in time</p>";
    }

    if(!$_POST['time_to']){
        $_error .= "<p>Please enter the out time</p>";
    }

    if($error!=""){
        $error = "<div class='error'>".$error."</div>";
    }
    else {
        header('Location: success.php');
    }
}
?>

I have added header location redirect if there is no error. 如果没有错误,我已添加标题位置重定向。 Consider success.php is the page where we need to redirect the user if the values are set in the page. 如果在页面中设置了值,请考虑success.php是我们需要重定向用户的页面。

<div>
<?php echo $error;?></div>
<div class="main-form">
    <form method="POST" action="slots.php">

        <div class="area main-form-components">
            <select name="parking_area">
                <option value="select">--Select--</option>
                <option value="mahu-naka">Mahu Naka Parking</option>
                <option value="collectorate">Collector Office Parking</option>
                <option value="treasure-island">Treasure Island</option>
                <option value="central-mall">Central Mall Parking</option>
            </select>
        </div>

        <div class="time main-form-components">
            <input type="time" name="time_from" value="09:30"> to 
            <input type="time" name="time_to" value="17:30">
        </div>


        <div class="vnumber main-form-components">
            <input type="text" name="vehicle_no">
        </div>


        <div>
            <input type="submit">
        </div>
    </form>
</div>

Should define ID or NAME tag for each input and then : 应该为每个输入定义ID或NAME标签,然后:

In this example for two textarea(a,b): 在此示例中,两个textarea(a,b):

<script type="text/javascript">
    function func()
    {
        var a=document.forms["Form"]["answer_a"].value;
        var b=document.forms["Form"]["answer_b"].value;
        if (a==null || a=="",b==null || b=="")
        {
            alert("Please Fill All Required Field");
            return false;
        }
    }
</script>

<form method="post" name="Form" onsubmit="return func()" action="">
     <textarea cols="30" rows="2" name="answer_a" id="a"></textarea>
     <textarea cols="30" rows="2" name="answer_b" id="b"></textarea>
     <input type="submit">

</form>

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

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