469,352 Members | 1,656 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Clarification about Radio button in php

hiii
i have a form within which i have 3 radio button with same name but value will be retrieved from the mysql database.But each radio button will do different actions.can plz suggest me some way.
Aug 11 '06 #1
17 14071
ronverdonk
4,258 Expert 4TB
What are the fields fromMySql that you get the values from. In order to populate all three button texts with diferent values you'll have to have 3 fields in your data base. And is there a default value to be set to 'checked=checked'?

Ronald :cool:
Aug 11 '06 #2
i'm retrieving 3 fields(values) from mysql,for this,i'm having 3 radio buttons having same names,when i checked,each must do different operations,no default checking...but when i check ,all the 3 radio buttons doing same operation,how can i check...
Aug 11 '06 #3
ronverdonk
4,258 Expert 4TB
Please show the code you have until now regarding those radio buttons. It is somewhat difficult to react to this problem without seeing the code involved.

Ronald :cool:
Aug 11 '06 #4
<?php
mysql_connect("localhost","root","");
mysql_select_db("asc");
$q=mysql_query("select * from course");
print '<form action="a.php" method="get">';
while($q1=mysql_fetch_array($q))
{
print '<input type="radio" name="op">';
print $q1[name];
}

print '<input type="Submit">';
print '</form>';
?>

In this name is the only one field in the table "course."
And i have three values for this field in mysql database.so that three radio buttons will have the value from the table "course"which is in mysql database.
Now if i select different radio button it must do different actions.Since i have used while loop to get the value of radio button I have a problem in accessing each radio button.
Aug 12 '06 #5
ronverdonk
4,258 Expert 4TB
Now the following code works. What I did is assign the SQL row value from column 'name' to the 'value=' attribute of the <input and to display the same 'name' at the end of the <input statement, so it displays next to the radio button.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_GET['op'])) {
  3.   print_r($_GET);
  4.   die;
  5. }
  6. else {
  7.    mysql_connect("localhost","ronverdonk","ronnie09");
  8.    mysql_select_db("vwso");
  9.    $q=mysql_query("select * from course");
  10.    print '<form action="a.php" method="get">';
  11.    while($q1=mysql_fetch_array($q)) {
  12.       print "<input type='radio' name='op' value='".$q1['name']."'>".$q1['name']."<br />";
  13.    } 
  14.    print '<input type="Submit" value="Send it!">';
  15.    print '</form>';
  16. }
  17. ?> 
Ronald :cool:
Aug 12 '06 #6
Now the following code works. What I did is assign the SQL row value from column 'name' to the 'value=' attribute of the <input and to display the same 'name' at the end of the <input statement, so it displays next to the radio button.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (isset($_GET['op'])) {
  3.   print_r($_GET);
  4.   die;
  5. }
  6. else {
  7.    mysql_connect("localhost","ronverdonk","ronnie09");
  8.    mysql_select_db("vwso");
  9.    $q=mysql_query("select * from course");
  10.    print '<form action="a.php" method="get">';
  11.    while($q1=mysql_fetch_array($q)) {
  12.       print "<input type='radio' name='op' value='".$q1['name']."'>".$q1['name']."<br />";
  13.    } 
  14.    print '<input type="Submit" value="Send it!">';
  15.    print '</form>';
  16. }
  17. ?> 
Ronald :cool:

I am having "a.php" which must be called when submit button is clicked.And it must do the operation based on the radio button selected.
for eg:
if the values of radio buttons are:

Orientation programme
Refresher course
Need based course.
Which are retrieved from the database.

And if i select the radio button with value Orientation Programme the op1.php must be called.And if i select a radio button with value Refresher Course the rc1.php must be called etc.So how can i include that in the "a.php" coding


This is my coding for "a.php"

<?php
mysql_connect("localhost","root","") or die("connection errro");
mysql_select_db("asc") or die("Database Error");
$s=$_GET['op'];
$sel=mysql_query("select * from opgm") or die("Table Error");
if($_GET['op']) /*I struck here how to get the value of selected radio button value from check.php.*/
{
include("op1.php");
}
if($_GET['op'])
{
include("rc1.php");
}
?>


Thank u
Aug 13 '06 #7
ronverdonk
4,258 Expert 4TB
First, it is probably better to include the name of the routine, belonging to a particular value, e.g. rc1.php with Refresher Course, in the data base entry. That way it will be a lot easier to change and you don't have to hardcode anything in your code, but just pick the button codes and the routines from the database.

