dsc862009 2013-11-05 00:23
浏览 53
已采纳

可以在php脚本中操作$ _POST变量并在另一个php脚本中表达它吗?

I've been trying to do form validation without using the url. So I thought that I would create a hidden field in my form and send it over to my validation php script. What I was hoping I would be able to do is set what ever errors there are in the form to this hidden field and return it. However once I get out of the scope it destroys whatever I set. I thought $_POST had global scope? Maybe I declared I set the hidden field wrong? I have placed the code below.

<?php
    include_once $_SERVER['DOCUMENT_ROOT'].'/poles/config/databaseConnect.php';
    include_once $_SERVER['DOCUMENT_ROOT'].'/poles/config/functions.php';
    include_once $_SERVER['DOCUMENT_ROOT'].'/poles/models/users.php';
    include_once $_SERVER['DOCUMENT_ROOT'].'/poles/models/userDetails.php';

    //get the refering url to be used to redirect
    $refUrl = $_SERVER['HTTP_REFERER'];

    if(isset($_POST['register'])){
        //declare a temp error array
        $tempError;

        //check if the form is empty
        if(empty($_POST['Email'])&&empty($_POST['Email Confirmation'])&&empty($_POST['Password'])&&empty($_POST['Password Confirmation'])
        &&empty($_POST['Stage Name'])&&empty($_POST['Main Club'])){
            $tempError = 'Please fill in the form.';
        }else{
            //set variables
        }

        if(!empty($tempError)){
            //start a session to declare session errors
            $_POST['errors'] = $tempError;
            //redirect back to referring url
            header('Location:'.$refUrl);
            exit();
        }else{
            //log user in and redirect to member home page
        }

    }

Basic form (I excluded the input field as it would be really long)

 <div class="col-md-6 well">
          <span class="jsError"></span><?php if(isset($_POST['errors'])){ $errors = $_POST['errors']; } if(!empty($errors)){ echo '<p class="alert alert-danger text-center">'.$errors.'</p>'; } ?>
          <form class="form-horizontal" role="form" method="post" action="controllers/registrationController.php" id="registration">
            <input type="hidden" name="errors" value="<?php if(isset($_POST['errors'])){echo $_POST['errors']; } ?>">
            </form>

I looked into using the $_SESSION variable method too but the stuff I found was either a bit complicated or it involved me starting a whole bunch of sessions everywhere (would make my code messy in my opinion).

  • 写回答

2条回答 默认 最新

  • douge7771 2013-11-05 00:36
    关注

    $_POST is populated from the contents of the data passed by the browser to the server. When you send a Location header it causes the browser to load a new page, but since it will have no form data, nothing will be passed.

    If you need to pass data from page to page then $_SESSION is the way to go. All that is required is a session_start() at the top of the pages that need access, and you can store your $_POST data like this:

    $_SESSION['postdata'] = $_POST;
    

    Retrieving it becomes

    $email = $_SESSION['post']['Email'];
    

    The alternative is to echo the data as a hidden <input> in a new form, but that will require a new form to be submitted and I get the feeling you want something seamless.

    Note also that $_SERVER['HTTP_REFERER'] is not guaranteed to be accurate, or even present. You shouldn't rely on this for production code. It might work for you with your browser in your test set-up, but that's no guarantee it'll work for other browsers. Find another way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!