homepage Welcome to WebmasterWorld Guest from 54.234.0.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to find if a form was submitted when no Post/Get data is sent?
If you submit a form with no data how to find out if it was submitted?
NooK




msg:3488287
 1:44 pm on Oct 26, 2007 (gmt 0)

Basically I have a form of, say, just checkboxes where each checkbox submits the form upon click (onclick event).

My problem is that if the user has 1 checkbox selected and he deselects that checkbox, the form is submitted with no data, thus I have no post or get data and so I cannot find out if the form was submitted or not (Making it so that the user will never be able to deselect all the boxes and 1 will always be selected).

Is there any way around this other than putting a hidden field on the form and checking for that hidden field on $_REQUEST to find out if the form was submitted or not? Is that even a nice solution?

Best Regards

NooK

 

d40sithui




msg:3488411
 3:29 pm on Oct 26, 2007 (gmt 0)

um, i wonder why you would need something like this.
typicaly, when you hit the submit button, thats all the server knows to be able to process the POST/GET data. In an ideal envirnment, your script would be able to check all the data submitted via the fields, which will ALWAYS be present even if the data are empty when you submit.
so you can check if the fields are there. if they are, it means the form was submitted.
try

<?

/*
assuming you have field names "name" and "password" in your form
and you're using POST
*/
if(isset($_POST['name']) && isset($_POST['password'])){

/*
form is submitted -> continue validating and processing data
*/

}
else{
/* display form */
}

the field that i find that doest work in this matter is the <textarea> field type. in this case, you may want to consider alternatives such as checking the POST field for your submit button.

cameraman




msg:3488492
 4:35 pm on Oct 26, 2007 (gmt 0)

You may be able to use
if($_SERVER['REQUEST_METHOD'] == 'POST') {
} // EndIf form was posted

I haven't tested it. I would imagine that if someone entered the page for the first time that the method would be GET even if there's no query string. The other thing you could do is separate the display script from the processing script - if your form appears on page1.php,
<form action="page2.php" method="post">

then page2.php can always assume that it's responding to a POST.

NooK




msg:3490459
 1:38 pm on Oct 29, 2007 (gmt 0)

Thanks for the answers.

In response to d40sithui, checkboxes that are not checked are not included in the POST/GET (Which I always found dumb myself, since the fact that it is unchecked doesn't mean it was not changed by the user) thus if your form consists of only checkboxes and none of the are checked and you use not submit button (Submit by javascript) your POST/GET will contain no data.

Thanks cameraman. I am not a fan of splitting up the files in 2 files but I'll give the other suggested method a try.

Best Regards

NooK

d40sithui




msg:3490565
 3:31 pm on Oct 29, 2007 (gmt 0)

i think this works if u rather not split up the pages.

<input type="submit" name="submit" value="submit">

/* process for posted data */
if(isset($_POST['submit'])){

}
/* display default form*/

Demaestro




msg:3490638
 4:18 pm on Oct 29, 2007 (gmt 0)

You can also just place a hidden element with a value into the form and see if that value exists. Even if nothing is filled out the hidden element will be submitted.

NooK




msg:3491337
 12:01 pm on Oct 30, 2007 (gmt 0)

Yeah that's what I thought, that a hidden field would probably solve the problem.

As for a submit button, it is something I didn't wnat because it is sort of a filter for a table (As soon as the user click on a checkbox to add/remove a filter the filter should be applied rather than having to click a submit button).

Thanks for the answers, I'll see what I can come up with.

Best Regards

NooK

penders




msg:3491425
 2:13 pm on Oct 30, 2007 (gmt 0)

...putting a hidden field on the form and checking for that hidden field on $_REQUEST to find out if the form was submitted or not? Is that even a nice solution?

IMO that is a 'nice solution'. You could even use the hidden field to perhaps store the original state of the fields, or whatever.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved