简体   繁体   中英

PHP - Security for html form dynamically adding rows to CSV

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 you help the code is below.

<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);
 
 ?>

http://php.net/manual/en/filter.examples.validation.php you could use that as a way to validate emails, or you could make a register and login page and have an email sent to theirs for verification. You can simply put required in the email input somewhere and it would make sure it wasn't empty before successfully submitting or you could make an if statement, which I see you're doing above. You'll also need an if statement for the submit button too. I do not see a DB connection being made, nor do I see an insert statement. I suggest doing !empty then making it echo successful submit or something and then an else statement, stating that fields are required. You can take the short way and put required in there though. (: Not sure if it's supposed to be proces.php or not, just pointing that out there... Maybe you misspelled it, or maybe just named it that way. :/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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