Anyway, the following is the code as it works. I have included A.PHP, RC1.PHP, OP1.PHP and QQ1.PHP. Also the setup of the table 'course' is in the a.php code.
Expand|Select|Wrap|Line Numbers
  1. A.PHP:
  2. <?php
  3. /*
  4.    THIS IS THE TEST TABLE:
  5.    create table course (id int(2) primary key auto_increment, name varchar(25));
  6.    insert course values (1,'Orientation Programme');
  7.    insert course values (2,'Refresher Course');
  8.    insert course values (3,'Need based course');  
  9. */
  10. if (isset($_GET['op'])) {
  11.   switch ($_GET['op']) {
  12.     case 'Orientation Programme':
  13.         header('Location: op1.php');
  14.         break;
  15.     case 'Refresher Course':
  16.         header('Location: rc1.php');
  17.         break;
  18.     case 'Need based course':
  19.         header('Location: qq1.php');
  20.         break;
  21.     default :
  22.         echo "You have selected an invalid value";
  23.         break;
  24.   } // end switch
  25. } // end if (isset
  26.  
  27. else {
  28.    mysql_connect("localhost","ronverdonk","ronnie09");
  29.    mysql_select_db("vwso");
  30.    $q=mysql_query("select * from course");
  31.    print '<form action="a.php" method="get">';
  32.    while($q1=mysql_fetch_array($q)) {
  33.       print "<input type='radio' name='op' value='".$q1['name']."'>".$q1['name']."<br />";
  34.    } 
  35.    print '<input type="Submit" value="Send it!">';
  36.    print '</form>';
  37. }
  38. ?>
  39. OP1.PHP:
  40. <?php
  41. echo 'You are in op1.php';
  42. exit;
  43. ?>
  44.  
  45. RC1.PHP:
  46. <?php
  47. echo 'You are in rc1.php';
  48. exit;
  49. ?>
  50.  
  51. QQ1.PHP:
  52. <?php
  53. echo 'You are in qq1.php';
  54. exit;
  55. ?>
Ronald :cool:
Aug 13 '06 #8
Thanks for your coding, it was very useful but in the switch case:

case 'orientation programme': //This should not be static it should be dynamic that is,the value of the radio button should be compared with the value in the database.
Aug 14 '06 #9
ronverdonk
4,258 Expert 4TB
First, it is probably better to include the name of the routine, belonging to a particular value, e.g. rc1.php with Refresher Course, in the data base entry. That way it will be a lot easier to change and you don't have to hardcode anything in your code, but just pick the button codes and the routines from the database.
Well, see my above note from yesterday. You know what to change now in you database and code. Good luck.

Ronald :cool:
Aug 14 '06 #10
Well, see my above note from yesterday. You know what to change now in you database and code. Good luck.

Ronald :cool:

Thank you for your quick reply.I have got it correct.
Aug 16 '06 #11
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Login Page</title>
<LINK media=all
href="script/align.css" type=text/css
rel=stylesheet>
<script type="text/javascript" language="JavaScript1.2" src="script/stmenu.js"></script>
<style type="text/css">
.ds_box {
background-color: #666600;
border: 1px solid #000;
position: absolute;
z-index: 32767;
}
</style>
<script language="javascript">
function valid()
{
if(document.regform.ugc.value=="")
{
alert("Please Enter Your ugc");
document.regform.ugc.focus();
return false;
}

if(document.regform.name.value=="")
{
alert("Please Enter Your Name");
document.regform.name.focus();
return false;
}
if(document.regform.desg.value=="")
{
alert("Please Enter Your Designation.");
document.regform.desg.focus();
return false;
}

if(document.regform.raddr.value=="")
{
alert("Please Enter Your Residential Address");
document.regform.raddr.focus();
return false;
}
if(document.regform.Acad.value=="")
{
alert("Please Enter Your Academic Qualifications");
document.regform.Acad.focus();
return false;
}
if(document.regform.clsper.value=="")
{
alert("Please Enter Your Class & Percentage");
document.regform.clsper.focus();
return false;
}
if(document.regform.univ.value=="")
{
alert("Please Enter Your University");
document.regform.univ.focus();
return false;
}
if(document.regform.totex.value!="")
{
var a=document.regform.totex.value;
var charset="1234567890";
var len=a.length;
for(var i=0;i<len;i++)
{
var c=a.charAt(i);
var pos=charset.indexOf(c);
if(pos==-1)
{
alert("Enter your total experience correctly");
document.regform.totex.focus();
document.regform.totex.value="";
return false;
break;
}
}
}
if(document.regform.declar.checked==false)
{
alert("Agree the Declare Statement To Submit Your Form");
document.regform.declar.focus();
return false;
}
}
</script>
</HEAD>
<BODY bgcolor="#E7DEA3">
</head>
<td width="773" align="center" valign="top" ><div id="centercol">

