Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

Adding an Edit Record Radio Button for displayed results

Hi everyone. Guys im working on a page called:  showtasks_select_v4.php

It currently does the following:
- Has a select box that gets its data from a mysql table
- Has a table that displays results according to what has been selected from the select box.

What id like to add is the following...
- An Edit radio button column for the tabled results
- Upon selecting a record in the displayed table results, and clicking an "Edit this record" button,
that record is displayed in another table on the same page as the results, and is available to be edited and saved back to the database.

I have attached a printscreen of before and afters for you guys to see, plus all code for this little project.
There is only 1 php file for this...which is:  showtasks_select_v4.php
And 2 tables for mysql....       projects_pro            currenttasks_ctk

Any help greatly appreciated.
=========================================== showtasks_select_v4.php
<head>
<link rel="stylesheet" media="all" type="text/css" href="tasks.css" />
<script src="sorttable.js" type="text/javascript"></script>
 
<?php
include ("connectiondb.php");
$tbl_tasks="currenttasks_ctk";
$tbl_projects="projects_pro";
// The below filters correctly.
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro ='mcaffee_antivirus'";   
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro = ";   
$grabid = $_POST[projectid];
$sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid'";
 
// $sql2="SELECT DISTINCT name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro";
$sql2="SELECT id_pro,name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro GROUP BY name_pro";
 
$result=mysql_query($sql);
$result2=mysql_query($sql2);
 // $res = SELECT * FROM table WHERE `location` = '".$_GET['form_location']."';  
?>
 
</head>
 
<body>
	
<form name="formcombo" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
           <label for="project" class="formlabels" style="margin-top:2%;">Project:</label>                        
<select name="projectid" id="projectid">
                 
                 <?php 
                        while($row2=mysql_fetch_array($result2)) {
                            if ($_POST[projectid] == $row2['id_pro'])
                                 $sel = "selected";
                            else
                                 $sel = "";
                             echo "<option value=\"".$row2['id_pro']."\" $sel>".$row2['name_pro']."\n";      
                       }
                 ?>
           </select>
 
<input type="submit" name="Submit" value="Show Tasks Belonging to this Project"><br>
</form>            
          
     
<?php
// print_r($_POST);
// echo $_POST['projectid'];
$grab = $_POST['projectid'];
// echo $grab;
?>
 
 
 
<div id="tablewrap">
	<table class="sortable" style="width:700px;">
 
		<thead>
		<tr>
			<td><b>ID</b></td>
			<td><b>Task Name</b></td>
			<td><b>Project</b></td>
			<td><b>Description</b></td>
		</tr>		
	</thead>
	<tbody>	
		<?php while($rows=mysql_fetch_array($result)){ ?>
		<tr>
			<td><?php echo $rows['id_ctk']; ?></td>
			<td><?php echo $rows['name_ctk']; ?></td>
			<td><?php echo $rows['name_pro']; ?></td>
			<td style="width:400px;"><?php echo $rows['description_ctk']; ?></td>
		</tr> <?php } ?>		
	</tbody>
	<tfoot>
	</tfoot>
	</table>
</div>
 
</body>
 
 
================================================  currenttasks_ctk:
create table currenttasks_ctk (
id_ctk int(4) not null auto_increment,
name_ctk varchar(65) not null,
fk_id_pro_ctk int(4) not null,
description_ctk text,
foreign key (fk_id_pro_ctk) references projects_pro (id_pro),
primary key (id_ctk, fk_id_pro_ctk),
unique (name_ctk)
)
engine=innodb;
 
============================================ projects_pro:
create table projects_pro (
id_pro int(4) not null auto_increment,
name_pro varchar(65) not null,
primary key (id_pro),
unique (name_pro)
)
engine=innodb;

Open in new window

ee-edit.jpg
ASKER CERTIFIED SOLUTION
Avatar of EMB01
EMB01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon336697

ASKER

Hi EMB01,
Thanks for that...will try now ...... sorry for the delay :>)
No worries, let me know if you need any help.
Hi EMB01..... I have added the Edit Radio button....to the right, and added the ID of the $row to it.
What im not sure now is how i display the selected record.

I now have 3 forms on the page.

1) For the select box
2) For the Edit button (which I have placed in the header row section of the table)
3) For the radio buttons in the table itself


For the 3rd one, I dont have a submit input element.

Here is the code for the 3rd one...

<form name="formedit" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="radio" name="editme" value="<?php echo $rows['id_ctk']; ?>">            
</form>

Im trying to retrieve the value for the radio button selected with the following, which i cant get.

$grabradiotask = $_POST['editme'];

My complete code so far is in the snippet...
============================================ showtasks_select_v4.php
<head>
<link rel="stylesheet" media="all" type="text/css" href="tasks.css" />
<script src="sorttable.js" type="text/javascript"></script>
 
