www.webdeveloper.com
Results 1 to 7 of 7

Thread: Radio button selection based on mysql query result.

  1. #1
    Join Date
    Aug 2014
    Posts
    3

    Unhappy Radio button selection based on mysql query result.

    Hi,

    In my page i am displaying the record in a table and there is one edit button in table which will fill the Form on same page with selected table row, all the text boxes are populating after clicking the edit button in table but radio button option is not selected/checked.


    here is my code.
    Code:
    <?php
    if ($_POST["pagecount"]=="1")
    	{
    		include("class/mysql_crud.php");
            $db = new Database();
            $db->connect();
            $n = '99';
        $zoneid = '001';
        $stationid = '001001';
        $eqtype = 'TR';
        $eqid = 'TR9999';
    		if ($_POST["f_action"]=="Save")
    		{
    			if ($_POST["lblPK"]=="0")
    			{
    				$db->insert('EquipmentMaster',array('n'=>$n,'ZoneId'=>$zoneid,'StationId'=>$stationid,'EqType'=>$eqtype, 'EqId'=>$eqid,'Type'=>$_POST[rbTType],'SrNo'=>$_POST[txtSrNo],'Make'=>$_POST[txtMake], 'Model'=>$_POST[txtModel],'Power'=>$_POST[txtPower],'Frequency'=>$_POST[txtFrequency],'doc'=>$_POST[txtDOC], '_Status'=>$_POST[txtStatus]));  // Table name, column names and respective values
                    $res = $db->getResult();
                    //print_r($res);
                    //print("Insert");
    			}
    			else
    			{
    				$db->sql("UPDATE EquipmentMaster SET Type ='$_POST[rbTType]',SrNo='$_POST[txtSrno]',Make='$_POST[txtMake]', Model='$_POST[txtModel]',Power='$_POST[txtPower]', Frequency='$_POST[txtFrequency]',doc='$_POST[txtDOC]', _Status='$_POST[txtStatus]' where Id=$_POST[lblPK]");
                    $res = $db->getResult();
                    print_r($res);
                    //print("Update");
    			}
    		}
    		else
    		{
    			$db->sql("DELETE FROM EquipmentMaster where Id=$_POST[lblPK]");
                $res = $db->getResult();
    		}
    		//header('Location: '.$_SERVER['PHP_SELF']);
    die;
    	}
    else
    	{
    ?> 
    <head>
    <script language="JavaScript">
    function save()
    	{
    		if (document.frm.txtSrNo.value=="")
    		{
    			alert("SrNo must be filled.");
    			frm.txtSrNo.focus();
    			return false;
    		}
    		var confirm_del= confirm ("Do you want to save the data ?")
    		if (confirm_del)
    		{
    			document.frm.f_action.value="Save";
    			document.frm.action="Transmitter.php";
    			document.frm.method="post";
    			document.frm.submit();
    		}
    	}
    	function editrecord(i)
    	{
    		if(Number(frm.rcount.value)>1)
    		{
    			frm.lblPK.value=frm.hid_id[i].value;
    			frm.txtSrNo.value=frm.hid_SrNo[i].value;
                frm.rbTType.value=frm.hid_Type[i].value;
    			frm.txtMake.value=frm.hid_Make[i].value;
    			frm.txtModel.value=frm.hid_Model[i].value;
                frm.txtPower.value=frm.hid_Power[i].value;
    			//frm.txtTonnage.value=frm.hid_Tonnage[i].value;
    			frm.txtFrequency.value=frm.hid_Frequency[i].value;
    			frm.txtDOC.value=frm.hid_DOC[i].value;
    			frm.txtStatus.value=frm.hid_Status[i].value;
    		}
    		else
    		{
    			frm.lblPK.value=frm.hid_id.value;
    			frm.txtSrNo.value=frm.hid_SrNo.value;
    			frm.rbTType.value=frm.hid_Type.value;
    			frm.txtMake.value=frm.hid_Make.value;
    			frm.txtModel.value=frm.hid_Model.value;
                frm.txtPower.value=frm.hid_Power.value;
    			//frm.txtTonnage.value=frm.hid_Tonnage.value;
    			frm.txtFrequency.value=frm.hid_Frequency.value;
    			frm.txtDOC.value=frm.hid_DOC.value;
    			frm.txtStatus.value=frm.hid_Status.value;
    		}
    	}
    	function del_record()
    	{
    		if (document.frm.lblPK.value=="0")
    		{
    			alert("Nothing to delete.");
    			frm.txtSrNo.focus();
    			return false;
    		}
    		var confirm_del= confirm ("Do you want to really delete the data ?")
    		if (confirm_del)
    		{
    			document.frm.f_action.value="delete";
    			document.frm.action="Transmitter.php";
    			document.frm.method="post";
    			document.frm.submit();
    		}
    	}
    </script>
    </head>
    <body>
        <table width="100%" border="0">
      <tr>
        <td><a href="Transmitter.php" title="Transmitter" target="_self">Transmitter</a></td>
        <td>&nbsp;<a href="SE.php" title="Studio Equipment" target="_self">Studio Equipment</a></td>
        <td><a href="STL.php" title="STL Links" target="_self">STL Links</a></td>
        <td><a href="RNT.php" title="RN Terminal" target="_self">RN Terminal</a></td>
        <td><a href="OB.php" title="OB Equipment" target="_self">OB Equipment</a></td>
      </tr>
      <tr>
        <td><a href="DG.php" title="Generator" target="_self">Generator</a></td>
        <td><a href="AC.php" title="AC" target="_self">AC</a></td>
        <td><a href="AVR.php" title="AVR" target="_self">AVR</a></td>
        <td><a href="UPS.php" title="UPS" target="_self">UPS</a></td>
        <td><a href="PS.php" title="Power Supply" target="_self">Power Supply</a></td>
      </tr>
    </table>
    <br>
        <br>
    <form name="frm" method="post" action="Transmitter.php">
    <table width="100%" border="0">
    <tr>
    <td>
    <input type="hidden" value="1" name="pagecount">
    <input type="hidden" name="f_action">
    <input type="hidden" name="lblPK" value="0">
    </td>
    </tr>
        <tr>
        <td width="26%"><label>Select</label></td>
        <td width="74%">
          <input type="radio" name="rbTType" value="FM"/>FM</label>
            <input type="radio" name="rbTType" value="MW"/>MW</label>
            <input type="radio" name="rbTType" value="SW"/>SW</label>
        </td>
      </tr>
      <tr>
        <td width="26%"><label>SrNo</label></td>
        <td width="74%">
          <label for="txtSrNo"></label>
          <input type="text" name="txtSrNo" id="txtSrNo" />
        </td>
      </tr>
      <tr>
        <td>Make</td>
        <td><label for="txtMake"></label>
          <input type="text" name="txtMake" id="txtMake" />
          </td>
      </tr>
      <tr>
        <td>Model</td>
        <td><label for="txtModel"></label>
          <input type="text" name="txtModel" id="txtModel" />
          </td>
      </tr>
      <tr>
        <td>Power</td>
        <td><label for="txtPower"></label>
          <input type="text" name="txtPower" id="txtPower" />
          </td>
      </tr>
      <tr>
        <td>Frequency</td>
        <td><label for="txtFrequency"></label>
          <input type="text" name="txtFrequency" id="txtFrequency" />
          </td>
      </tr>
      <tr>
        <td>Commissioning Date</td>
        <td><label for="txtDOC"></label>
          <input type="text" name="txtDOC" id="txtDOC" />
          </td>
      </tr>
      <tr>
        <td>Status</td>
        <td><label for="txtStatus"></label>
          <input type="text" name="txtStatus" id="txtStatus" />
          </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
    	<input type="button" value="SAVE" onClick="save()">
    	<input type="button" value="DELETE" onClick="del_record()">
    	<input type="reset" value="RESET">
    	</td>
      </tr>
    </table>
    		<?php 
    		function get_rec()
    		{
                include('class/mysql_crud.php');
    $db = new Database();
    $db->connect();
    $db->sql("SELECT * FROM EquipmentMaster WHERE EqType = 'TR'"); // Table name, Column Names, WHERE conditions, ORDER BY conditions
    $res = $db->getResult();
    		echo "<table border='0' width='100%' align='center' CELLPADDING='0' CELLSPACING='0' bordercolor='#800000' style='BORDER-RIGHT: #800000 1px solid; BORDER-TOP: #800000 1px solid; BORDER-LEFT: #800000 1px solid; BORDER-BOTTOM: #800000 1px solid'>
    			<tr>
    				<td>
    					<table border='1' width='100%' align='center' CELLPADDING='0' CELLSPACING='0'>
    						<tr bgcolor='#800000'>
                            <td align='center'><font face='verdana' size='2' color='#ffffff'>Edit</font></td>
    							<td align='center'><font face='verdana' size='2' color='#ffffff'>SrNo</font></td>
                                <td align='center'><font face='verdana' size='2' color='#ffffff'>Type</font></td>
    							<td align='center'><font face='verdana' size='2' color='#ffffff'>Make</font></td>
    							<td align='center'><font face='verdana' size='2' color='#ffffff'>Model</font></td>
                                <td align='center'><font face='verdana' size='2' color='#ffffff'>Power</font></td>
    							<td align='center'><font face='verdana' size='2' color='#ffffff'>Frequency</font></td>
                                <td align='center'><font face='verdana' size='2' color='#ffffff'>Commissioning Date</font></td>
    							<td align='center'><font face='verdana' size='2' color='#ffffff'>Working Status</font></td>
    							
    						</tr>";
    						$i=0;
    						foreach($res as $row)
    						  {
    						  $i=$i+1;
    						  echo "<tr>";
    						  echo "<td><a href='javascript:editrecord(" . ($i-1) .")' style='color:#0000ff;TEXT-DECORATION:none;'>Edit</td>";
    						  echo "<input type=hidden name='hid_id' value='" . $row['Id'] ."'>";
    						  echo "<input type=hidden name='hid_SrNo' value='" . $row['SrNo'] ."'>";
                              echo "<input type=hidden name='hid_Type' value='" . $row['Type'] ."'>";
    						  echo "<input type=hidden name='hid_Make' value='" . $row['Make'] ."'>";
    						  echo "<input type=hidden name='hid_Model' value='" . $row['Model'] ."'>";
                              echo "<input type=hidden name='hid_Power' value='" . $row['Power'] ."'>";
    						  echo "<input type=hidden name='hid_Frequency' value='" . $row['Frequency'] ."'>";
    						  echo "<input type=hidden name='hid_DOC' value='" . $row['DOC'] ."'>";
                              echo "<input type=hidden name='hid_Status' value='" . $row['_Status'] ."'>";
    						  echo "<td>" . $row['SrNo'] . "</td>";
                              echo "<td>" . $row['Type'] . "</td>";
    						  echo "<td>" . $row['Make'] . "</td>";
    						  echo "<td>" . $row['Model'] . "</td>";
                              echo "<td>" . $row['Power'] . "</td>";
    						  echo "<td>" . $row['Frequency'] . "</td>";
                              echo "<td>" . $row['DOC'] . "</td>";
    						  echo "<td>" . $row['_Status'] . "</td>";
    						  echo "</tr>";
    						  //echo $i;
    						  }
    						  echo "<input type=hidden name=rcount value='" . $i ."'>";
    					echo "</table>";
    				echo "</td>";	
    			echo "</tr>";
    		}
    		get_rec(); 
    		?>
    	</form>
    </body>
    <?php
    }
    ?>

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    790
    Read up on the radio button syntax. Simply set that attribute for the button you want selected using your php code.

    [php]
    $radio_html='';
    $radios = array("PM","RM","XX");
    foreach ($radios as $btn)
    {
    $select = '';
    if ($row['sel_field'] == $btn)
    $select = 'selected';
    $radio_html .= "<input type='radio' name='radiobtn' value='$btn' $select>$btn<br>";
    }

    Be sure to echo $radio_html in the right place in your html output section.

    You might also read up on css usage to avoid ALL THAT REPEATED STYLING you are doing. What a mess! Did you copy this code from some outdated website?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  3. #3
    Join Date
    May 2014
    Posts
    1,020
    Why are you making it harder to maintain and develop by putting the static scripting in the markup? I don't know what you are using to wrap your database accesses, but it looks like a waste of memory as I suspect that $res is being filled with the complete result set. (try to avoid doing that! Pass the result handler, NOT the result!)... and that markup... This is 2014 not 1997, what's with the FONT tags, CELLPADDING, BORDER, ALIGN and TARGET attributes, and host of other things that are a decade and a half out of date? (and a guaranteed waste of bandwidth in the age of CSS!)

    On the PHP side why are you using multiple echo to do the job of one? Why do you have an outer table that will only ever have one TD (that's a DIV's job -- assuming there should even BE a wrapper). Where are your LABEL on the form? What makes that form tabular data?

    Why are you using target="_SELF" when that's the default behavior? Again, not that you should be using TARGET in the first place!

    Much less all the hidden ID that only seem to serve a purpose for scripting, in which case output them as scripting.

    Worse though, you seem to be setting up to output multiple rows. If in those rows they all get the same 'name' inside the same form, how is anything except the first row even supposed to work? I'd consider using (textContent || innerText) on the TD instead of using hidden inputs for that. Then it would be a simple dom-walk that you could easily automate.

    Basically, about a third to half your HTML belongs in the trash can as unneccessary bloat, and what you are trying to do with the scripting makes no sense for what your multiple rows of output is.

    You should probably also learn what THEAD, TBODY, CAPTION and TH are for, and how to use them.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    Aug 2014
    Posts
    3
    Sorry for the outdated style of coding...(I work in .net)
    I dont know the P of php its my first project on php, so i am just finding the matching code for my requirement and trying to apply it.

    Its difficult for me to now learn and do it in proper way, first i have to just finish the project and show that project is working as required.


    Hence you guyz are requseted to just guide me for the above radio button problem. (Exact code to fit in my page, if possible)

    Thanks for all ur effort to read this outdated style of coding.

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    790
    I gave you the exact code - just change the names to fit your project.

    And what exactly is this project? School work? Or 'real' work? If it's real work, then you may have misrepresented yourself to your employer. If it's school work, well, you're supposed to be learning so do some work and learn!
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  6. #6
    Join Date
    Aug 2014
    Posts
    3
    it will be helpful if you can provide a sample code to add, edit and delete data from mysql on same page in a single form and display the stored data in a grid with radio button for selection and the selected record get filled in the above form to edit or delete.

    I have searched for the above but couldn't find any tutorial or sample for that.

    and the above method that i tried is outdated and bulky.

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    790
    It seems that besides demonstrating your inability to write proper English, you are also letting us know that you don't have a clue as to how to write proper PHP either, and furthermore don't even want to bother to learn how to do it. Therefore I have no interest in any further conversation with you.

    This forum is about helping those with an interest in php programming. You seem to only have an interest in a goal, not learning how to get there. Too bad for you.

    Maybe you should concentrate on English first?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL | E_NOTICE);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles