PDA

View Full Version : getting form to send error if all fields aren't filled in.


Josh
12-16-2010, 11:12 PM
Hi,

I have a form where at the moment, when the user fills in the form, if they leave out a field then it brings up an error, but it brings it up in a blank page with the field name that has been left empty. All I want is a small error next to my submit button so it looks more professional.

When the user clicks submit form this is the code that is generated


<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'info@onlinestage.co.uk';

// Where to redirect after form is processed.
$url = 'http://www.makeyouafilm.co.uk/includes/orderform-standard.php';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '1';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 100)
{echo "Name of form element $key cannot be longer than 100 characters";die;}
$j = 100 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>


Please help,

Thanks,

Josh

Corrosive
12-17-2010, 07:05 AM
I would tend to use javascript for validation. Try some of these ideas; http://speckyboy.com/2009/12/17/10-useful-jquery-form-validation-techniques-and-tutorials-2/