【发布时间】:2017-04-28 05:36:02
【问题描述】:
我已经阅读了许多类似的问题,所以我知道这已经得到了回答,但现在我的问题是为什么我正在做的事情不起作用?
我是 Web 开发的新手,正在开发一个将提交的数据传递到 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
//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>
感谢所有提示!
【问题讨论】:
-
为什么这么长? D:
-
@JustCarty 你会怎么做?
-
这里的代码太多了。找到相关部分并发布。
-
我没有阅读你所有的代码,但是这一行中的 { } 是不必要的
header('Location: submittedApplication.php?result={$result}');。应该是header('Location: submittedApplication.php?result=' . $result . '); -
@Qirel 我已经提取了之前大约 80% 的代码,我只留下了表单的基本外壳,只留下了几个声明,但保留了我随机的部分ID 生成器和提交所有内容的验证(如果正确)。