简体   繁体   English

PHP将数据保存到.csv,然后重定向

[英]Php save data to .csv and then redirect

Like the title says, I am trying to make a php script that saves the data to a csv then redirects them to another url on my site. 就像标题所说的那样,我正在尝试制作一个php脚本,将数据保存到csv,然后将它们重定向到我的网站上的另一个URL。 All that is happening is when I hit the submit button I get an error on saying it cannot handle the request. 发生的一切是,当我按下“提交”按钮时,我收到一条错误消息,说它无法处理请求。

This is my form 这是我的表格

<form action="submit.php" method="post">

    <h1 style="text-align: center;">Register</h1><br>

    <fieldset>
     <legend>Your registration info</legend>
     <label for="name">Name:</label>
     <input type="text" id="name" name="user_name" required="">

     <label for="mail">Email:</label>
     <input type="email" id="mail" name="user_email" required="">

     <label for="phone">Phone:</label>
     <input type="phone" id="phone" name="phone">

     <label for="guest">Guest or Spouse:</label>
     <input type="text" id="gsname" name="gsname">

     <label>Date Attending:</label>
     <input type="radio" id="tuesday" value="tuesday" name="date"><label 
for="tuesday" class="light">Tuesday, May #, 2018 10 AM - 11:30 AM</label> 
<br>
     <input type="radio" id="thursday" value="thursday" name="date"><label 
for="thursday" class="light">Thursday, May #, 2018 10 AM - 11:30 AM</label>
    </fieldset>


     <button type="submit" name="submit">Register Now</button>
    </form>

This is my php script. 这是我的PHP脚本。

if (isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$name = $_POST['user_name'];
$email = $_POST['user_email'];
$phone = $_POST['phone'];
$gsname = $_POST['gsname'];
$date = $POST['date'];
$open = fopen("formdata.csv", "a");
$csvData = $name. "," $email. "," $phone. "," $gsname. "," $date. "n";
fwrite($open,$csvData)
fclose($open)
#if($name !=''&& $email !=''&& $phone !=''&& $gsname
//  To redirect form on a particular page
header("Location:https://www.google.com/");
}
else (
header("Location:https://www.charter.com");
)

?>

Can anyone give me an idea where I am messing up? 谁能给我个主意呢? I know data validation is important, but if I am using it on the form itself, do I need to validate it here? 我知道数据验证很重要,但是如果我在表单本身上使用它,是否需要在此处进行验证?

A few things that I would try: 我会尝试的几件事:

  • Fix the typo $date = $_POST['date']; 修正错字$date = $_POST['date'];
  • Fix the string concatenation $name . "," . $email . "," . $phone . "," . $gsname . "," . $date . "\\n"; 修复字符串串联$name . "," . $email . "," . $phone . "," . $gsname . "," . $date . "\\n"; $name . "," . $email . "," . $phone . "," . $gsname . "," . $date . "\\n";
  • Check to make sure your script is receiving the POST request. 检查以确保您的脚本正在接收POST请求。 If not double check the path on the form action attribute. 如果不是,请仔细检查表单操作属性上的路径。
  • Double check the directory in which you are attempting to create/write formdata.csv is writable. 仔细检查您试图在其中创建/写入formdata.csv的目录是否可写。 I would suggest using fputcsv() as well. 我建议也使用fputcsv()

Although it's not mandatory, it's highly recommended that you do server-side validation. 尽管不是强制性的,但强烈建议您进行服务器端验证。 Never assume the data you are receiving is the way you want it. 永远不要以为要接收的数据就是您想要的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM