Welcome to WebmasterWorld Guest from 54.159.26.69

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

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

5+ Year Member



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

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

5+ Year Member



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

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

WebmasterWorld Senior Member 5+ Year Member



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

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

5+ Year Member



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

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

5+ Year Member



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

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

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

5+ Year Member



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

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

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



...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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month