<p align="Justify"><b> <font face="Arial, Helvetica, sans-serif" color="#0B350B" size="3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;</font></b>
<form action="1.php" method="post" name="regform" id="regform" >
<br>
<table width="100%" border="2" bordercolor="#666600">
<caption>
<span class="style1"> Registration </span>
</caption>
<tr bordercolor="#666600">
<td width="410"><span class="style19">Application for the UGC Sponsored Orientation Course / Refresher Course in <span class="style21">*</span></span></td>
<td width="330"><p>
<?php
mysql_connect("localhost","root","");
mysql_query("CREATE DATABASE `aaa`") ;
mysql_select_db("aaa");
mysql_query("TRUNCATE TABLE `course` ");
mysql_query("CREATE TABLE `course` (`name` VARCHAR( 100 ) NOT NULL ) TYPE = MYISAM");
mysql_query("INSERT INTO `course` ( `name` ) VALUES ('xyz'), ('abc'), ('ijk')");
$q=mysql_query("select * from course");
print '<p class="style19">';
while($q1=mysql_fetch_array($q))
{
print "<input type='radio' name='op' value='".$q1['name']."'>".$q1['name']; /*this displays the radio button according to the number of records in the database and if the first radio buttonn is checked then only the first list box(in preffered Course) must be enabled similarly for other radio buttons also*/
echo"</br>";
echo"</br>";
}
print '</p>';
?>
</p>
</td>
</tr>
</table>
<table width="100%" height ="613" border="2" bordercolor ="#666600">
<tr>
<td width="55%" bordercolo r="#666600"><span class="style19">Name of the Applicant (in Capital letters) <span class="style21">*</span></span></td>
<td width="45%" bordercolor ="#666600"><input name="name" type="text" id="name">
</td>
</tr>
<tr>
<td bordercolor = "#666600"><span class="style19">Designation<span class="style21"> *</span></span></td>
<td bordercolor = "#666600"> <input name="desg" type="text" id="desg"> </td>
</tr>
<tr>
<td bordercolor = "#666600"><span class="style19">College</span></td>
<td bordercolor = "#666600"> <input name="col" type="text" id="col"> </td>
</tr>
<tr>
<td bordercolor = "#666600"><span class="style19">University</span></td>
<td bordercolor = "#666600"> <input name="univ1" type="text" id="univ1"> </td>
</tr>
<tr>
<td bordercolor = "#666600"><span class="style19">Official Address with Pincode &amp; Phone number </span></td>
<td bordercolor ="#666600"> <input name="oaddr" type="text" id="oaddr"> </td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Residential Address with Pincode &amp; Phone number <span class="style21">*</span></span></td>
<td bordercolor ="#666600"><input name="raddr" type="text" id="raddr"> </td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Sex</span></td>
<td bordercolor ="#666600"><span class="style19">
<select name="sex" id="sex">
<option>Male</option>
<option>Female</option>
</select>
</span> </td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Whether belongs to SC / ST </span></td>
<td bordercolor ="#666600"><span class="style19">
<select name="scst" id="scst">
<option>Yes</option>
<option>No</option>
</select>
</span> </td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Date of Birth </span></td>
<td bordercolor ="#666600"><input name="dob" type="text" id="dob" onFocus="ds_sh(this)"></td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Academic Qualifications <span class="style21">*</span></span></td>
<td bordercolor ="#666600"><input name="Acad" type="text" id="Acad"></td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Class / Percentage of Marks <span class="style21">*</span></span></td>
<td bordercolor ="#666600"> <input name="clsper" type="text" id="clsper"> </td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Year of Passing <span class="style21">*</span></span></td>
<td bordercolor ="#666600"> <select name="yop" id="yop">
<?php
for($i=1970;$i<=2006;$i++)
{
print '<option value="';
print $i;
print '">';
print $i;
print '</option>';
}
?>
</select></td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">University <span class="style21">*</span></span></td>
<td bordercolor ="#666600"><input name="univ" type="text" id="univ">
</td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">Service details </span></td>
<td bordercolor ="#666600"><span class="style19">Teaching Experience </span></td>
</tr>
<tr>
<td height="25" bordercolor ="#666600"><span class="style19">a) Date of First Appointment </span></td>
<td bordercolor ="#666600"><input name="dfa" type="text" id="dfa" onFocus="ds_sh(this)"></td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">b) Date of Entry to Continuous Service </span></td>
<td bordercolor ="#666600"><input name="decs" type="text" id="decs" onFocus="ds_sh(this)"></td>
</tr>
<tr>
<td bordercolor ="#666600"><span class="style19">c) Total Experience </span></td>
<td bordercolor ="#666600">
<input name="totex" type="text" id="totex" maxlength="2">
</td>
</tr>



