Page 1 of 1

radiobutton

Posted: Wed Aug 13, 2003 11:11 pm
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. :(

Posted: Thu Aug 14, 2003 6:56 am
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

Posted: Thu Aug 14, 2003 7:09 am
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

Posted: Thu Aug 14, 2003 7:10 am
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.