How to grab the value of a radio button?

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
ThumbsUp
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2004 12:05 am

How to grab the value of a radio button?

Post by ThumbsUp »

Hi all,

I've searched through the PHP manual and this forum until I'm blue in the face and can't seem to find the answer to my question, so I appologize in advance if this has been asked and answered previously.

I can't seem to figure out how to grab the actual value of a radio button, which is checked and submitted through a form, processed by PHP and added to a MySQL database. In javascript, I would use a for loop to step through the values and figure out which one is checked, add it's value to a variable and use it from there. However, either my brain is fried tonight and I'm not seeing the obvious, or ...... my brain is just plain fried! ;)

Here's the code that does NOT work. It fills the database with nothing in that field. Could someone please give me a clue how to grab the value of this dang radio button?

Code: Select all

<?PHP 
// some security stuff goes here but has been removed for display purposes
// assume user is logged in and has appropriate userlevel if they made it this far
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#cfdac5" text="#003300" link="#006600" vlink="#003300">
<?
// check to see if this form reached by clicking submit to add an entry
if ($submit) {
  $org1 = $HTTP_POST_VARS["org"];
  $clnum1 = intval($HTTP_POST_VARS["clnum"]);
  $cldescr1 = $HTTP_POST_VARS["cldescr"];
  $aglim1 = $HTTP_POST_VARS["agelim"];
  $agereq1 = $HTTP_POST_VARS["agereq"];
  $sql = "INSERT INTO classlist (org,clnum,cldescr,agelim,agereq)
    VALUES ('$org1','$clnum1','$cldescr1','$agelim1','$agereq1')";
  $result = mysql_query($sql) or die("Invalid Query");
  echo "<p align="center">$org $clnum $cldescr $agelim $agereq <br>
	<b>added to the database</b>.</p>";
  echo "<p align="center"><a href="dbadrec.php?action=additnow">Add another</a></p>";
  echo "<p align="center"><a href="dbadmin.php?action=addconfirmed">All Done</a></p>";
}
else {
?>
  <form name="addform1" method="POST" action="dbadrec.php">
  <fieldset>
  <legend><font size="-1">Add DB Records</font></legend>
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <th align="center"><font size="-1">Organization</font></th>
        <th align="center"><font size="-1">Class #</font></th>
        <th align="center"><font size="-1">Description</font></th>
        <th align="center"><font size="-1">Limit N/Y?</font></th>
        <th align="center"><font size="-1">Age Limit</font></th>
      </tr>
      <tr>
        <td><input type="text" name="org" size="6"></td>
        <td><input type="text" name="clnum" size="3"></td>
        <td><input type="text" name="cldescr"></td>
        <td>No:<input type="radio" name="agelim" value="no" checked>
              Yes:<input type="radio" name="agelim" value="yes"></td>
        <td><input type="text" name="agereq" size="10"></td>
      </tr>
      <tr>
        <td colspan="5" align="center"><input type="submit" name="submit" value="submit"> &nbsp;
          <input type="reset"></td>
      </tr>
    </table>
  </fieldset>
  </form>
<?
}
?>
</body>
</html>
<?
// end of script
?>
TIA,
Pat :mrgreen:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

spelling mistake in the variable name

Code: Select all

$aglim1 = $HTTP_POST_VARS["agelim"];
Should be

Code: Select all

$agelim1 = $HTTP_POST_VARS["agelim"];
Mark
ThumbsUp
Forum Newbie
Posts: 4
Joined: Mon Feb 09, 2004 12:05 am

Post by ThumbsUp »

Hi Mark,

:oops: Well Geeze! Told ya my brain is fried. 8O

:oops: I tried to make more out of it than it actually ended up being. I was SURE there was some special thing I had to do (like in javascript) to grab that silly thing! :oops:

Thanks!
Pat :mrgreen:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

don't worry, it has happened to us all at some stage (apart from me) :roll:

Mark
Post Reply