【问题标题】:redirect to php page if fields are empty如果字段为空,则重定向到 php 页面
【发布时间】:2011-08-07 08:48:27
【问题描述】:

当用户提交没有任何参数的表单时,我想重定向到有表单的页面,同时我想返回错误消息,如何从控制器重定向到表单?

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

这是 Customer.controller.php 页面

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

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

【问题讨论】:

    标签: php forms validation redirect


    【解决方案1】:
    <?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');
    }
    ?>
    

    【讨论】:

    • 我不会检查电话号码中的 is_int,通常是 +39 02.. 或 347/123456 或任何可接受的电话号码提供值。电话号码不是整数
    • @Lawrence Cherone,这是什么警告“警告:无法修改标头信息 - 标头已由(输出开始于 C:\AppServ\www\crm\controllers\Customer.controller.php:5 ) 在 C:\AppServ\www\crm\controllers\Customer.controller.php 第 11 行"
    • 这意味着第 5 行的输出已经开始,考虑到 session_start() 在第 2 行,您必须在 &lt;?php 上方有空格
    【解决方案2】:

    不知道你的框架结构,可以用php的header

    if(trim($_POST['cellPhoneNo']) == "" && trim($_POST['telephone']) ==""){
    
       $_SESSION['error'] = 'Fields cannot be empty!';
       header('Location: myformlocation.php');
       exit(); 
    }
    

    就在您的表单上方:

    <?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">
    

    因此,每当提交表单时,如果字段为空,则重新加载表单页面,并且由于现在设置了 $_SESSION 错误,它将被显示。您可能希望通过显示 $_SESSION['error'] 来创建一个函数,因此您不会在每个表单中编写所有代码。

    评论后编辑:
    嗯,我不太清楚你的问题,你可以使用 $_GET:

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

    然后你在索引中检索它 $pageToInclude = $_GET['page']; //适当消毒

    或使用

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

    在你使用的索引中

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

    【讨论】:

    • 由于 index.php 位于控制器文件夹之外,我怎样才能在文件夹中后退一步?
    • @Alaa ../index.php 应该可以解决问题。您可以提供完整路径,http://mysite.com/index.php
    • @Damien Pirsy,如何将参数传递给 index.php,因为我是这样 include("subpages/$pageToInclude.sub.php");在索引中,我将 $pageToInclude="CustomerSearch" 设为 header('Location: ../index.php');但 id 不起作用!
    • 这是什么警告“警告:无法修改标头信息 - 标头已由(输出开始于 C:\AppServ\www\crm\controllers\Customer.controller.php:5)在 C: \AppServ\www\crm\controllers\Customer.controller.php 在第 11 行“
    • @Alaa 该警告是因为您在发送标头之前输出了一些内容;它可以是回声,空间,等等。在第 11 行查看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2016-04-04
    • 1970-01-01
    • 2017-06-25
    • 2014-06-10
    • 2012-10-05
    • 2011-07-23
    相关资源
    最近更新 更多