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!
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?
$fields=array("somefield","someotherfields", "and so on....");
foreach($fields as $field)
if(empty($_POST[$field]))
die("you need to fill in form properly");
$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
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:
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
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.
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.