Page 1 of 1
Check a checkbox from the URL
Posted: Wed Nov 05, 2008 12:14 am
by lovelf
How can I check a checkbox or more than one from the url?
I want to be able to have some of the checkboxes in my form already checked only if the user comes from a certain link to the form page.
example:
http://www.url.com/form.php?check11=checked or
http://www.url.com/form.php?check11=che ... k7=checked two different links from previous pages that would automatically check a checkbox.
I have no clue how to make this work.
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 12:53 am
by papa
Code: Select all
<input type="checkbox" name="foo" <?php echo $_GET['check11']; ?> />
or
<input type="checkbox" name="foo" <?php if($_GET['check11'] == "checked") echo "checked=\"checked\""; ?> />
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 1:22 am
by lovelf
THANK YOU SO MUCH!
It works great!
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 1:31 am
by lovelf
OK, now is there a way to make it work if the form is inside an iframe?
pagecontainingiframeurl?form.php?check11=checked ?
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 1:58 am
by papa
Hm,
<form method="get" action="thepagethatwantstheurlparameters.php">
Edit:
But that wont work, I just woke up so everything is not working properly.
You could solve it with javascript.
Edit2:
http://www.webdeveloper.com/forum/archi ... 52864.html
This might work, but you do it the other way around.
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 3:06 am
by lovelf
Thanks again for your help.
But that javascript code is used to submit a form. How can I add the auto check value to the page containing the iframe in the URL?
I need the auto check to be activated only if coming from a previous link in a different page that does not contain the iframe with the form.
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 3:09 am
by papa
Aha ok.
Well say that the user is coming from the form page. You have some variables passed there right?
So we can work the other way around instead. Say that we call your form "name".
If the user is coming from a page
without the form:
Code: Select all
<input type="checkbox" name="foo" <?php if(empty($_GET['name])) echo "checked=\"checked\""; ?> />
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 3:30 am
by lovelf
Yeah, that does work, but the checkbox gets checked automatically even if the user is not coming from the specified link, I need it to be activated only if they come from that link and on the iframe of the page which contains the form. I could use a php include function instead of an iframe but it needs to be an iframe

Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 3:36 am
by lovelf
Is it possible to get a
Code: Select all
<input type="checkbox" name="foo" <?php if($_GET['check11 (from the URL of the page that contains the iframe which is including this checkbox) '] == "checked") echo "checked=\"checked\""; ?> />
?
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 3:37 am
by papa
You should look upp http referer. Using that you can specify which sites/pages the checkboxes will be pre-checked for.
Edit:
Or you can use the url you had earlier, just define a var in that url and change the php code accordingly.
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 5:34 am
by lovelf
This is what I did to the page that shows the iframe:
Code: Select all
<?
if($_GET['db'] == "y") echo <<<html
<iframe frameborder=0 width=780px height=650px src=http://www.siteurl/contactb.php?check11=checked></iframe>
html;
?>
<?
if($_GET['db'] == "n") echo <<<html
<iframe frameborder=0 width=780px height=650px src=http://www.siteurl/contactb.php?check12=checked></iframe>
html;
?>
<?
if($_GET['db'] == "r") echo <<<html
<iframe frameborder=0 width=780px height=650px src=http://www.siteurl/contactb.php?check11=checked&check12=checked></iframe>
html;
?>
<?
if($_GET['db'] == "z") echo <<<html
<iframe frameborder=0 width=780px height=650px src=http://www.siteurl/contactb.php></iframe>
html;
?>
I finally got what I wanted thanks to you Papa. Since I do not know much PHP code I did not even know there was that GET method which I could use.
One final question, could I set the "db=z" as a default to be shown in case there is no db=y , n , or r in the URL?
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 5:50 am
by papa
Instead of if you can use a switch statement
Code: Select all
switch($_GET['db']) {
case "r":
bla bla
break;
case "n":
bla bla
break;
default:
z
break;
}
Re: Check a checkbox from the URL
Posted: Wed Nov 05, 2008 7:33 am
by lovelf
Alright, I just implemented the switch. That is it for this topic.