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

时间:2017-04-27 16:15:02

标签: php html css post

我已经阅读了很多类似的问题,所以我知道之前已经回答了这个问题,但现在我的问题是为什么我没有做什么工作?

我是网络开发的新手,开发了一个将提交的数据传递给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>

这是我的第二页提交申请.php

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

            <h4>NOTES:</h4>
                <hr>

        <div align="center"><h3>Application Submitted Successfully!</h3> </div>

        <?php
                echo $_GET['result'];
        ?>
        </body>
    </html>

感谢所有提示!

1 个答案:

答案 0 :(得分:0)

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

您可以在form.php页面

中执行此操作

session_start(); $SESSION_["result"] = $result;

然后在其他页面中执行以下操作

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

确保在完成任何会话变量时销毁或取消设置。