Jump to content

form validation


dave_55

Recommended Posts

Hi can anyone help me please. I have a email form created that works but i need some form validation.  I need the user to make sure they have all the fields filled in.  Can anyone help please. i dont know where to start.

 

My code is for mailer is:

<?php 
if(isset($_POST['submit'])) { 
$to = "[email protected]"; 
$subject = "Player Availibilty"; 
$name_field = $_POST['name']; 
$position_field = $_POST[ 'position'];
$email_field = $_POST['email']; 
$comments = $_POST['comments']; 
  
foreach($_POST['check'] as $value) { 
$check_msg .= "Checked: $value\n"; 
}

$body = "From: $name_field\n Position: $position_field\n E-Mail: $email_field\n $check_msg\n Comments:\n $comments"; 
  	
echo "Data has been submitted to $to!"; 
mail($to, $subject, $body); 
} else { 
echo "error: no data sent!"; 
} 
?> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/102329-form-validation/
Share on other sites

Hi can anyone help me please. I have a email form created that works but i need some form validation.  I need the user to make sure they have all the fields filled in.  Can anyone help please. i dont know where to start.

 

My code is for mailer is:

<?php 
if(isset($_POST['submit'])) { 
$to = "[email protected]"; 
$subject = "Player Availibilty"; 
$name_field = $_POST['name']; 
$position_field = $_POST[ 'position'];
$email_field = $_POST['email']; 
$comments = $_POST['comments']; 
  
foreach($_POST['check'] as $value) { 
$check_msg .= "Checked: $value\n"; 
}

$body = "From: $name_field\n Position: $position_field\n E-Mail: $email_field\n $check_msg\n Comments:\n $comments"; 
  	
echo "Data has been submitted to $to!"; 
mail($to, $subject, $body); 
} else { 
echo "error: no data sent!"; 
} 
?> 

 

 

 

Okay.  FIRSTLY:

mail($to, $subject, $body);

} else {

echo "error: no data sent!";

}

 

There's no IF, just an else.  Maybe you wanted:

if (mail($to, $subject, $body)) {

} else {

echo "error: no data sent!";

}

 

Secondly: There's no way to actually see if mail went through.  You can see the success of the function, but not if it actually got through. =(

 

Thirdly (since I'm busy, I'm doing this the lazy way.  Don't hate me. =( ):

       

$allowed = array("name", "position", "email", "comments");
if (!is_array(array_diff($allowed, array_keys($_POST))) {
$name_field = $_POST['name']; 
$position_field = $_POST[ 'position'];
$email_field = $_POST['email']; 
$comments = $_POST['comments']; 
        }
else {
        $not_filled = array_diff($allowed, array_keys($_POST));
        echo "You need to enter: <br />"
        foreach ($not_filled as $v) {
        $new_v = ucfirst($v);
        echo $new_v . "<br />";
        }
}

             

In retrospect, that was rather clever. o-o

           

Link to comment
https://forums.phpfreaks.com/topic/102329-form-validation/#findComment-523956
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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