...

View Full Version : need form validation



SDP2006
10-02-2003, 11:19 PM
How do I validate a form?

is it like this?


<?php
$from = $_POST['sender'];

if (!isset($from)){
echo "You did not specify a sender!";
}
else
{
include('sucess.php');
}
?>

Nightfire
10-02-2003, 11:43 PM
Did you try it?

SDP2006
10-03-2003, 12:56 AM
No, I didn't I like to see if it is right before I try it...

mordred
10-03-2003, 02:57 AM
Well, I think nightfire was implying that you should try it out. You don't need to put it on your actual website, so what harm can be done by it? If it doesn't work as expected, then you know that it's not working correctly.

Tip: isset() test if a variable exists. All form values appear initially as string values or arrays in the PHP script.

SDP2006
10-03-2003, 10:43 PM
Okay, I tried this


<?php
$name = $_POST['search'];

if(!isset($name)){
echo "You must enter a search query";
}
else
{
include('sucess.php');
}

?>


It partially works. If I enter something, it includes sucess.php. If I do not enter something, it still includes sucess.php!!

Now can someone help?

Nightfire
10-03-2003, 11:34 PM
if(trim($name) == ""){


That'll check if anything's been added.

You're better off using regex though, as someone could just enter a . or something stupid ;)

SDP2006
10-03-2003, 11:57 PM
Thanks ;)

How could I add more than one to that?

Like

if(trim($senderemail,$message) == ""){
echo "Must specify your email and message";
}

missing-score
10-04-2003, 01:01 AM
well, if all fields were required, you could do a foreach loop:




$valid = true;

foreach($_POST as $postvar){

if(trim($postvar) == ''){

$valid = false;

}

}

if($valid == true){

include('success.php');

} else {

echo 'Your form did not validate, please retry';

}





if you are only wanting to validate a few fields...




$validate = array('sender', 'message', 'subject');

$valid = true;

for($i=0;$i<count($validate);$i++){

if(trim($_POST[$validate[$i]]) == ''){

$valid = false;

}
}

if($valid == true){

include('success.php');

} else {

echo 'Your form did not validate, please retry';

}


I think that would be your best bet



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum