表单提交后,将数据传递到另一个页面 [英] Passing data to another page after form submit with PHP

查看:216
本文介绍了表单提交后,将数据传递到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我是网络开发新手,开发一个将提交的数据传递给CSV文件的Web表单。我目前所做的,是在表单页面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
//定义变量并设置为空值

// ### CUSTOMER DATA ###
//名称
$ custName =;
$ custNameError =;

//参考编号

$结果= gen_uid();

//错误持有者
$ errors =; //在表单顶部的通用错误列表可以追加到
$ error = 0; //错误提示,如果0 =好。如果1 =错误。


//产生一个带有前缀和当前日期的10个字符的随机字符串
函数gen_uid($ l = 10){
$ prefix =BLANK DATA #;
$ str =;
date_default_timezone_set('America / New_York');
$ date = date(Y.m.d); ($ x = 0; $ x <$ l; $ x ++)
$ str。= substr(str_shuffle(adding),0,1);


echo $前缀。 $ str。 <峰; br /> 中。 生成于:。 $日期; }

//用于测试
echo $ result;


if($ _ SERVER [REQUEST_METHOD] ==GET){
$ custName =;
$ custAddress =;
}
else if($ _SERVER [REQUEST_METHOD] ==POST){//检查消息中的空值。

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

$ passedResult = $ _POST ['result'];


// ##################################表单验证#####################################
// CUSTOMER NAME
if (!isset($ custName)|| $ custName ==)
{

$ custNameError =需要的名字;
$ errors。=需要客户联系信息,承包商可选。< br />;
$ custName =;
$ error = 1;
}
else {
$ custName = $ _POST ['customername'];
}

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

//这是创建csv的地方
$ cvsData = $ custName。 ,。 $ custAddress。 ,。 $ custPhone。 ,。 $ custMobile。 ,。 $ custFax。 ,。 $ custEmail。 ,。 $ conName。 ,。 $ conAddress。 ,。
$ custPhone。 ,。 $ conPhone。 ,。 $ custMobile。 ,。 $ conMobile。 ,。 $ custEmail。 ,。 $ conEmail。 ,。 $ accNum。\\\
;

$ fp = fopen(formTest.csv,a); // $ fp现在是指向文件$ filename

的文件指针if($ fp){
fwrite($ fp,$ cvsData); //将信息写入文件
fclose($ fp); //关闭文件
}
header('Location:submittedApplication.php?result = {$ result}');
}
}
?>

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

< h4>注意:< / h4>

< div id =wrapper>
< br />
< h3 class =error><?php echo $ errors; ?>< / H3>
< form method =postalign =centername =applicationformid =applicationformaction =<?php echo htmlspecialchars($ _ SERVER [PHP_SELF]);?> ENCTYPE = 多部分/格式数据 >

<! - - ##################################### #联系信息FIELDSET ###################################### - >
< fieldset style =border:1px black solidalign =center>
< legend style =font-weight:bold>< u>联络资讯< / u>< / legend>
< table>
< tr>
< th>< / th>
< th>< u>客户< / u>< / th>
< th title =电工>< u>< u>顾问/承包商< / u>< / th>
< / tr>
< tr>
< td>
< tr>
< td align =rightid =namelabel>联络人名称:< / td>
< td>< input type =textid =customernamename =customernamevalue =<?php echo $ custName;?> title =账户联系人姓名/>< / td>
< td>< input type =textid =contractornamename =contractornametitle =承包商或顾问的名称/>< / 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>< u> Service Request Application Form< / u>< / h2>
< hr>

< h4>注意:< / h4>
< hr>

< div align =center>< h3>申请成功提交!< / h3> < / DIV>

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

欢迎您提供任何提示!

解决方案

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



您可以在你的form.php页面做到这一点



session_start();
$ SESSION _ [result] = $ result;



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

 在session_start(); 
if(isset($ SESSION _ [result]){
$ result = $ SESSION _ [result];
}
pre>

只要确保在完成任务时摧毁或取消设置任何会话变量即可。


I've read through a number of similar questions, so I know this has been answered before, but now my question is why isn't what I'm doing working?

I'm new to web development, developing a web form that passes the submitted data to a CSV file. What I currently do, is after all form validation is done on the form page "form.php", it sends the user to another page "submittedApplication.php", and in the same statement goes all of my code to push the data into a CSV.

What I NEED, is to pass one particular variable from "form.php", over to "submittedApplication.php". It's a reference number that I have a random generator for, on form.php.

In my code I use a function to create the reference number, I store it in a variable called $result. In the bottom of the validation I use

header('Location: submittedApplication.php?result={$result}');

to try and pass it over, and then in the second page I use

echo $_GET['result'];

to try and grab the variable.

If you spot it in my code, I've also tried the hidden input method, to no avail as well.

Here is my form.php main page

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

And here is my second page submittedApplication.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>

Any and all tips are appreciated!

解决方案

The way I would normally tackle passing values between pages is to use session variables.

You can do this in your form.php page

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

Then do the following in your other page

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

Just make sure you destroy or unset any session variables when you're done with them.

这篇关于表单提交后,将数据传递到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