使用PHP在表单提交后将数据传递到另一个页面

使用PHP在表单提交后将数据传递到另一个页面,php,html,css,post,Php,Html,Css,Post,我已经读过很多类似的问题,所以我知道这个问题以前已经得到了回答,但现在我的问题是为什么我所做的不起作用 我是web开发的新手,正在开发一个web表单,将提交的数据传递到CSV文件。我现在做的是,在表单页面“form.php”上完成所有表单验证之后,它将用户发送到另一个页面“submittedApplication.php”,在同一个语句中,我的所有代码都将数据推送到CSV中 我需要的是将一个特定变量从“form.php”传递到“submittedApplication.php”。这是一个参考号,

我已经读过很多类似的问题,所以我知道这个问题以前已经得到了回答,但现在我的问题是为什么我所做的不起作用

我是web开发的新手,正在开发一个web表单,将提交的数据传递到CSV文件。我现在做的是,在表单页面“form.php”上完成所有表单验证之后,它将用户发送到另一个页面“submittedApplication.php”,在同一个语句中,我的所有代码都将数据推送到CSV中

我需要的是将一个特定变量从“form.php”传递到“submittedApplication.php”。这是一个参考号,我有一个随机生成器,在form.php上

在我的代码中,我使用一个函数来创建参考号,并将其存储在名为$result的变量中。在我使用的验证的底部

header('Location: submittedApplication.php?result={$result}');
试着把它传过去,然后在第二页我用

echo $_GET['result'];
尝试获取变量

如果你在我的代码中发现了它,我也尝试了隐藏输入法,但也没有用

这是我的form.php主页

    <!DOCTYPE html>
    <html>

    <?php
    //Define variables and set to empty values

    //###CUSTOMER DATA###
    //Name
    $custName= "";
    $custNameError = "";

    //Reference Number

    $result = gen_uid();

    //Error holders
    $errors = ""; //Generic Error list at top of form, can be appended to
    $error = 0;   //Error Tally, If 0 = good. If 1 = error.


    //Generates a 10 character random string with a prefix and current date attached 
    function gen_uid($l=10){ 
        $prefix = "BLANK DATA#";
        $str = ""; 
        date_default_timezone_set('America/New_York');
        $date = date("Y.m.d");

        for ($x=0;$x<$l;$x++)
        $str .= substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1);
        echo $prefix . $str . "<br/>" . "Generated On: " . $date; }

    //for testing
    echo $result;


    if($_SERVER["REQUEST_METHOD"] == "GET"){
        $custName = "";
        $custAddress = "";
    }
    else if ($_SERVER["REQUEST_METHOD"] == "POST") { // Checking null values in message.

     $custName = $_POST['customername'];
     $custAddress = $_POST['CustomerMailingAddress'];

     $passedResult = $_POST['result'];


    //################################## Form Validation #####################################
        //CUSTOMER NAME
        if(!isset($custName) || $custName == "")
        {

            $custNameError = "Name required";
            $errors .= "Customer contact information required, Contractor optional.<br/>";
            $custName = "";
            $error = 1;
        }
        else{
            $custName = $_POST['customername'];
        }

        if($error == 0) 
        {
             echo "<input type='hidden' name='result' value='{$result}'/>";

             //this is where the creating of the csv takes place
             $cvsData = $custName . "," . $custAddress . "," . $custPhone . "," . $custMobile . "," . $custFax . "," . $custEmail . "," . $conName . "," . $conAddress . "," .
             $custPhone . "," . $conPhone . "," . $custMobile . "," . $conMobile . "," . $custEmail . "," . $conEmail . "," . $accNum ."\n";

             $fp = fopen("formTest.csv","a"); // $fp is now the file pointer to file $filename

             if($fp){
             fwrite($fp,$cvsData); // Write information to the file
             fclose($fp); // Close the file
             }
        header('Location: submittedApplication.php?result={$result}');
        }
    }
    ?>

        <body>
            <h2 align="center"><u>Service Request Application Form</u></h2>
            <hr>

            <h4>NOTES:</h4>

            <div id="wrapper">
    <br/>
    <h3 class="error"><?php echo $errors; ?></h3>
            <form method="post" align="center" name="applicationform" id="applicationform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">

                <!--###################################### CONTACT INFORMATION FIELDSET ######################################-->
                <fieldset  style="border: 1px black solid" align="center">
                    <legend style="font-weight:bold"><u>Contact Information</u></legend>
                    <table>
                    <tr>
                    <th></th>
                    <th><u>Customer</u></th>
                    <th title="Electrician"><u>Consultant/Contractor</u></th>
                    </tr>
                    <tr>
                        <td>
                            <tr>
                                <td align="right" id="namelabel">Contact Name:</td>
                                <td><input type="text" id="customername" name="customername" value="<?php echo $custName;?>" title="Name of contact on account"/></td>
                                <td><input type="text" id="contractorname" name="contractorname" title="Name of contractor or consultant" /></td>
                            </tr>

                            <tr>
                            <td></td>
                            <td><div class="error"><?php echo $custNameError;?></div></td>
                            <td></td>
                            </tr>




                        </td>

                    </tr>
                    </table>            
                </table>
            </form>
            </div>
        </body>
    </html>


我通常处理在页面之间传递值的方法是使用会话变量

您可以在form.php页面中执行此操作

session_start();
$SESSION_u[“result”]=$result;

然后在另一页中执行以下操作

session_start();
if(isset($SESSION_["result"]) {
    $result = $SESSION_["result"];
}

只需确保在处理完会话变量后销毁或取消设置它们。

为什么这么长?D:@JustCarty你会怎么做呢?这里的代码太多了。找到相关部分并发布。我没有阅读您的所有代码,但此行中的{}不必要
标题('Location:submittedApplication.php?result={$result}')。它应该是
header('Location:submittedApplication.php?result='.$result')
@Qirel我已经提取了之前存在的大约80%的代码,我只保留了表单的基本外壳,只有几个声明,但保留了随机ID生成器的部分以及提交它的验证(如果正确的话)。
session_start();
if(isset($SESSION_["result"]) {
    $result = $SESSION_["result"];
}