Page 1 of 1

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

Posted: Sat Jul 31, 2004 12:58 pm
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 !

Posted: Sat Jul 31, 2004 1:01 pm
by markl999
name="group1[]"

...

Posted: Sat Jul 31, 2004 1:06 pm
by Calimero
nope still not working,
I put that for each radio.

Is there additional coding needed?

Posted: Sat Jul 31, 2004 1:14 pm
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>

...

Posted: Sat Jul 31, 2004 1:17 pm
by Calimero
Can GET method do this, I noticed you use POST.

Posted: Sat Jul 31, 2004 1:17 pm
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 ;)

...

Posted: Sat Jul 31, 2004 1:20 pm
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 !

Posted: Sat Jul 31, 2004 1:24 pm
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.

...

Posted: Sat Jul 31, 2004 1:27 pm
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.

...

Posted: Mon Aug 02, 2004 9:56 am
by Calimero
Still have the same problem...

Any ideas. :?:

Posted: Mon Aug 02, 2004 11:47 am
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.