redirect to php page if fields are empty

瘦欲@ 提交于 2020-01-06 12:41:00

问题


I want to redirect to the page which has a form when user submit the form without any parameters, also I want to return an error message, how can I redirect from controller to the form?

<form action="controllers/Customer.controller.php" method="post">

    <label for="cellPhoneNo">cell phone number</label>
    <input type="text" name="cellPhoneNo" class="textField"/>

    <label for="telephone">telephone number</label>
    <input type="text" name="telephone" class="textField"/>

    <input type="submit" name="searchCustomer" value="بحث"/>
</form>

and here's the Customer.controller.php page

  if(trim($_POST['cellPhoneNo']) == "" && trim($_POST['telephone']) ==""){

  //include('Location:index.php'); //what I supposed to write here?
   echo "empty";
}

回答1:


Not knowing the structure of your framework, you can use php's header

if(trim($_POST['cellPhoneNo']) == "" && trim($_POST['telephone']) ==""){

   $_SESSION['error'] = 'Fields cannot be empty!';
   header('Location: myformlocation.php');
   exit(); 
}

And just above your form:

<?php if(isset($_SESSION['error'] )) : ?>

<div class="error"><?php echo $_SESSION['error'];?></div>

<?php 
unset($_SESSION['error']); 
endif; ?>


<form action="controllers/Customer.controller.php" method="post">

So, whenever the form submits, if fields are empty, the form page is reloaded and, since $_SESSION error is now set, it will be displayed. You might want to make a function out of $_SESSION['error'] displaying, so you won't writing all that code in each form.

EDIT after comment:
Uhm, I'm not really sure to understand your question, you can use either $_GET:

header("Location: ../index.php?page=customerSearch"); 

and you retrieve it in index with $pageToInclude = $_GET['page']; //properly sanitized

or use

$_SESSION['pageToInclude'] = 'CustomerSearch'; 
$_SESSION['error'] = 'Fields cannot be empty!';
header('Location: myformlocation.php');
....

and in index you use

$pageToInclude = isset($_SESSION['pageToInclude']) ? $_SESSION['pageToInclude'] : 'someotherdefaultpage';



回答2:


<?php
session_start();

if(isset($_POST)){
    $cont=true;
    //cellPhoneNo
    if(!isset($_POST['cellPhoneNo']) || strlen($_POST['cellPhoneNo'])< 13){ //13 being the telephone count
        $cont=false;
        $_SESSION['error']['cellPhoneNo']='Cell phone is required & must be 13 in length';
        header('Location: ./index.php');
        die();
    }
    //telephone
    if(!isset($_POST['telephone']) || strlen($_POST['telephone'])< 13){ //13 being the telephone count
        $cont=false;
        $_SESSION['error']['telephone']='Telephone is required & must be 13 in length';
        header('Location: ./index.php');
        die();
    }

    if($cont===true){
        //continue to submit user form

    }else{
        header('Location: ./index.php');
        die();
    }
}else{
    header('Location: ./index.php');
}
?>


来源:https://stackoverflow.com/questions/6971690/redirect-to-php-page-if-fields-are-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!