Form validation, this should be simple so I'm told?

Bleek

Junior Member
Another request for some php help on what is probably the most simple php form ever... :wacko:

I copied a tutorial off the next and here's the content:

<?php

$EmailFrom = "";
$EmailTo = "";
$Subject = "";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = ($_POST['Tel']);
$Message = Trim(stripslashes($_POST['Message']));

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

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$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=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>


After that I tried to add validation so each field was required, I used other web tutorials but I'm failing miserably to get it to work.

It's probably simple but I really don't know php at all, can anyone shed any light?
 
well I would point you in the direction of a post by jazajay but I can't seem to find it with the new updated forum layout... we're a bit limited on the user threads fronts, there's only 4/5 per user by the looks of it :(
 
What errors do you get? Is this hosted online anywhere for us to look at? Can you post the link to the tutorial as well please.
 
You can follow the one in my sig....I think it should still work.
Alternatively, just change the form to this:

[color=#ff0000;]<?php[/color]
[color=#ff0000;]If(isset($_POST['name'])){ [/color][color=#ff8c00;]//check form has been submitted.[/color]
[color=#ff0000;]If(!empty($_POST['name'] || !empty($_POST['Message']) || (!empty($_POST['Tel']) && is_numeric($_POST['Tel']) )){ [/color]
[color=#ff8c00;]//check all fields have data within them and that the phone number contains only numbers[/color]
[color=#ff0000;]$EmailFrom = "";[/color][color=#ff8c00;] //enter your email address[/color]
[color=#ff0000;]$EmailTo = ""; [/color][color=#ff8c00;]//enter the recipiant email address[/color]
[color=#ff0000;]$Subject = "";[/color][color=#ff8c00;] //enter a subject message[/color]
[color=#ff0000;]$Name ="";[/color]
[color=#ff0000;]$Tel="";[/color]
[color=#ff0000;]$Message ="";
$Name = Trim(stripslashes(strip_tags($_POST['Name'])));
$Tel = strip_tags($_POST['Tel']);
$Message = Trim(stripslashes(strip_tags($_POST['Message'])));[/color]
[color=#ff8c00;]// prepare email body text[/color]
[color=#ff0000;]$Body = "";
$Body .= "Name: ". $Name ."n";
$Body .= "Tel: ". $Tel . "n";
$Body .= "Message: ". $Message ."n";[/color]

[color=#ff8c00;]// send email[/color]
[color=#ff0000;]$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");[/color]
[color=#ff0000;]} [/color][color=#ff8c00;]//End of empty if check[/color]
[color=#ff0000;]else{ [/color][color=#ff8c00;]//something failed, so let's find out what.[/color]
[color=#ff0000;]$err="To be able to contact us, please enter: ";[/color]
[color=#ff8c00;]//Initial error message.[/color]
[color=#ff0000;]if(empty($_POST['name'])){[/color]
[color=#ff0000;]$err.="your name, ";[/color]
[color=#ff0000;]}[/color]
[color=#ff8c00;]//check to see if it is the name field[/color]
[color=#ff0000;]if(empty($_POST['Message']){[/color]
[color=#ff0000;]$err.="your message, ";[/color]
[color=#ff0000;]}[/color]
[color=#ff8c00;]//check to see if it is the message field[/color]
[color=#ff0000;]if(empty($_POST['Tel'])[/color]
[color=#ff0000;] $err.="Your phone number, ";[/color]
[color=#ff0000;]}[/color]
[color=#ff8c00;]//check to see if it is the phone field[/color]
[color=#ff0000;]if(!is_numeric($_POST['Tel'])){[/color]
[color=#ff0000;]$err.="Your phone number appears to be wrong (please remove any spaces), ";[/color]
[color=#ff0000;]}[/color]
[color=#ff8c00;]//check to see if the phone field is numeric.[/color]
[color=#ff0000;]echo "[/color][color=#4b0082;]<div id='error'>$err;</div>[/color][color=#ff0000;]";[/color]
[color=#ff0000;]}[/color]
[color=#ff0000;]} [/color][color=#ff8c00;]//End of form submitted check[/color]
[color=#ff0000;]?>[/color]
Please note:
This form is pretty basic - and I wrote that off the cuff. Any errors let us know and I'm sure one of us will correct it/them. Looks okay, but needs testing.
Hope it helps
Jaz
Key:
[color=#ff0000;]PHP[/color]
[color=#ff8c00;]Comments[/color]
[color=#4b0082;]HTML[/color]
 
Back
Top