Jump to content


Photo

Radio button in PHP


  • Please log in to reply
2 replies to this topic

#1 pravonline

pravonline
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 18 May 2006 - 10:40 AM

Dear All,

My requirement is to list out the databases from our localhost server along with a radio button for each database, I need to allow the user to select any one database through radiobutton and the selected database name has to be passed on to the next php script, since i'm new to this PHP environment, need your guidance please, Thanks in advance,

The sample code is as follows:

<html>

 <head>
  <title>PHP Test</title>
 </head>

<body> 

<?php
require_once 'DB.php'; // must be included in any script that uses PEAR::DB
$db_host    = "localhost";    // hostname of the database server
$db_user    = "root";           // database user's username
$db_pass    = "";        // database user's password, nothing by default
//$db_name    = "economy";    // the name of the database to connect to
$db_type    = "mysql";          // the type of database server.


// your data source name string. This contains the connection
// information for your database.
$dsn = "$db_type://$db_user:$db_pass@$db_host/$db_name";

// creates a database connection object or a database error
// object based on the success of the database connection.
$db = DB::connect($dsn, TRUE);

// if an error was encountered, the script exits with an error message
if (DB::isError($db)) {
    die($db->getMessage());
}
$tabelle = $cricket."cpu"; 

// SQL query that you wish to use to query the database
$sql1 = "SHOW DATABASES";
//$sql = "SELECT * from cricket";

if (!$result = MYSQL_QUERY($sql1)) 
    {print ERR_SQL.$tabelle; exit;} 
else 
    {if (mysql_num_rows($result) > 0) 
        {while ($row=mysql_fetch_row($result)) 
          {
            foreach ($row as $col=>$val)
            foreach ($row as $col=>$val2)
               { 

?>

<P><input type=radio name="locf" VALUE="$val">




<?php

print $val;
               }
          }
        }
      else 
      {print ERR_NULL.$tabelle; exit;} 
      } 


?>

<form action="subpgm.php" method="post">
<P><INPUT TYPE=submit VALUE="Validate it!"></P>
</form>

</body>

</html>

In the above program, i was supposed to get the selected database name in the variable called "locf", but no value is storing in that variable, can anyone throw some light on that, Thank you

#2 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 18 May 2006 - 11:45 AM

As for as i see... there are a couple of issues with this code

1) the form tag should be before radio button. it should be after the body tag,

2) <input type=radio name="locf" VALUE="$val"> - in this it should be

<input type=radio name="locf" VALUE="<?php echo $val;?>">

hth

Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#3 pravonline

pravonline
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 May 2006 - 05:46 AM

Dear Samshel,

Thanks for your comments, I reworked the coding as per your advice and it worked perfectly, Thanks to you once again,

Regards,
Praveen




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users