PHP form validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

PHP form validation

Post by akimm »

I'd like to use PHP to as form validation, but at this point its not working right, am I making some sort of mistake I can't see?

error.php

Code: Select all

<?php
$a = (!isset($_POST['name']));
$b = (!isset($_POST['title']));
$c = (!isset($_POST['article']));
$submit = "<input type=submit value=submit your article name=submit>";

if($a) { 
echo "Please put your name"; 
}	elseif($b) 	{ 
echo "Please post your title"; 
}	elseif($c) 	{ 
echo "Please post an article"; 
}	else 	{
echo $submit;
}
?>
form.php

Code: Select all

<form method="POST" action="muk.php">
<p><b>Date</b><input type="text" value="<b><?php echo date('F j, Y, g:i a');?></b>" name="date" size="30">
<p><b>Your name</b> 
<input type='text' name='name' size='30'></p>

<p><b>Your email</b>
<input type='text' name='email' size='30'></p>

<p><b>article title</b>
<input type='text' name='title' size='30'></p>

<p><b>article</b>
<TEXTAREA NAME='article' COLS='120' ROWS='20' WRAP='virtual'></TEXTAREA></P> 
<?php include("error.php");?>
<p><b><input type="submit" value="submit your article" name="submit">
</b></p>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP form validation

Post by Christopher »

Try starting with something like this:

Code: Select all

if(!isset($_POST['submit']) { 
    if(!isset($_POST['name']) { 
        echo "Please put your name"; 
    }	elseif(!isset($_POST['title']) 	{ 
        echo "Please post your title"; 
    }	elseif(!isset($_POST['article']) 	{ 
        echo "Please post an article"; 
    }
}
?>
(#10850)
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Thanks.. but...

Post by akimm »

I've tried it that way, it yielded the same response as when I tried the elseif statements..

Code: Select all

Parse error: parse error, unexpected '{' in /nfs/cust/8/25/05/650528/web/error.php on line 2
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
// $a will be set to false or true depending on whether this post var is set
$a = (!isset($_POST['name']));

// $b will be set to false or true depending on whether this post var is set
$b = (!isset($_POST['title']));

// $c will be set to false or true depending on whether this post var is set
$c = (!isset($_POST['article']));

// The is invalid HTML. You need to wrap your attribute values in double quotes
$submit = "<input type=submit value=submit your article name=submit>";

if($a) {
echo "Please put your name";
}       elseif($b)     {
echo "Please post your title";
}       elseif($c)     {
echo "Please post an article";
}       else   {
echo $submit;
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Actually, now that I think about it, what is happening in your script?
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

I prefer JavaScript upon PHP in form validation:

Code: Select all

<script type="text/javascript">
function checkForm()
{
	var error = "";
    if(document.myForm.name.value == "")
    {
		error += "Name missing! \n\t";
	}
    if(document.myForm.email.value == "")
    {
		error += "Email missing! \n\t";
	}
    if(document.myForm.title.value == "")
    {
		error += "Title missing! \n\t";
	}

	if(error != "")
	{
		alert(error);
		return false;
	}
}
</script>
and you also have to change the form tag:

Code: Select all

<form method="POST" action="muk.php" name="myForm" onsubmit="return checkForm();">
EDIT: put the javascript script below the form or in the head
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Off topic, but...
you need to wrap your html attributes... especially when the value is more than one word... how is the browser supposed to parse this?

Code: Select all

<input type=submit value=submit your article name=submit>
Do this instead:

Code: Select all

<input type="submit" value="submit your article" name="submit">
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The Ninja Space Goat wrote:Off topic, but...
you need to wrap your html attributes... especially when the value is more than one word... how is the browser supposed to parse this?

Code: Select all

<input type=submit value=submit your article name=submit>
Do this instead:

Code: Select all

<input type="submit" value="submit your article" name="submit">
I said that...
Everah wrote:

Code: Select all

// The is invalid HTML. You need to wrap your attribute values in double quotes
$submit = "<input type=submit value=submit your article name=submit>";
ok wrote:I prefer JavaScript upon PHP in form validation:
What if the user has disabled javascript?
User avatar
gkwhitworth
Forum Commoner
Posts: 85
Joined: Tue Sep 05, 2006 8:28 pm
Location: Wasilla, Alaska

Post by gkwhitworth »

Testy test, settle down guys. Jk, but you do really need to get in that habit real quick or your problems have only begun.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Everah wrote:
The Ninja Space Goat wrote:Off topic, but...
you need to wrap your html attributes... especially when the value is more than one word... how is the browser supposed to parse this?

Code: Select all

<input type=submit value=submit your article name=submit>
Do this instead:

Code: Select all

<input type="submit" value="submit your article" name="submit">
I said that...
Everah wrote:

Code: Select all

// The is invalid HTML. You need to wrap your attribute values in double quotes
$submit = "<input type=submit value=submit your article name=submit>";
ok wrote:I prefer JavaScript upon PHP in form validation:
What if the user has disabled javascript?
Oops.. sorry! :oops: :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Its all good. Man, I have sounded like an utter jerk today. I need some candy.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Guys

Post by akimm »

I need not learn about HTML double quotation. I knew that already, however, I thought within the PHP if I double quoted everything I would throw the script for a loop.

Secondly, this isn't off topic, its a PHP question with the validation. if I'm wrong I'll leave this site if it pleases those who are so cathartic. If the off topic was not to me, I appoligize.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

And...

Post by akimm »

I'm not trying to sound like a jerk

but just to make sure I was right before I said it, you can indeed use this

<input type=submit value=submit your article name=submit>

and it will be parsed just fine. However, its poor syntax and the only reason it was how it was, was because of my misunderstadning, I was under the impression that as I said before, if I put that in the PHP variable it would throw it off, because of the multiple double quotes.

I guess I could of concactanatted it, but I didn't really think of it at the time.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Guys

Post by Luke »

akimm wrote:Secondly, this isn't off topic, its a PHP question with the validation. if I'm wrong I'll leave this site if it pleases those who are so cathartic. If the off topic was not to me, I appoligize.
I wasn't saying you were off topic. I was saying that correcting your HTML was off topic. :roll:
akimm wrote:I'm not trying to sound like a jerk

but just to make sure I was right before I said it, you can indeed use this

<input type=submit value=submit your article name=submit>

and it will be parsed just fine. However, its poor syntax and the only reason it was how it was, was because of my misunderstadning, I was under the impression that as I said before, if I put that in the PHP variable it would throw it off, because of the multiple double quotes.

I guess I could of concactanatted it, but I didn't really think of it at the time.
No you can't. It renders a button, but does it say "submit your article" nope. It says "submit"
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ninja space goat

Post by akimm »

Humor me. Go to this website

http://www.akimm.com/add_art.php


Type whatever you want and click Submit

afterwards you'll be directed to a page that outputs what you wrote, then go ahead and follow the link to my philosophy section and you'll see your content, Thereby proving the syntactically retarded code does indeed work.
Post Reply