Page 1 of 1
[SOLVED]How to check if user have submitted a form
Posted: Thu Dec 25, 2003 3:26 pm
by vigge89
I have a form on my page which when submitted, stores all the form fields and other things in a mysql table, but now onto the question;
How do i check if a user have submitted a form properly?
until now, i've used;
Code: Select all
<?php
if (!empty($_POST['something'])) {
//Do something
} else {
//Do something
}
?>
But that can look kinda strange, since im only checking for ONE var, and not the form itself
Posted: Thu Dec 25, 2003 3:30 pm
by Weirdan
that's what the loops were invented for
Code: Select all
$fields=array("somefield","someotherfields", "and so on....");
foreach($fields as $field)
if(empty($_POST[$field]))
die("you need to fill in form properly");
Posted: Thu Dec 25, 2003 3:32 pm
by vigge89
Weirdan wrote:that's what the loops were invented for
Code: Select all
$fields=array("somefield","someotherfields", "and so on....");
foreach($fields as $field)
if(empty($_POST[$field]))
die("you need to fill in form properly");
sorry, i edited the topic too late, i was asking for the wrong thing at the start, please check the topic again

Re: How to check if user have submitted a form
Posted: Thu Dec 25, 2003 3:50 pm
by Weirdan
vigge89 wrote:
But that can look kinda strange, since im only checking for ONE var, and not the form itself
It doesn't look strange. PHP knows nothing about 'forms'. But you can implement, say, 'Form' class and put all form checking routine to it. Or just use google: [google]php form class[/google]
Personally I do use my own form class as follows:
Code: Select all
require 'Form.inc';
$f=new Form();
$f->AddCaption("Form!");
$f->AddText("field1","It's a field number one","Default contents");
$f->AddText("field2","It's a field number two","some Default contents");
$f->AddSubmit("Click me!");
if($f->submitted){ //process the form
if($f->checkForm())
//....
}else{ //display the form
echo $f->generateHTML();
}
I can't publish its source because that class is a part of proprietary software but pretty sure there are a lot such classes available. Just google it

Posted: Thu Dec 25, 2003 4:03 pm
by vigge89
i think i've seen before that "if ($_POST['submit'])" have been used, does it work, and would that be that the submit-button have been clicked on? that is what i would like, so i don't have to check if a post-var isn't empty, so i instead could check if the user have submitted a form.
Posted: Thu Dec 25, 2003 4:31 pm
by nigma
One thing you might try is looping through the entire $_POST's array and checking to see if each key has been assigned a value. But that might not be so helpful if some of the form items in the previous form were optional.
Posted: Thu Dec 25, 2003 4:36 pm
by Weirdan
vigge89 wrote:i think i've seen before that "if ($_POST['submit'])" have been used, does it work?
It does if you name your button 'submit'.
actually, "if(isset($_POST['submit']))".
Posted: Thu Dec 25, 2003 4:55 pm
by vigge89
ok, so if i name my submit button "submit", it will return true if the user clicked it?
right

Posted: Thu Dec 25, 2003 5:03 pm
by Weirdan
exactly!

Posted: Fri Dec 26, 2003 3:38 am
by vigge89
~great, thanks a bunch !
