Page 1 of 1

PHP form validation help

Posted: Sun Sep 19, 2004 12:34 pm
by NewComputer
feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I need to have the following form force information to be added. NO BLANK FORMS. I want all forms to containg data and if they do not I want a pop up to open with the fields that do need to have the data filled. All the tutorials I have seen do not really help me.

Below is some of the code:

Code: Select all

<?php
$to = "email@email.ca";
$from = "From: email@email.com\t";
$content = "Business Name: " . $_POST["business-name"] . "\r\n \r\n";
$content .= "Their Name: " . $_POST["your-name"] . "\r\n \r\n";
$content .= "Sign Address: " . $_POST["sign-address"] . "\r\n \r\n";
$content .= "Phone Number: " . $_POST["phone-number"] . "\r\n \r\n";
$content .= "Fax Number: " . $_POST["fax-number"] . "\r\n \r\n";
$content .= "Current Message: " . $_POST["current-message"] . "\r\n \r\n";
$content .= "New Message: " . $_POST["new-message"] . "\r\n \r\n";
mail($to, "Website form submission", $content);
?>
Thanks in advance for any help you may provide.


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


[color=darkgreen][b]feyd[/b] | Moved to PHP - Code[/color]

Posted: Sun Sep 19, 2004 12:40 pm
by feyd
what have you tried? Not the tutorials, the code for your validation. We aren't here to do the work for you.

Posted: Sun Sep 19, 2004 12:45 pm
by NewComputer
feyd wrote:what have you tried? Not the tutorials, the code for your validation. We aren't here to do the work for you.
I originally tried to use javascript to force the entry, but it would never work properly. I am fairly new to PHP so the 'if' statement with "" never worked properly for me either.

I will be honest, I am not looking for someone to 'do the work' for me, but I am looking for someone to maybe either point me to a tutorial that will explain it simply or edit my code once to show me what I need to be doing. I am reading all the time on the web trying to get this resolved.

Posted: Sun Sep 19, 2004 1:01 pm
by feyd
the basics for a fairly simple to maintain form processor:

create an [php_man]array[/php_man] of required filled field names.
use a [php_man]for[/php_man], [php_man]while[/php_man], or [php_man]foreach[/php_man] to loop through the field names, checking if the posted field is [php_man]empty[/php_man] (after [php_man]trim[/php_man]ming leading and trailing whitespace).
if all the required fields pass, you do your mail. If not, you redisplay the form with an error.. or some other concept like that.

the following may help:
viewtopic.php?t=23687
viewtopic.php?t=23483

Posted: Sun Sep 19, 2004 1:06 pm
by NewComputer
Thank you.

Posted: Sun Sep 19, 2004 7:45 pm
by tim
isset() is a nice n handy function worthy to mention too

Posted: Sun Sep 19, 2004 7:51 pm
by dethron
and do not forget the nice: ===

Posted: Sun Sep 19, 2004 8:02 pm
by John Cartwright
in a nutshell

Code: Select all

<?php

$required = array('name','age');

$error = array();

foreach ($required as $req)
{

if (empty($_POST[$req]))
{

$error[] = 'You are missing $req';

}

}

?>
Good old arrays are best way to catch errors, obviously.