abeli
August 6, 2010, 8:56pm
1
<html>
if(isset($_POST[‘submit’]))
{
$name=$_POST[‘name’];
$email=$_POST[‘email’];
$message=$_POST[‘message’];
$errorstring=‘’;
if(!name)
$errorstring=$errorstring.“NAME<br/>”;
if(!email)
$errorstring=$errorstring.“EMIL<br/>”;
if(!message)
$errorstring=$errorstring.“MESSAGE<br/>”;
if (errorstring!=“”)
echo"please fill out the following fields:<br/> $errorstring " ;
else{
die(‘success’);
}
}
?>
<form action=‘formvalidation.php’ method=‘POST’>
name:<input type=‘text name=‘name’ value=’<?php echo $name;?>‘><br />
email:<input type=‘text’ name=‘email’value=’<?php echo $email;?>’ >
<br />
message:<br /><textarea rows=“2” cols=“20”></textarea><br /><br />
<input type=‘submit’ name=‘submit’ value=“send”>
</html>
and i got this error i don’t know where is the error
if(isset($_POST[‘submit’])) { $name=$_POST[‘name’]; $email=$_POST[‘email’]; $message=$_POST[‘message’]; $errorstring=‘’; if(!name) $errorstring=$errorstring."NAME
"; if(!email) $errorstring=$errorstring."EMIL
"; if(!message) $errorstring=$errorstring.“MESSAGE
“; if (errorstring!=””) echo"please fill out the following fields:
$errorstring " ; else{ die(‘success’); } } ?>
abeli
August 9, 2010, 3:38am
2
thank you i just put name="message " in side the textarea
<textarea name="message" > <?php echo $message; ?> </textarea>
and it helped
If you look at your form, you’ll see that there is no named message field.
You have a place to type a message, but the form does not yet know to submit it as a field called “message”
abeli
August 9, 2010, 2:40am
4
thank u i appreciate your efforts but i still have the same problem even if i filled out the message field it shows the message “please fill out the message field”
You really shouldn’t filter those warnings while developing your code. It’s important for you to see and to fix those warnings.
The error that you are getting is due to your creating the variables inside the if statement. When the page is first displayed, the code in the if statement is not run, so those variables do not yet exist.
If your web server uses PHP 5.2 or greater, you can greatly simplify things by using filter_input, and optionally improve your security by using [url=“http://www.php.net/manual/en/filter.filters.php”]filters .
Notice how the email is being sanitised against the FILTER_SANITIZE_EMAIL filter.
<!DOCTYPE html PUBLIC >
<body>
<?php
$submit = filter_input(INPUT_POST, 'submit', FILTER_SANITIZE_STRING);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
$errorstring = '';
if ($submit === 'send') {
if (empty($name)) {
$errorstring .= "name<br/>";
}
if (empty($email)) {
$errorstring .= "Email<br/>";
}
if (empty($message)) {
$errorstring .= "message<br/>";
}
if (!empty($errorstring)) {
echo "please fill out the following fields:<br/>" . $errorstring;
} else {
die('success');
}
}
?>
<form action='formvalidation.php' method='POST'>
name:<input type='text' name='name' value ='<?php echo $name;?>'/> <br />
email:<input type='text' name='email' value='<?php echo $email;?>' /><br />
<br />
message:<br /> <textarea > <?php echo $message; ?> </textarea><br /><br />
<input type='submit' name='submit' value="send"/>
</body>
</html>
abeli
August 8, 2010, 5:49pm
6
thank you very much but when after the edition i got this messages
Notice: Undefined index: name in C:\wamp\www\formvalidation.php on line 6
Notice: Undefined index: message in C:\wamp\www\formvalidation.php on line 8
please fill out the following fields:
.NAME
Message
<!DOCTYPE html PUBLIC >
<body>
<?php
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$errorstring='';
if(!$name)
$errorstring=$errorstring."NAME<br/>";
if(!$email)
$errorstring=$errorstring."Email<br/>";
if(!$message)
$errorstring=$errorstring."Message<br/>";
if ($errorstring!="")
echo"please fill out the following fields:<br/>.$errorstring " ;
else{
die('success');
}
}
?>
<form action='formvalidation.php' method='POST'>
name:<input type='text name='name' value ='<?php echo $name;?>'/> <br />
email:<input type='text' name='email' value='<?php echo $email;?>' /><br />
<br />
message:<br /><textarea rows="2" cols="20"><?php echo $message;?></textarea><br /><br />
<input type='submit' name='submit' value="send"/>
</body>
</html>
RNEL
August 6, 2010, 8:59pm
7
you forgot to include $
if(!$name)
if(!$email)
if(!$message)
if($errorstring!="")
always filter user inputs
abeli
August 8, 2010, 10:27pm
8
thank you again you r amazing
but i got this error even if i filled out the message field i get this error
please fill out the following fields:
message
the script doesn’t work to check if the message exist or not
<!DOCTYPE html PUBLIC >
<body>
<?php
error_reporting(E_ALL ^ E_NOTICE);
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$errorstring='';
if(!$name)
$errorstring=$errorstring."name<br/>";
if(!$email)
$errorstring=$errorstring."Email<br/>";
if(!$message)
$errorstring=$errorstring."message<br/>";
if ($errorstring!="")
echo "please fill out the following fields:<br/>".$errorstring;
else{
die('success');
}
}
?>
<form action='formvalidation.php' method='POST'>
name:<input type='text' name='name' value ='<?php echo $name;?>'/> <br />
email:<input type='text' name='email' value='<?php echo $email;?>' /><br />
<br />
message:<br /> <textarea > <?php echo $message; ?> </textarea><br /><br />
<input type='submit' name='submit' value="send"/>
</body>
</html>
RNEL
August 8, 2010, 7:03pm
9
ok thanks. you have E_NOTICE on
put this at the top of your code, it will not report E_NOTICE errors like that
error_reporting(E_ALL ^ E_NOTICE);
or set your vars like these
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
always filter user inputs