Jump to content

[SOLVED] Form Validation


kumarrana

Recommended Posts

It seems stupid but I could not debug form validation. Code looks like this

function verify_user_input()
{ 
if($_POST['title'] == "")
{
$error = "Blank title not allowed";
echo $error;
}
 else if($_POST['category'] == "")
{
$error = "Blank Category Not allowed";
echo $error;
}
else if($_POST['body'] == "")
{
         $error = "Empty body not allowed";
         echo $error
}

else
{
echo "<br> New post submitted successfully";
}
}

This code cold not validate $_POST['body'] part, others work fine.

Form input for this part look like this;

echo "<textarea name = body cols = 40 rows = 15> </textarea> <br><br>";

Any idea what is wrong?

 

 

Link to comment
https://forums.phpfreaks.com/topic/87020-solved-form-validation/
Share on other sites

I am ganna give you full code, may be that will help more. Rest of the code works besides validating body of the form. ($_POST['body'])

function verify_user_input()
{ 
echo "<br> Post: >". $_POST['body'];
if($_POST['title'] == "")
{
$error = "Blank title not allowed";
echo $error;
}
 else if($_POST['category'] == "")
{
$error = "Blank Category Not allowed";
echo $error;
}
else if($_POST['body'] == "")
{
echo "Empty body not allowed";
}
else
{
echo "<br> New post submitted successfully";
}
}
verify_user_input();
//dump_in_mysql_post();
} 
else
{
echo "Submit Blog Entry";
echo "<form action=\"$PHP_SELF\" method=\"POST\">";
echo "Title: <input type = text name = title> <br><br>"; 
echo "Category: <input type = text name = category><br><br>";
echo "Body: <br><br>";
echo "<textarea name = body cols = 40 rows = 15> </textarea> <br><br>";
echo "<input name=submit type=submit value=New Post>";
echo "</form>";
}

here's what always works for me

i use an array to hold all the errors

so

<?php
function verify_user_input()
{ 
$errors = array();
echo "<br> Post: >". $_POST['body'];
if($_POST['title'] == ""){
$errors[] = "Blank title not allowed";
}
if($_POST['category'] == ""){
$errors[] = "Blank Category Not allowed";
}
if($_POST['body'] == ""){
$errors[] = "Empty body not allowed";
}
if(empty($errors) {
echo "<br> New post submitted successfully";
} else {
foreach ($errors as $error) {
	echo "- ". $error;
}
}
verify_user_input();

?>

 

hope that helped!

-Zack

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.