gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:52:20 25 4
gpt4 key购买 nike

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

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

这是我提交的第二页Application.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>

感谢任何和所有提示!

最佳答案

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

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

session_start();
$SESSION_["结果"] = $结果;

然后在你的其他页面做以下操作

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

只需确保在使用完所有 session 变量后销毁或取消设置即可。

关于php - 使用PHP提交表单后将数据传递到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43662986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com