kiki Posted August 25, 2008 Share Posted August 25, 2008 I've been looking around for a simple command for php for required fields from my form before it is sent to the email? im using an javascript right now but it gives the reminder to fill in the required fields but still sends me email anyways. i was wondering if there is a simple command that i can use with php to make sure fields are filled in BEFORE its sent to me by email? like if the name="required" then what command would i use in php to say that it must be filled before emailing. it doesnt have to validate if its correct or anything i dont mind if its spam or something i just need to make sure things are filled up. Link to comment https://forums.phpfreaks.com/topic/121315-required-fields/ Share on other sites More sharing options...
BlueSkyIS Posted August 25, 2008 Share Posted August 25, 2008 what i do: $errors = array(); $some_field_value = trim($_POST['some_field']); if ($some_field_value == "") { $errors[] = "Please enter a value in Some Field"; } if (count($errors) == 0) { // there are no errors, so send the email } else { // there is at least one error. don't send the email, but show each error. } Link to comment https://forums.phpfreaks.com/topic/121315-required-fields/#findComment-625473 Share on other sites More sharing options...
Ken2k7 Posted August 25, 2008 Share Posted August 25, 2008 <?php $field_names = array( "name", "number", "address", ); $errors = array(); foreach ($field_names as $f) { if (empty($_POST[$f])) { array_push($errors, $f); } } $err_len = count($errors); $err_msg = ""; while (--$err_len > -1) { $err_msg = $err_msg . "<br />Field " . $errors[$err_len] . " is empty."; } if ($err_msg == "") { echo $err_msg; exit; } ?> Link to comment https://forums.phpfreaks.com/topic/121315-required-fields/#findComment-625493 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.