Form processing script help

Hi there…

I am currently working through my first attempt at php and i have hit a brick wall, i am trying to put together a form processing script. What i have so far i have put together through research but it is very basic. Although basic it works and sends the email i have told it to, but other than “htmlspecialchars” it has no security or validation.

Through research i can find code that is supposed to achieve what i want but it is given in snippets and with no guide as to where in the code it should go therefore leaving me confused. Also after submission it echo’s a lil message and a link back to a page on site but its not pretty and would prefer it to redirect to a prepared thankyou page which as yet i havent been able to accomplish either.

The code below is created in dreamweaver, any or all help will be greatly appreciated…

Form

[code]















subject appointment aftercare info design info

Message

 

[/code]

Processing code
[php]<?php

$name = htmlspecialchars($_POST[‘name’]);
$email = htmlspecialchars ($_POST[‘email’]);
$number = htmlspecialchars ($_POST[‘number’]);
$topic = htmlspecialchars ($_POST[‘topic’]);
$message = htmlspecialchars ($_POST[‘message’]);
$formcontent = “From: $name \n Email: $email \n Number: $number \n Topic: $topic \n Message: $message”;
$recipient = “*******@hotmail.co.uk”;
$subject = “Contact Form”;
$mailheader = “From: $email \r\n”;

mail($recipient, $subject, $formcontent, $mailheader) or die(“Error!”);

echo “Thank You!” . " -" . " Return Home";
?>[/php]

Well, what kind validation were you looking to do? if you want to do required fields, use empty(), something like
[php]
$error = array();
if(empty($_POST[‘name’])) {
$error[] = “Required Field”;
} else {
$name = htmlspecialchars($_POST[‘name’]);
}[/php]

that will check to see if the field is empty. If you want do string validation, look up preg_match(). There are tons of form validation tutorials out there, just search for php form validation

Here is a link for a full sample of validation scripts… It might help you out…

http://www.montanaprogrammer.com/php-web-programming/php-form-validation/

There is a link on it for a more advanced version which is interesting, too…

Thankyou both so much for your help :smiley:

The empty field validation will be perfect, in the case of that would i place it in my code like this?

[php]
$error = array();
if(empty($_POST[‘name’])) {
$error[] = “Required Field”;
} else {
$name = htmlspecialchars($_POST[‘name’]);
}
$error = array();
if(empty($_POST[‘email’])) {
$error[] = “Required Field”;
} else {
$email = htmlspecialchars($_POST[‘email’]);
}
[/php]

and continue this same code for each value?

And thankyou Ernie i think that link you posted shows how to redirect to a thankyou page, i never came across that when i was searching google.

One last question if is ok, in the case of spam protection, is it better to use a question ie 2+2 or the captcha images??

Thanks again for your replys guys…

Yes. As for human verification, it doesn’t matter which one. They both accomplish the same thing.

Excellent thankyou very much, i will add this code to my script when i get the kids to bed tonight, and if it is successful i will hit topic solved. Thankyou both again. ;D

I would actually use striptags() on all but the message. Youll need add some headers to send a message as html

Sponsor our Newsletter | Privacy Policy | Terms of Service