Log in

View Full Version : Required Form Field



Ryan Fitton
04-21-2009, 09:52 PM
Hi, i amwanting to know ow i can make these three form fileds required. i want 'YourName', 'YourEmail' and 'YourMessage' to be required.

Below is contact_script.php file

<?php

// get posted data into local variables
$EmailFrom = "ryan.fitton@hotmail.co.uk";
$EmailTo = "ryan.fitton@hotmail.co.uk";
$Subject = "Email From Personal Website";
$YourName = Trim(stripslashes($_POST['YourName']));
$YourEmail = Trim(stripslashes($_POST['YourEmail']));
$YourMessage = Trim(stripslashes($_POST['YourMessage']));

// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
exit;
}

// prepare email body text
$Body = "";
$Body .= "YourName: ";
$Body .= $YourName;
$Body .= "\n";
$Body .= "YourEmail: ";
$Body .= $YourEmail;
$Body .= "\n";
$Body .= "YourMessage: ";
$Body .= $YourMessage;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_sent.php\">";

}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
}

?>

Below is the form part of contact.php (dont know if you will need it ;))

<form method="post" action="contact_script.php">

<p>Your Name:<br />
<input type="text" name="YourName" class="contact_text_Input" />
</p>

<p>Your Email:<br />

<input type="text" name="YourEmail" class="contact_text_Input" />
</p>

<p>Your Message:<br />
<textarea name="YourMessage" cols="" rows="" class="contact_textarea_Input"></textarea>
</p>

<p><input type='image' name='submit' src='images/send_button.gif' class='contact_submit_Input' />
</p>
</form>

Schmoopy
04-21-2009, 10:11 PM
This is nice and simple just add this near to the top of your script after you've trimmed the slashes:



if(trim($YourName) == '' || trim($YourEmail) == '' || trim($YourMessage) == '') {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact.php?e=null\">";
exit;
}


Then in contact.php, put this somewhere after your form, or just somewhere where the message can be clearly seen:



if(isset($_GET['e']) && $_GET['e'] == 'null') {
echo "You didn't fill in all of the required fields.";
}



The trim just basically goes to the fields and strips and whitespace out of them, if someone tries to enter " " with a load of spaces in the middle, it will count as an empty string and will take them back to the contact form.

The || between the conditions acts as an OR, saying - If this is blank, or this is blank, or if this is blank then do this : code here.

Hope this solves your problem.

Edit: I don't quite get the validation in your script, was it taken out or something, have you forgotten to post the code for it?



$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=contact_error.php\">";
exit;
}


This will never get executed with the code you've provided since the only thing that happens to $validationOK is that it evaluates to true.
There's no part in the code where a condition sets it false, is there more of the code somewhere?

Ryan Fitton
04-23-2009, 08:00 PM
Thanks the script works great, i used a contact form generator here: http://www.tele-pro.co.uk/scripts/contact_form/, so i dont know whay the validation code as put in.