Jump to content

Archived

This topic is now archived and is closed to further replies.

Mirek

Check Box Status

Recommended Posts

Hi all,

How does one get a status of a check box from a form via the $_POST[] variable and the assign that same status in a check box of a form on the new page?

i.e if a form has been submited with a check box 'checked' I want to be able to 'check' a box on a different form.

[code]
<?php

if ($_POST['checkbox1']){

      ???;

}

?>
[/code]

thx in advance
Mirek

Share this post


Link to post
Share on other sites
Here is a quick example:

<?php

echo('<input type="checkbox" name="anothercheckbox"');
if($_POST['checkbox'] == "SomeValue"){
    echo(' checked');
}
echo('>');

?>

So now on the first form, the checkbox would have the value "SomeValue".
I think this is how you do that

Share this post


Link to post
Share on other sites
Hrm

Is the other form a seperate page altogether? Does it do any of the processing of the previous form?

If it is a seperate form and does not do any of the previous form's processing then you might have to pass the status of the checkbox via the URL and obtain it with $_GET['value'].

To see if box is checked:
[code=php:0] if (isset($_POST['checkbox1'])) { [/code]

If it is not checked, then it's like it doesnt exist.

Now in the next form, if you want to have a box already checked then its something similar to:
[code]
<?php

// Get value from URL of the checkbox
$checked = $_GET['checkbox1'];
?>
<!-- HTML HERE-->
<input type="checkbox" name="checkbox2" <?php if (isset($checked)) echo "checked=\"checked\"";?> /> Checkbox 2

[/code]

Share this post


Link to post
Share on other sites
Thanks to you both,

I did forget to mention that the actual script runs from an external include file and I was hoping to not to have to fiddle with every form on several pages but have a global script to adjust what I need.

I guess what I seek is something similar to the javascript :: document.form.textfield.value etc

I cant seem to find reference to similar in PHP.

Your solution is good and it works great, its what I use atm but I am hoping for something more like above.

Share this post


Link to post
Share on other sites
You do not have to use $_GET you can simply use....

<input type="checkbox" name="checkbox2" value="1" <?php if ($_POST['checkbox2'] == "1") echo "checked=\"checked\"";?> /> Checkbox 2

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.