radiobutton

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
pnxi
Forum Newbie
Posts: 3
Joined: Wed Aug 13, 2003 8:08 am

radiobutton

Post by pnxi »

can anybody show me a example to handle radiobutton in a html form
from a seperate php file? thanks
I'm a newer for php. :(
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

form:

Code: Select all

<form name="myform" method="post" action="radio.php">
<input type="radio" name="radio" value="radio 1" checked="checked">
<input type="radio" name="radio" value="radio 2">
<input type="radio" name="radio" value="radio 3">
<input type="radio" name="radio" value="radio 4">
</form>
radio.php

Code: Select all

<?
echo "$radio";
?>
i hope that helps. that how i handle my radio buttons. feel free to correct me if i'm wrong.

-Nay
megamanic
Forum Newbie
Posts: 5
Joined: Wed Aug 13, 2003 7:40 am

Post by megamanic »

There's nothing wrong with the code you've been given so long as register_globals is set on (the default is off) so it's a good idea to use an alternative method which I shall give you a slightly more real-world example of here...

Code: Select all

<form action="UProduct.php" method="post" name="Product" target="_self">

...

    <tr> 
      <td><strong>Grade</strong></td>
      <td><p> 
          <? 
						$Get = "SELECT GRADE, NAME FROM GRADES ORDER BY GRADE";
						$result = mysql_query($Get,$DB)
							or die("SELECT Query failed");
						$count = mysql_num_rows($result);
						for($n=0; $n< $count; $n++ ) &#123;
							$res = mysql_fetch_object($result);
							print '<label><input type="radio" name="Grade" value="'.$res->GRADE.'"';
							if ($res->GRADE == $Grade)
								print " checked";
							print ">$res->NAME</label>\n";
						&#125;; 
?>					
          <br>
        </p></td>
    </tr>
...
    <tr> 
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
</form>
and the handling of the variable from UProducts.php

Code: Select all

$SQL = 'UPDATE PRODUCTS SET
						GRADE='.strip_tags(substr($_POST&#1111;"Grade"],0,4)).',
						WHERE PRODUCT_ID = '.$_POST&#1111;'ID'];
Note I'm using the $_POST array by name to get the variable - this is much safer than relying on finding $Grade.

Hope this helps

Mike
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Be aware of the register_globals issue (last link in my signature).

Code: Select all

<? 
// if register_globals = off, this wont work:
    echo "$radio"; 
// but this will:
    echo $_POST&#1111;'radio']; 
?>
Note: One minute after previous poster, that also explains the issue.
Post Reply