Page 1 of 4
PHP form validation
Posted: Tue Sep 12, 2006 1:44 pm
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>
Re: PHP form validation
Posted: Tue Sep 12, 2006 2:24 pm
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";
}
}
?>
Thanks.. but...
Posted: Tue Sep 12, 2006 2:30 pm
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
Posted: Tue Sep 12, 2006 2:40 pm
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;
}
?>
Posted: Tue Sep 12, 2006 2:41 pm
by RobertGonzalez
Actually, now that I think about it, what is happening in your script?
Posted: Tue Sep 12, 2006 2:46 pm
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
Posted: Tue Sep 12, 2006 2:48 pm
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">
Posted: Tue Sep 12, 2006 3:25 pm
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?
Posted: Tue Sep 12, 2006 3:31 pm
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.
Posted: Tue Sep 12, 2006 4:11 pm
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!

Posted: Tue Sep 12, 2006 4:59 pm
by RobertGonzalez
Its all good. Man, I have sounded like an utter jerk today. I need some candy.
Guys
Posted: Tue Sep 12, 2006 5:02 pm
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.
And...
Posted: Tue Sep 12, 2006 5:17 pm
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.
Re: Guys
Posted: Tue Sep 12, 2006 5:29 pm
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.
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"
Ninja space goat
Posted: Tue Sep 12, 2006 5:35 pm
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.