<tr>
<td colspan="2" bordercolor ="#666600"><p align="center" class="style19">Preffered Course <span class="style21">*</span></p>
<p align="center">
<?php
mysql_connect("localhost","root","");
mysql_select_db("asc");
$q=mysql_query("select * from opgm");
print '<select name="op">';
while($q1=mysql_fetch_array($q))
{

print '<option value="';
print '">';
print $q1['cname'];
print '</option>';
}
print '</select>';
echo"<br/>";
echo"<br/>";

$q=mysql_query("select * from rc");
print '<select name="rc">';
while($q1=mysql_fetch_array($q))
{

print '<option value="';
print '">';
print $q1['cname'];
print '</option>';
}
print '</select>';
echo"<br/>";
echo"<br/>";

$q=mysql_query("select * from nbc");
print '<select name="nbc">';
while($q1=mysql_fetch_array($q))
{

print '<option value="';
print '">';
print $q1['cname'];
print '</option>';
}
print '</select>';
echo"<br/>";
echo"<br/>";


?>
</p> </td>
</tr>


<td height="2" colspan="2" bordercolor="#666600">
</table>
<input type="submit" name="Submit" value="Submit" onClick="return valid()">
</form>
<p align="Justify">
</div>
</BODY>
</HTML>



/*In the above php file the "Application for the UGC Sponsored Orientation Course / Refresher Course in " field , i have retrieved the value for radio buttons from the database and if the first radio buttonn is checked then only the first list box(in preffered Course at the bottom of the form ) must be enabled and similarly for second only the second list box must be enabled.i dont know how to access this.
Shall i use scripting to do this or is there some other way to do this

can you suggest this plz :p*/
Aug 18 '06 #12
ronverdonk
4,258 Expert 4TB
When you use radio buttons, you can only click one of them and thus, you only have to verify one of the (in this case) 3 possible values.

If you want more boxes to check, you just use check-boxes, they can have different box-names and values.

So what is it you want: just 1 click for a set of buttons or multiple clicks for check-boxes?

Ronald :cool:
Aug 18 '06 #13
No its radio buttons.Only one must be checked at a time and accordingly the list menu(in preffered Course) below must be disabled or enabled.

Thank u
Aug 19 '06 #14
ronverdonk
4,258 Expert 4TB
I don't quite understand the meaning of your last entry. Does it mean that you have completed you form or is there still something you need to do and want help?

Ronald :cool:
Aug 20 '06 #15
sorry for the inconvenience.

I wanna a help.

when i execute the above php file which i sent on 18th august

In that first field, that is "Application for the UGC Sponsored Orientation Course / Refresher Course in *" has radio buttons whose values are retrieved from the database.

Suppose for eg:

if i have three values in the database then it will be showing three radio buttons(in the first field of the form) in which the user has to select any one of them.

here the problem arises

In the same form (at the bottom) i have a field named "Preffered Course *" whose value has been retrived from database. (Suppose i have three values in the database it will show three list boxes corresponding to each value)

So if i select the first radio button (at the top of the form) then in the Preffered Course field the first list box must be enabled and second, third listbox must be disabled. Suppose if i select the second radio button then the second list box must be enabled and first ,third list box must be disabled. Similarly if i select the third radio button then the first,second listbox must be disabled.

like to hear from you soon.
Aug 21 '06 #16
ronverdonk
4,258 Expert 4TB
I think you need JavaScript to accomplish this. Best sources I could find about populating drop-down boxes are:

http://javascript.internet.com/forms...opulation.html
http://search.internet.com/www.javas...ms.com&squery=

Keep cool!

Ronald :cool:
Aug 21 '06 #17
Well, see my above note from yesterday. You know what to change now in you database and code. Good luck.

Ronald :cool:
hi I have a problem, i have a page on which i m fetching the data from data base i want that the content on the page should remain untill the user logout from that how can i do it?
plz send the code to me.



thanx
Sep 12 '06 #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Oscar Monteiro | last post: by
5 posts views Thread by Digital Puer | last post: by
3 posts views Thread by John Davis | last post: by
8 posts views Thread by David Cameron | last post: by
9 posts views Thread by IchBin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.