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
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>";
}

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.