0

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

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?

1
  • You have several syntax errors (string concatenation ., instructions to close ;). Also, you should use fputcsv() to write CSV instead of creating it manually. Commented Apr 3, 2018 at 19:57

1 Answer 1

1

A few things that I would try:

  • Fix the typo $date = $_POST['date'];
  • Fix the string concatenation $name . "," . $email . "," . $phone . "," . $gsname . "," . $date . "\n";
  • Check to make sure your script is receiving the POST request. 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. I would suggest using fputcsv() as well.

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I’m not familiar with coding in php and barely code anyway. How can I fix the concatenation and how do I use fputcsv()?
@Kamikaze_goldfish String concatenation in PHP can be accomplished using the period . character which must be used after a string or variable that you are appending to. As for using fputcsv(), it takes two required parameters. The first being the file pointer, in this case $open, and the second being an array of values, in this case array($name, $email, $phone, $gsname, $date). Hope this helps! :)
I've added the following, is this more inline with that I need to do? '$open = fopen("formdata.csv", "a"); $csvData = $name . "," $email . "," $phone . "," $gsname . "," $date . "\n"; fputcsv($open, array($name, $email, $phone, $gsname, $date)); fclose($open);'
@Kamikaze_goldfish Yes! You're on the right path. Because you are now using fputcsv() you won't need the $csvData variable anymore since it does the delimiters for you.
You rock. It all works and I thank you for helping me set this up!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.