Check a checkbox from the URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Check a checkbox from the URL

Post 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.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Check a checkbox from the URL

Post 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\""; ?> />
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Check a checkbox from the URL

Post by lovelf »

THANK YOU SO MUCH!

It works great!
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Check a checkbox from the URL

Post by lovelf »

OK, now is there a way to make it work if the form is inside an iframe?

pagecontainingiframeurl?form.php?check11=checked ?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Check a checkbox from the URL

Post 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.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Check a checkbox from the URL

Post 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.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Check a checkbox from the URL

Post 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\""; ?> />
 
 
 
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Check a checkbox from the URL

Post 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 :banghead:
Last edited by lovelf on Wed Nov 05, 2008 3:37 am, edited 1 time in total.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Check a checkbox from the URL

Post 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\""; ?> />
?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Check a checkbox from the URL

Post 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.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Check a checkbox from the URL

Post 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?
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Check a checkbox from the URL

Post 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;
}
 
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Check a checkbox from the URL

Post by lovelf »

Alright, I just implemented the switch. That is it for this topic.
Post Reply