Jump to content

PHP - Security for html form dynamically adding rows to CSV


mikecaba

Recommended Posts

Hey guys I need help in adding security such as form validation to my PHP form which writes a new row on a csv as users submit their email. I'm a complete novice on PHP and require a bit of help. I need to...

  1. Making sure the Email field is not empty and is an actual valid address.
  2. Making sure when the submit button is pressed that the field is not empty and is a valid email address.

I do want to add user feedback such as when the form is submitted and is valid the user gets a message saying 'Your email address has been added.' However I would like this to be within the same page so it could be underneath the button.

How do I get around doing this? I would appreciate all your help!

 

<form action='proces.php' method='post'>

<p><label>Email</label><br><input type='text' name='email' value=''></p>
<p><input type='submit' name='submit' value='Submit'></p>
</form>


<?php


//read data from form
$lName = filter_input(INPUT_POST, "name");
$fName = filter_input(INPUT_POST, "email");
$email = filter_input(INPUT_POST, "email");
$phone = filter_input(INPUT_POST, "phone");

$output = $fName . "\t";
$output .= $lName . "\t";
$output .= $email . "\t";
$output .= $phone . "\n";

if(empty($fname) || empty($ln) || empty($phone)){//show the form
$message = 'Fill in areas in red!';
$aClass = 'errorClass';
}
//open file for output
$fp = fopen("contacts.csv", "a");
//write to the file

fwrite($fp, $output);
fclose($fp);

?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.