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

问题描述 投票:0回答:2

我有4个字段,当页面加载时我不希望出现任何错误。但是如果字段留空并单击提交,那么我想显示错误。

我希望我的页面首先加载。然后当点击“提交”时,它会检查是否有任何字段未设置,如果是,则显示这些错误并阻止页面重定向到其他页面(此处为“slots.php”),如果一切正常然后重定向页面。这是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>";
          }
      }

?>

这是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>

编辑:我知道我怎么能用javascript做到这一点,但我正在寻找一种方法来做到这一点,而不使用JavaScript,如果可能只有PHP。

php html5
2个回答
1
投票

这应该工作。我将html和php代码放在与slots.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');
    }
}
?>

如果没有错误,我添加了标题位置重定向。考虑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>


0
投票

应为每个输入定义ID或NAME标记,然后:

在这个例子中有两个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>
© www.soinside.com 2019 - 2024. All rights reserved.