Jump to content

required fields stops the user, but the form is still submitting


tangosukka

Recommended Posts

I was able to make my fields required and the user gets a message and has to go back and enter all of the required fields before they see a successful submission, but I still get an email every time they get the error message.  Below is the code, thanks so much for your help.

 

(fyi, i got all of the code from the web and changed to fit my needs, so I'm not entirely sure what it all does, if i had to guess, i would think the problem is in "$sent = mail($to, $subject, $body, $headers);", but that's just a guess)

 

<?php

$to = "email@mydomain.com";
$email = $_REQUEST['email'] ;
$fname = $_REQUEST['fname'] ;
$lname = $_REQUEST['lname'] ;
$phone = $_REQUEST['phone'] ;
$type = $_REQUEST['type'] ;
$details = $_REQUEST['details'] ;
$subject = "Message from: $fname $lname";
$headers = "noreply@mydomain.com";
$body = "FirstName: $fname \n\n Lastname: $lname \n\n PhoneNumber: $phone \n\n Email: $email \n\n Type: $type \n\n Details: $details \n\n";
$sent = mail($to, $subject, $body, $headers) ;

if ($_POST['fname']=="") 
{
Print("Ooops, please use your back button and provide your first name!<br>");
}
elseif ($_POST['lname']=="")
{
Print("Ooops, please use your back button and provide your last name!<br>");
}
elseif ($_POST['phone']=="")
{
Print("Ooops, please use your back button and provide your phone number!<br>");
}
elseif ($_POST['email']=="")
{
Print("Ooops, please use your back button and provide your email!<br>");
}
elseif($sent)
{echo "<script language=javascript>window.location = 'thanks.php';</script>";}
else
{echo "<script language=javascript>window.location = 'error.php';</script>";}

?>

Link to comment
Share on other sites

Use $_POST instead of $_REQUEST. And also don't mixed them up  ::)

 

NOw on topic ; )

 

Your functionality file is first of all sending an email and than checks stuff. Read form top to bottom in that order it will be executed. So place conditions above the mail() function. And if someone didn't do as you like exit; the script. Or place the mail within a function that checks if all the fields you want are as you expect them to be like:

 

<?php
if(!isset($_POST['submit'])){
   if ($_POST['fatmonkeys']=='')
      die ('noooOoOO!!');
}else{

//Your mail script inside here
}
  ?>

 

 

Also for all those error messages switch() might be nice and/or an array

- edit:  and yes you were right about the problem in the mail() but that's also your solution

Link to comment
Share on other sites

Ideally, you'd want to validate the entire form and capture the errors in an array for display to the user, along with redisplay of the form already populated with the values that were previously submitted ("sticky" form). Using if/elseif/else or switch only validates as far as the first error, and should only be used in instances where each for field con only be validated based on the previous field being correct (and that's rare).

 

Using exit() or die() for form validation is a rather poor way of handling it. It's like slamming the door in the user's face for making a mistake.

 

Making them use the browser's back button isn't a great idea either. The more they have to do to fill in that form, the less likely they are to actually complete it. The easier you can make it for the user, the higher the likelihood they'll stick around.

 

Quick example with a 2 field form that submits to itself, validates the input, redisplays with errors highlighted, and can write to a DB table, or whatever you choose, when no errors are encountered:

<?php
if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { // main conditional checks to see if form has been submitted by testing for value of hidden field 'submitted'

array_map(trim, $_POST); // send all $_POST array elements through the trim() function at one time.

$errors = array(); // initialize an array to store errors.

// Validate the name field		
if( !empty($_POST['name']) ) {
	if( !ctype_alpha($_POST['name']) ) { // if $_POST['name'] is not alpha characters
		$errors[] = '<font color="red">Name may only contain letters.</font>';
	}
} else {
	$errors[] = '<font color="red">Name is a required field</font>'; // if $_POST['name'] is empty . . .
}

// Validate the pin field		
if( !empty($_POST['pin']) ) {
	if( !ctype_alnum($_POST['pin']) ) { // if $_POST['pin'] is not alphanumeric characters
		$errors[] = '<font color="red">PIN may only contain alphanumeric characters.</font>';
	}
	if( strlen($_POST['pin']) > 20 || strlen($_POST['pin']) < 5 ) { // if $_POST['pin'] is more than 20 or less than 5 chars.
		$errors[] = '<font color="red">PIN must be 5 - 20 characters in length.</font>';
	}
} else {
	$errors[] = '<font color="red">PIN is a required field.</font>'; // if $_POST['pin'] is empty . . .
}

// Check for validation errors in $errors array	
if( empty($errors) ) { // if there are no validation errors.
	echo '<font color="green"><b>No errors.</b></font>'; // or run DB insert, etc.
} else { // if there are validation errors, list them and append a line break, unless it's the last error in the list.
	$i = 1;
	$num = count($errors);
	foreach( $errors as $val ) {
		echo $val;
		echo $i < $num ? '<br />' : '';
	}
}
}
?>
<form action="" method="post">
<table>

<tr>
<td>First Name:</td>
<td><input type="text" name="name" value="<?php echo empty($_POST['name']) ? '' : $_POST['name']; // if $_POST['name'] has a value, echo it.?>" /></td>
</tr>
<tr>
<td>PIN (5 - 20 alphanumeric characters):</td>
<td><input type="text" name="pin" value="<?php echo empty($_POST['pin']) ? '' : $_POST['pin']; // if $_POST['pin'] has a value, echo it.?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
<input type="hidden" name="submitted" value="yes" />
</form>

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.