<?php
include ("connectiondb.php");
$tbl_tasks="currenttasks_ctk";
$tbl_projects="projects_pro";
// The below filters correctly.
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro ='mcaffee_antivirus'";   
// $sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro and name_pro = ";   
$grabid = $_POST[projectid];
$sql="SELECT * FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro AND id_pro = '$grabid'";
 
// $sql2="SELECT DISTINCT name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro";
$sql2="SELECT id_pro,name_pro FROM `$tbl_tasks`, `$tbl_projects` where fk_id_pro_ctk = id_pro GROUP BY name_pro";
 
$result=mysql_query($sql);
$result2=mysql_query($sql2);
 // $res = SELECT * FROM table WHERE `location` = '".$_GET['form_location']."';  
 
 
$grabradiotask = $_POST['editme'];
 
 echo "grabradiotask is: " . $grabradiotask;
 
?>
 
</head>
 
<body>
 
 
<!-- .................................................. wrap_formcombo_tablewrap............... -->	
	<div id="wrap_formcombo_tablewrap" style="position:absolute; left:5%;top:3%;float:left;">
 
<!-- ................................................................ formcombo ............... -->	
<form name="formcombo" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
           <label for="project" class="formlabels" style="margin-top:2%;">Project:</label>                        
<select name="projectid" id="projectid">
                 
                 <?php 
                        while($row2=mysql_fetch_array($result2)) {
                            if ($_POST[projectid] == $row2['id_pro'])
                                 $sel = "selected";
                            else
                                 $sel = "";
                             echo "<option value=\"".$row2['id_pro']."\" $sel>".$row2['name_pro']."\n";      
                       }
                 ?>
           </select>
 
<input type="submit" name="Submit" value="Show Tasks for Project"><br>
</form>            
          
     
<?php
print_r($_POST);
// echo $_POST['projectid'];
$grab = $_POST['projectid'];
// echo $grab;
?>
 
 
<!-- ............................................................ results table ............... -->
<div id="tablewrap">
	<table class="sortable" style="width:700px;">
 
		<thead>
			<tr>
				<td><b>ID</b></td>
				<td><b>Task Name</b></td>
				<td><b>Project</b></td>
				<td><b>Description</b></td>
				<td>
					<form name="formeditrecord" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
						<input type="submit" name="Submit" value="Edit">			
					</form>
				</td>
			</tr>		
			</thead>
	<tbody>	
		<?php while($rows=mysql_fetch_array($result)){ ?>
		<tr>
			<td><?php echo $rows['id_ctk']; ?></td>
			<td><?php echo $rows['name_ctk']; ?></td>
			<td><?php echo $rows['name_pro']; ?></td>
			<td style="width:400px;"><?php echo $rows['description_ctk']; ?></td>
			<td>
				<form name="formedit" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
					<input type="radio" name="editme" value="<?php echo $rows['id_ctk']; ?>">				
				</form>
			</td>			
		</tr> <?php } ?>		
	</tbody>
	<tfoot>
	</tfoot>
	</table>
</div> <!-- end of tablewrap div -->
<!-- .............................................................. results table ............... -->
 
</div> <!-- end of wrap_formcombo_tablewrap div-->
 
 
 
</body>

Open in new window

Hi EMB01,
Bud here is a quick screen dump of what it currently looks like...
All works except that I cannot display the selected record with the Edit button .
ee-q.jpg
EM,
Another screenshot mate :>)
ee-q2.jpg
When adding an input for submit, i get a value for grabradiotask, but check the screen dump mate :>)
ee-q3.jpg
EM,
Mate do you want me to close this question and open another one bud?
No you don't have to open up a new one. I can try to get the radio button method to work if you want. But it would be basically the same and a lot easier to just make the radio buttons submit buttons, then on submit register the $row_['ID'] to the form (below) on that same page. What do you want to do?
If you want to do it in two steps, as you have it, just make a form on that page and make your SQL UPDATE query use the value of $grabradiotask. I recommend that you use just one step, replace the radio button with a submit button and capture the value that way. Then, use the same method for updating the SQL. Let me know which way you want to go and if you need any help.
If you want to use radio buttons, all you need to do is this:
Change your radio button HTML to <input type="radio" name="radio" value="$row['ID']">

Then, add this PHP code in your <head>:
<?php
if(isset($radio))
{
  $id = $radio;
} else {
  $id = 0;
}
?>

Finally, make the form below update using the ID of $radio. If you are using a processing script on another page, your update form would look like this:
<form name="myform" action="/processing_script.php?<?php echo $id; ?>" method="POST">

Just let me know what you are trying to do. If you want to open a new question for this, let me know where it is so I can help!