[SOLVED]How to check if user have submitted a form

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

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[SOLVED]How to check if user have submitted a form

Post 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
Last edited by vigge89 on Fri Dec 26, 2003 3:38 am, edited 2 times in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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");
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :oops: :? :roll:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How to check if user have submitted a form

Post 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 ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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']))".
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, so if i name my submit button "submit", it will return true if the user clicked it?
right :D
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

exactly! ;)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

~great, thanks a bunch ! :) :wink:
Post Reply