if - radiobutton - checked - check it on the next page

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
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

if - radiobutton - checked - check it on the next page

Post by Calimero »

I have a form and in it 4 radiobuttons.

When I submit a form I need it to preserve the value of the radiogroup

I tried this, but it keeps returning the last value checked - as IF -doesnt check at all

Code: Select all

<input name="group1" type="radio" value="" <?if ($group1='') &#123;echo "checked";&#125; else &#123;&#125; ?> >

<input type="radio" name="group1" value="r2" <?if ($group1='r2') &#123;echo "checked";&#125; else &#123;&#125; ?> >

<input type="radio" name="group1" value="r3" <?if ($group1='r3') &#123;echo "checked";&#125; else &#123;&#125; ?> >

<input type="radio" name="group1" value="r4" <?if ($group1='r4') &#123;echo "checked";&#125; else &#123;&#125; ?> >

where is the error in the code, or how shoud I do this?

Thanks Ahead !
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

name="group1[]"
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

nope still not working,
I put that for each radio.

Is there additional coding needed?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Sorry, i misread your original question, just using name="group1" should do it, but you're relying on register_globals being On. With register_globals Off (as they should be) you'de do something like:

Code: Select all

<?php
$group1 = '';
if(!empty($_POST['group1'])){
  $group1 = $_POST['group1'];
}
?>
<form method="post" action="">
<input type="radio" name="group1" value="" <?php if($group1 == '') echo ' checked'; ?>>one<br />
<input type="radio" name="group1" value="r2"<?php if($group1 == 'r2') echo ' checked'; ?>>r2<br />
<input type="radio" name="group1" value="r3"<?php if($group1 == 'r3') echo ' checked'; ?>>r3<br />
<input type="radio" name="group1" value="r4"<?php if($group1 == 'r4') echo ' checked'; ?>>r4<br />
<input type="submit" name="submit" value="submit" />
</form>
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

Can GET method do this, I noticed you use POST.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yeah, it can, though i don't know why you'de use GET and not POST :o
Anyway, just substitute POST for GET to use ... GET ;)
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

Well, some guy that first coded my search engine pages used GET,
so I saw it works and continued to use them.

In practise what is the difference between POST and GET - security, speed, workflow ... ?

Read some doc's on this, but to me it never made significant difference in work (my 3 months of PHP coding :), and I was not completely sure what the authors wanted to say in those doc's.

Thanks Ahead !
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Bsically GET vars get passed in the url, POST vars get passed via a form. So GET forms tend to create ugly urls and also everyone can see the passed information (can also cause problems with bookmarks etc).
POST forms tend to be preferable. I usually reserve GET vars for links, foo.php?var=whatever etc.. and always use POST for forms.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

oh, yeah sorry.

To mention.

Just before radiogroup I echoed the value of group1.

It passes the values to this page, but it seems that IF comand does not function.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post by Calimero »

Still have the same problem...

Any ideas. :?:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, put a var_dump($_POST); and look at the output of it after you press submit. You should see the correct value of group1 in there. Then just before you display the form again put an echo $group1; , that should have the same value. If it doesn't have the same value or is empty/undefined then you have register_globals Off (which is good) and you need to do the $group1 = $_POST['group1']; line that i had above.
Also put error_reporting(E_ALL); as the very first line of your script, that should help in debugging it.
Post Reply