...

View Full Version : Checking post variables from checkboxes



piz
11-15-2003, 12:23 PM
Hi,

I have a script that saves atomatically all submitted POST-Variables in a MySQL Table, if they match with the columns, updating a row.

Now I have following Problem. One of the Variables is submitted with a checkbox in the previous HTML Form. If the checkbox is checked, there is no problem. The value of the checkbox is submitted. But if the checkbox isn't checked, the Variable isn't submitted. It even doesn't respond on isset().

Is there any possibility to assign a value to an unchecked checkbox?
Or to check the empty POST-Variables?
...without checking the variable by his name, because I am using this script for several forms.

Thx very much.

Saludo
piz

ConfusedOfLife
11-15-2003, 12:50 PM
No, you can't assign a variable or anything to an unchecked check box, it merely doesn't exist in your server side script. Why don't you wana put a name for that?? You can call it by a name, like "whatever" and then check it wherever you want:



if ( !empty($_POST["whatever"]) )
// do something


But I think I know your problem! You are having your checkbox in the first page whose action is the 2nd page, when this checkbox is submitted, you can see it in the 2nd page, but when the 2nd page is itself submitted, you see nothing, right?!! Well, you have several solutions for that:

1- when the 2nd page is loaded for the first time, you can easily check if the checkbox is checked or not, then you can write it into a hidden field that you recieve it whenever the 2nd page is submitted.

2- this one is better and more systematic I think! I always use $_SERVER["PHP_SELF"] for the action of my forms. Then I check the validity of info in the same page and only if the data is valid, I transfer the user to the 2nd page. What do I do with my first page data?? Well, you can either $_SESSION them, or put them in a query string and send it to your 2nd page. Then in your 2nd page to get your data back whenever that page is submitted, you have to write an action like this:


<form action="<?=$_SERVER["PHP_SELF"] . "?" . $_SERVER["QUERY_STRING"]?>">


In case you use sessions, be careful that you empty the session when your job is finished with your forms/database, because the 2nd time that you come to the same form, you see that the data is still there!

For the 2nd solution you can make multi-paged registration forms, in which you don't get all the info in only one page. :p

Íkii
11-15-2003, 02:30 PM
Another (perhaps dodgy) workaround would be to set a hidden input prior to every checkbox and set a default value in it

<input type="hidden" name="checkbox1" value="off" />
<input type="checkbox" name="checkbox1" value="proper value" />

then the receiving page would set $_POST['checkbox1'] to 'off' and only reassign that to 'proper value' if the checkbox was sent.

*** untested -
also note: arrays of checkboxes (or radio buttons) would be trickier to manage.

raf
11-15-2003, 04:00 PM
Maybe i misunderstood, but why not simply use !isset()? (that's what i always do --> unchecked checkboxes aren't posted so they aren't known in the $_POST collection)
If it isn't set (so if it is unchecked) then you set some variable to a defaultvalue. If it is set, you set the variable to the chkbox value.

You then nuse that variable inside the insertstatement



if (!isset($_POST['var']))) {
$var = '0';
} else {
$var = $_POST['var'];
}


If you want it to be completely generic, then you need to include a hidden formfield with a (pipe)delimited list of the names of all checkboxes as a value

like
value="var2|var3|var4"

Then you can use explode() to split the value up and get all checkboxes names. With a foreach-loop, you can then check if the checkbox isset



$chk = explode("|", $_POST['checkboxes']);

foreach ($chk as $var) {
if (!isset($_POST[$var])) {
$var_sql = '0';
} else {
$var_sql = $_POST[$var];
}
// append $var and $var_sql to the field and valuelist of the sql_statament
}

piz
11-15-2003, 04:34 PM
@ConfusedOfLife: No, that isn't my problem. My problem is on the page, where the variables are postet to the first time. This is a page where I redirect several forms to. So I can't name the variable, because I am using different tables, too, without check which table I am using - and that would be a lot of script more.

@raf: see below... ;-)

@Íkii: Yes, I got this idea, too - I'll try it. Hope it works.

Thx to all.

piz
11-15-2003, 05:03 PM
Yes - it works!
Great.
Thx.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum