Clarification about Radio button in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hemashiki
    New Member
    • Aug 2006
    • 34

    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.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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=checke d'?

    Ronald :cool:

    Comment

    • hemashiki
      New Member
      • Aug 2006
      • 34

      #3
      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...

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        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:

        Comment

        • hemashiki
          New Member
          • Aug 2006
          • 34

          #5
          <?php
          mysql_connect(" localhost","roo t","");
          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"whi ch 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.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #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.
            Code:
            <?php
            if (isset($_GET['op'])) {
              print_r($_GET);
              die;
            }
            else {
               mysql_connect("localhost","ronverdonk","ronnie09");
               mysql_select_db("vwso");
               $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' value='".$q1['name']."'>".$q1['name']."<br />";
               } 
               print '<input type="Submit" value="Send it!">';
               print '</form>';
            }
            ?>
            Ronald :cool:

            Comment

            • hemashiki
              New Member
              • Aug 2006
              • 34

              #7
              Originally posted by ronverdonk
              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.
              Code:
              <?php
              if (isset($_GET['op'])) {
                print_r($_GET);
                die;
              }
              else {
                 mysql_connect("localhost","ronverdonk","ronnie09");
                 mysql_select_db("vwso");
                 $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' value='".$q1['name']."'>".$q1['name']."<br />";
                 } 
                 print '<input type="Submit" value="Send it!">';
                 print '</form>';
              }
              ?>
              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","roo t","") or die("connection errro");
              mysql_select_db ("asc") or die("Database Error");
              $s=$_GET['op'];
              $sel=mysql_quer y("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.ph p");
              }
              if($_GET['op'])
              {
              include("rc1.ph p");
              }
              ?>


              Thank u

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                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.
                Code:
                A.PHP:
                <?php
                /*
                   THIS IS THE TEST TABLE:
                   create table course (id int(2) primary key auto_increment, name varchar(25));
                   insert course values (1,'Orientation Programme');
                   insert course values (2,'Refresher Course');
                   insert course values (3,'Need based course');  
                */
                if (isset($_GET['op'])) {
                  switch ($_GET['op']) {
                    case 'Orientation Programme':
                	    header('Location: op1.php');
                		break;
                    case 'Refresher Course':
                	    header('Location: rc1.php');
                		break;
                    case 'Need based course':
                	    header('Location: qq1.php');
                		break;
                	default :
                	    echo "You have selected an invalid value";
                		break;
                  } // end switch
                } // end if (isset
                
                else {
                   mysql_connect("localhost","ronverdonk","ronnie09");
                   mysql_select_db("vwso");
                   $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' value='".$q1['name']."'>".$q1['name']."<br />";
                   } 
                   print '<input type="Submit" value="Send it!">';
                   print '</form>';
                }
                ?>
                OP1.PHP:
                <?php
                echo 'You are in op1.php';
                exit;
                ?>
                
                RC1.PHP:
                <?php
                echo 'You are in rc1.php';
                exit;
                ?>
                
                QQ1.PHP:
                <?php
                echo 'You are in qq1.php';
                exit;
                ?>
                Ronald :cool:

                Comment

                • hemashiki
                  New Member
                  • Aug 2006
                  • 34

                  #9
                  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.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    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:

                    Comment

                    • hemashiki
                      New Member
                      • Aug 2006
                      • 34

                      #11
                      Originally posted by ronverdonk
                      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.

                      Comment

                      • hemashiki
                        New Member
                        • Aug 2006
                        • 34

                        #12
                        <!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="JavaS cript1.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="javas cript">
                        function valid()
                        {
                        if(document.reg form.ugc.value= ="")
                        {
                        alert("Please Enter Your ugc");
                        document.regfor m.ugc.focus();
                        return false;
                        }

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

                        if(document.reg form.raddr.valu e=="")
                        {
                        alert("Please Enter Your Residential Address");
                        document.regfor m.raddr.focus() ;
                        return false;
                        }
                        if(document.reg form.Acad.value =="")
                        {
                        alert("Please Enter Your Academic Qualifications" );
                        document.regfor m.Acad.focus();
                        return false;
                        }
                        if(document.reg form.clsper.val ue=="")
                        {
                        alert("Please Enter Your Class & Percentage");
                        document.regfor m.clsper.focus( );
                        return false;
                        }
                        if(document.reg form.univ.value =="")
                        {
                        alert("Please Enter Your University");
                        document.regfor m.univ.focus();
                        return false;
                        }
                        if(document.reg form.totex.valu e!="")
                        {
                        var a=document.regf orm.totex.value ;
                        var charset="123456 7890";
                        var len=a.length;
                        for(var i=0;i<len;i++)
                        {
                        var c=a.charAt(i);
                        var pos=charset.ind exOf(c);
                        if(pos==-1)
                        {
                        alert("Enter your total experience correctly");
                        document.regfor m.totex.focus() ;
                        document.regfor m.totex.value=" ";
                        return false;
                        break;
                        }
                        }
                        }
                        if(document.reg form.declar.che cked==false)
                        {
                        alert("Agree the Declare Statement To Submit Your Form");
                        document.regfor m.declar.focus( );
                        return false;
                        }
                        }
                        </script>
                        </HEAD>
                        <BODY bgcolor="#E7DEA 3">
                        </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;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp; </font></b>
                        <form action="1.php" method="post" name="regform" id="regform" >
                        <br>
                        <table width="100%" border="2" bordercolor="#6 66600">
                        <caption>
                        <span class="style1"> Registration </span>
                        </caption>
                        <tr bordercolor="#6 66600">
                        <td width="410"><sp an 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","roo t","");
                        mysql_query("CR EATE DATABASE `aaa`") ;
                        mysql_select_db ("aaa");
                        mysql_query("TR UNCATE TABLE `course` ");
                        mysql_query("CR EATE TABLE `course` (`name` VARCHAR( 100 ) NOT NULL ) TYPE = MYISAM");
                        mysql_query("IN SERT 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"><sp an class="style19" >Name of the Applicant (in Capital letters) <span class="style21" >*</span></span></td>
                        <td width="45%" bordercolor ="#666600"><inp ut name="name" type="text" id="name">
                        </td>
                        </tr>
                        <tr>
                        <td bordercolor = "#666600">< span class="style19" >Designation<sp an 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"><spa n class="style19" >Residential Address with Pincode &amp; Phone number <span class="style21" >*</span></span></td>
                        <td bordercolor ="#666600"><inp ut name="raddr" type="text" id="raddr"> </td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n class="style19" >Sex</span></td>
                        <td bordercolor ="#666600"><spa n class="style19" >
                        <select name="sex" id="sex">
                        <option>Male</option>
                        <option>Femal e</option>
                        </select>
                        </span> </td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n class="style19" >Whether belongs to SC / ST </span></td>
                        <td bordercolor ="#666600"><spa n class="style19" >
                        <select name="scst" id="scst">
                        <option>Yes</option>
                        <option>No</option>
                        </select>
                        </span> </td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n class="style19" >Date of Birth </span></td>
                        <td bordercolor ="#666600"><inp ut name="dob" type="text" id="dob" onFocus="ds_sh( this)"></td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n class="style19" >Academic Qualifications <span class="style21" >*</span></span></td>
                        <td bordercolor ="#666600"><inp ut name="Acad" type="text" id="Acad"></td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n 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"><spa n 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"><spa n class="style19" >University <span class="style21" >*</span></span></td>
                        <td bordercolor ="#666600"><inp ut name="univ" type="text" id="univ">
                        </td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n class="style19" >Service details </span></td>
                        <td bordercolor ="#666600"><spa n class="style19" >Teaching Experience </span></td>
                        </tr>
                        <tr>
                        <td height="25" bordercolor ="#666600"><spa n class="style19" >a) Date of First Appointment </span></td>
                        <td bordercolor ="#666600"><inp ut name="dfa" type="text" id="dfa" onFocus="ds_sh( this)"></td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n class="style19" >b) Date of Entry to Continuous Service </span></td>
                        <td bordercolor ="#666600"><inp ut name="decs" type="text" id="decs" onFocus="ds_sh( this)"></td>
                        </tr>
                        <tr>
                        <td bordercolor ="#666600"><spa n 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","roo t","");
                        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="#6 66600">
                        </table>
                        <input type="submit" name="Submit" value="Submit" onClick="return valid()">
                        </form>
                        <p align="Justify" >
                        </div>
                        </BODY>
                        </HTML>



                        /*In the above php file the "Applicatio n 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*/

                        Comment

                        • ronverdonk
                          Recognized Expert Specialist
                          • Jul 2006
                          • 4259

                          #13
                          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:

                          Comment

                          • hemashiki
                            New Member
                            • Aug 2006
                            • 34

                            #14
                            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

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              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:

                              Comment

                              Working...