I have tried to submit my form which is having two different links leading to the delete and edit php files but i noticed that the links do not submit the value of the radio button to php files especially the delete php file. But when i changed the links to buttons the value was submitted. Please i do not want to use the button tag, how can i make the value get across to the php files with with my links?
Here is the form:
<form action="del_cn_rec.php" method="POST" id="consignments" >
<table id="example1" class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Consignment No</th>
<th>Sender</th>
<th>Reciever</th>
<th>Pickup Date/Time</th>
<th>Msg_Status</th>
<th>Status</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sqlc="SELECT * FROM consignments";
$resultc= mysql_query($sqlc) or die(mysql_error());
while($rwsc= mysql_fetch_array($resultc)){
echo "<tr>";
echo "<td><input type='radio' class='flat-red' name='cn_id' value=".$rwsc[0];
echo " /></td>";
echo "<td>".$rwsc[1]."</td>";
echo "<td>".$rwsc[16]." ".$rwsc[17]."</td>";
echo "<td>".$rwsc[21]." ".$rwsc[22]."</td>";
echo "<td>".$rwsc[6]." ".$rwsc[7]."</td>";
$sqli='SELECT * FROM admin_inbox WHERE cn="'.$rwsc[1].'"';
$resulti= mysql_query($sqli);
$rwsi= mysql_fetch_array($resulti);
$mstatus= $rwsi[4];
if("$mstatus" === "Unreplied"){
$moutput = "<span class='label label-danger'><i class='fa fa-circle'></i> Unreplied</span>";
}
if("$mstatus" === "Replied"){
$moutput = "<span class='label label-success'><i class='fa fa-check-circle'></i> Replied</span>";
}
if("$mstatus" === ""){
$moutput = "<span class='label label-warning'><i class='fa fa-circle-o'></i> No Message</span>";
}
echo "<td>".$moutput."</td>";
$status= $rwsc[9];
if("$status" === "Delivered"){
$output = "<span class='label label-success'><i class='fa fa-check-square-o'></i> Delivered</span>";
}
if("$status" === "On Hold"){
$output = "<span class='label label-danger'><i class='fa fa-hand-stop-o'></i> On Hold</span>";
}
if("$status" === "Arrived"){
$output = "<span class='label label-primary'><i class='fa fa-motorcycle'></i> Arrived</span>";
}
if("$status" === "In Transit"){
$output = "<span class='label label-warning'><i class='fa fa-truck'></i> In Transit</span>";
}
echo "<td>".$output."</td>";
echo "<td><span class=\"label label-primary\"><i class=\"fa fa-edit\"></i> Edit Rec.</span></td>";
echo "<td><span class=\"label label-danger\"><i class=\"fa fa-times\"></i> Delete Rec.</span></td>";
echo "</tr>";}
?>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Consignment No</th>
<th>Sender</th>
<th>Reciever</th>
<th>Pickup Date/Time</th>
<th>Msg_Status</th>
<th>Status</th>
<th>Action</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</form>
</div>
<script>
form=document.getElementById("consignments");
function askForEdit_Rec() {
form.action = <?php echo json_encode($urlc); ?>;
form.submit();
}
</script><script>
form=document.getElementById("consignments");
function askForDelete_Rec() {
form.action="del_cn_rec.php";
form.submit();
}
</script>
Here is the Delete php file: del_cn_rec.php
<?php
session_start();
include '_inc/dbconn.php';
$sql="SELECT * FROM admin WHERE id='1'";
$result= mysql_query($sql);
$rws= mysql_fetch_array($result);
$email_1= $rws[3];
$email_2= $rws[4];
$phone= $rws[5];
$address= $rws[6];
$url= $rws[7];
if(!isset($_SESSION['admin_login']))
header("location:$url/server-side");
?>
<?php
$id= mysql_real_escape_string($_REQUEST['cn_id']);
$sql1="SELECT * FROM consignments WHERE id='$id'";
$result1= mysql_query($sql1);
$rwsn= mysql_fetch_array($result1);
$cn= $rwsn[1];
$sql2="DROP TABLE IF EXISTS ".$cn."status ";
mysql_query($sql2) or die(mysql_error());
$sql3="DROP TABLE IF EXISTS ".$cn."msg ";
mysql_query($sql3) or die(mysql_error());
$sql6 = mysql_query("SELECT * FROM admin_inbox WHERE cn='$cn'");
if (mysql_fetch_row($sql6)){
$sql4="DELETE FROM admin_inbox WHERE cn='$cn'";
mysql_query($sql4) or die(mysql_error());
}
$sql5="DELETE FROM consignments WHERE id='$id' AND cn='$cn'";
mysql_query($sql5) or die(mysql_error());
//Consignment Remove Success Msg
$msg = "<i class=\"fa fa-check\"></i> Consignment: $cn Has Been Successfully Deleted!";
header("Location:$url/server-side/consignments?msg=$msg");
?>
this should generally work, just ensure you don't have any errors or notices in your php log, or in the generated html, or any javascript errors in the browser console. Ensure that the value for the radio button is filled in the html (maybe you are getting wrong result from the sql).
Also I hope you know you have to first select the radio button and only after that click the "Delete" or "Edit" link - it is a little confusing here, since you put the link on every row. Radio buttons send values only when they are selected, otherwise they will not even appear in the POST data.
If you wanted to use the links on every row without first selecting the radio button, you have to pass the cn_id as a parameter to the askForDelete_Rec() function and then use it there. An input type="hidden" might be usefull for that, or just append it to the action as a GET parameter.
Related
HTML button not selecting correct form value.
The following code selects data and displays it correctly in a html table: fname, lname , customer_id and the last column in the table shows a button labelled more info.
The first part of the code works fine.
<table>
<thead>
<tr>
<th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$sql = "select fname, lname,customer_id from customer_address ";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
$fname = $row['fname'];
$lname = $row['lname'];
$customer_id = $row['customer_id'];
echo" <tr>";
echo"<td>$fname</td>";
echo"<td>$lname</td>";
echo"<td>$customer_id</td>";
echo"<td><input type='hidden' name='customer_id' value='$customer_id'>";
echo "<button type='submit' name='submit'>More Info</button></td>" ;
}
echo" </tr>";
?>
</tbody>
</table>
</form>
With the second part of the code, I want to grab the customer_id when the button is clicked and display it below the table(I am going to uses the variable in a subsequent query to display more information later). The button selects the same customer_id no matter which button is clicked, and it is always the last record in the table. I would appreciate any help in correcting my error.
<?php
$ccf=" ";
if(isset($_POST['submit'])){
$ccf = $_POST['customer_id'];
}
echo "Customer Id selected is:$ccf";
?>
Your elements are all inside one form, so there are multiple elements with name='customer_id'. Place each row inside their own separate <form></form>:
<table>
<thead>
<tr>
<th>First Name</th> <th>Last Name</th> <th>Customer ID</th><th>Info</th>
</tr>
</thead>
<tbody>
<?php
$sql = "select fname, lname,customer_id from customer_address";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
$fname = $row['fname'];
$lname = $row['lname'];
$customer_id = $row['customer_id'];
echo" <tr>";
echo "<td>$fname</td>";
echo "<td>$lname</td>";
echo "<td>$customer_id</td>";
echo "<td>";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='customer_id' value='$customer_id'>";
echo "<button type='submit' name='submit'>More Info</button>";
echo "</form>";
echo "</td>";
echo" </tr>";
}
?>
</tbody>
</table>
I'm having some trouble trying to build a very simple inventory management system.
What I'm doing is showing the data from a database in a html table and in each row a create two buttons: one to edit and one to delete the item. The problem is that I'm not being able to call these buttons with the isset() function and I can't understand why. I've tried to create a specific function for these but still doesn't work. Anybody has any idea?
Here is the code:
P.S.: Don't mind small english erros or a lack of of brackets. I had to change the code a little bit.
function searchTablet(){
if(isset($_POST['btnSearchTablet'])){
global $connection;
$query="SELECT * FROM tablet";
$run=mysqli_query($connection, $query);
echo "<table class='table table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Brand</th>";
echo "<th>Model</th>";
echo "<th>Color</th>";
echo "<th>Price</th>";
echo "<th>Fabrication Date</th>";
echo "<th>Provider</th>";
echo "<th>Registration Date</th>";
echo "<th>Edit</th>";
echo "<th>Delete</th>";
echo "</tr>";
echo "</thead>";
while($obj=mysqli_fetch_object($run)){
echo "<tr>";
echo "<td>$obj->id</td>";
echo "<td>$obj->idBrand</td>";
echo "<td>$obj->idModel</td>";
echo "<td>$obj->idColor</td>";
echo "<td>$obj->price</td>";
echo "<td>$obj->fabricationDate</td>";
echo "<td>$obj->idProvider</td>";
echo "<td>$obj->registrationDate</td>";
echo "<td><a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a></td>";
echo "<td><a href='resultTablet.php?btnDeleteTablet{$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a></td>";
echo "</tr>";
if(isset($_POST["btnDeleteTablet{$obj->id}"])){
$idTablet=$obj->id;
$delQuery="DELETE FROM tablet WHERE id='$idTablet'";
$delRun=mysqli_query($connection, $delQuery);
if($delRun){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
}
As always... looks like StackOverflow users are toxic and unfriendly and don't even try to help new people who want to get into programming.
So I will try to help you a little bit.
If you are passing parameters via URL you have to use $_GET not $_POST method.
Also, I would change
<a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
to
<a href='resultTablet.php?btnEditTablet={$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
So you could do this:
if(isset($_GET["btnDeleteTablet"])){
and you would get id via $_GET like this:
$idTablet=$_GET["btnDeleteTablet"];
After this change, you can close while loop you used before
this if(isset($_GET["btnDeleteTablet"])) line, also you wont need $obj->id
anymore, because you will $_GET data decoupled from while loop or any other code you wrote before.
On another note, I see you forgot <tbody> </tbody> in your table.
EDIT:
Also, what are you doing with
if(isset($_POST['btnSearchTablet'])){
This wont work after delete button click.
You shouldn't use it like that, because after the delete button click your page will go to URL with $_GET parameters and that if logic will prevent
if(isset($_GET["btnDeleteTablet"])){
logic working.
So move whole
if(isset($_GET["btnDeleteTablet"])){
...
}
out of if(isset($_POST['btnSearchTablet'])){
Read up about $_POST and $_GET methods also, read about forms
you really need it.
Also, I recommend you to get program to profile requests so you could see how post and get data moves.
I recommend you to install Fiddler program, so you would be able to see how post, get data moves.
Ok, I will try to fix your code at last so it could work:
<?php
function searchTablet(){
global $connection;
if(isset($_GET['btnDeleteTablet'])){
deleteTablet();
}
//I dont why are you using it so I commented it out.
//if(isset($_POST['btnSearchTablet'])){
//}
displaySearchTablet();
}
function displaySearchTablet(){
global $connection;
$query = "SELECT * FROM tablet";
$run = mysqli_query($connection, $query);
while($obj = mysqli_fetch_object($run)){
//Combine all rows into one variable
$table_rows .= "
<tr>
<td>{$obj->id}</td>
<td>{$obj->idBrand}</td>
<td>{$obj->idModel}</td>
<td>{$obj->idColor}</td>
<td>{$obj->price}</td>
<td>{$obj->fabricationDate}</td>
<td>{$obj->idProvider}</td>
<td>{$obj->registrationDate}</td>
<td>
<a href='resultTablet.php?btnEditTablet={$obj->id}' class='btn btn-primary' name='btnEditTablet'>Alterar</a>
</td>
<td>
<a href='resultTablet.php?btnDeleteTablet={$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a>
</td>
</tr>";
}
$result_table =
"<table class='table table-striped'>
<thead>
<tr>
<th>ID</th>
<th>Brand</th>
<th>Model</th>
<th>Color</th>
<th>Price</th>
<th>Fabrication Date</th>
<th>Provider</th>
<th>Registration Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
$table_rows
</tbody>
</table>";
echo $result_table;
}
function deleteTablet(){
global $connection;
$id = $_GET['btnDeleteTablet'];
$query = "DELETE FROM tablet WHERE id = '$id'";
$run = mysqli_query($connection, $query);
if($run){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
I kept it very basic, so you would be able to understand.
I didn't pass parameters or returned anything so it would be more understandable to you.
Good luck on your journey to programming.
isset() will return false if an empty string is being submitted, so try using !empty() instead.
I have added a button at the end of the row of my datatable intended to update the information in the database for the specific row chosen.
When I click the update button, a form pops us with the relevant fields to update.
Ideally, I would like the form to be autopopulated with the information from the table row which I have chosen to update
Code...
Table:
<div class="viewalljob tab-pane show active" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<h2>Edit Job Table</h2>
<table id="edit-job-table" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th class="job_id">ID</th>
<th class="job_date">Date</th>
<th class="job_company">Company Name</th>
<th class="job_contact">Contact</th>
<th class="job_from">From</th>
<th class="job_to">To</th>
<th class="job_driver nowrap">Driver</th>
<th class="job_income">Income (£)</th>
<th class="job_payment">Payment (£)</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<!--Fetch from Database-->
<!--Connect To Database-->
<?php
$host_name = 'xxx';
$database = 'xxx';
$user_name = 'xxx';
$password = 'xxx';
$conn = mysqli_connect($host_name, $user_name, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT *,AS markup
FROM `table`
GROUP BY id";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
// output data of each row
while($result = mysqli_fetch_assoc($query)) {
echo "<tr>
<td class='job_id'>".$result['id']."</td>
<td class='job_date'>".$result['adddate']."</td>
<td class='job_company'>".$result['customer']."</td>
<td class='job_contact'>".$result['addcontact']."</td>
<td class='nowrap job_from'>".$result['addfrom']."</td>
<td class='nowrap job_to'>".$result['addto']."</td>
<td class='nowrap job_driver'>".$result['adddriver']."</td>
<td class='currency job_income'>".$result['addincome']."</td>
<td class='currency job_payment'>".$result['addpayment']."</td>
<td><button type='button' name='update' id=".$result['id']." class='btn btn-warning btn-xs update updatebtn'>Update</button></td>
<td><button type='button' name='delete' id=".$result['id']." class='btn btn-danger btn-xs delete deletebtn'>Delete</button></td>
</tr>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
<div id="contactForm3">
<h1>Edit Job</h1>
<form id="dataForm" name="dataform" method="POST" action="/">
/**** FORM DATA ****/
</form>
</div>
JS to open form:
$(function() {
// contact form animations
$('.update').click(function() {
$('#contactForm3').fadeToggle();
})
$(document).mouseup(function (e) {
var container = $("#contactForm3");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.fadeOut();
}
});
});
I have added the update button using <td><button type='button' name='update' id=".$result['id']." class='btn btn-warning btn-xs update updatebtn'>Update</button></td> I have given the id=".$result['id']." hoping that I can use this to populate the form from the ID
I am assuming that I will need to connect to the database table, something like:
<form>
<?php
//Connect to Database ... //
$sql = SELECT * FROM `table`
WHERE ID = ???
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
// output data of each row
while($result = mysqli_fetch_assoc($query)) {
echo "<span>
<label> label1 </label>
<input value = ".$result['column'].">
</span>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Im hoping that this is correct and someone can help me do this?
Actually you can acheive this using javascript/jquery, get row element from table displayed, from which you can get every columns/inputs value like as below:
$('.updatebtn').on('click', function(e) {
e.preventDefault();
var row = $(this).closest('tr'); //get table row from displayed table.
var form = $('#contactForm3').fimd('form'); //form to be populate.
//make sure your form input field with same name as the column name.
//now create form_data object with key as same name as column/input name .
var form_data = { 'id' : row.find('.job_id').value(),
'adddate' : row.find('.job_date').value(),
.....
'addpayment' : row.find('.job_payment').value()
}
//Apply loop to populate values in form.
$.each(form_data, function(key, value){
form.find('input[name="'+ key +'"]').val(value);
});
});
Then on form submit update database record/row using PHP (hope you know that well, if not let me know I will update my code).
I am very new in codeigniter. This is my first app. In my database I insert empid and deptid. When both are inserted in table all data show. But I want when both data not present in table then also all data show with blank that column. If any data not present in table then a add button display in place of delete url.
controller
public function index()
{
$rows = $this->EmpDept_model->get_empdept();
$data['recdept'] = $this->EmpDept_model->get_empdept();
$this->load->helper('url');
$this->load->view('empdept/EmpDept_list',$data);
}
view
<table class="table table-striped table-bordered" id="dataTables-example">
<thead>
<tr style="background-color: #d7ccc8;">
<th>Employee Name</th>
<th>Department Name</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
foreach($recdept as $r)
{
echo "<tr class='odd gradeX'>";
echo "<td>".$r->empname."</td>";
echo "<td>".$r->deptfname."</td>";
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/empdept/delete/".$r->empdeptid."'>Delete</a></td>";
/*echo "<td><a data-toggle='tooltip' data-placement='bottom' title='Edit' style='color: #000000;' href = '".base_url()."index.php/emp/edit/".$r->empid."'><span class='glyphicon glyphicon-pencil'></span></a>";
echo " || <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/emp/delete/".$r->empid."'><span class='fa fa-ban fa-1x'></span></a></td>";*/
echo "</tr>";
}
?>
</tbody>
</table>
model
public function get_empdept()
{
$this->db->select('ed.empdeptid,e.empname,d.deptfname');
$this->db->from('empdept ed,empinfo e,deptinfo d');
$this->db->where('e.empid =ed.empid and d.deptid = ed.deptid');
$query = $this->db->get();
return $query->result();
}
Try this code:
public function get_empdept()
{
$this->db->select('ed.empdeptid,e.empname,d.deptfname');
$this->db->from('empdept ed');
$this->db->join('empinfo e','e.empid = ed.empid', 'left');
$this->db->join('deptinfo d','d.deptid = ed.deptid', 'left');
$query = $this->db->get();
return $query->result();
}
Check if column is blank than set add button otherwise delete button:
<table class="table table-striped table-bordered" id="dataTables-example">
<thead>
<tr style="background-color: #d7ccc8;">
<th>Employee Name</th>
<th>Department Name</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
foreach($recdept as $r)
{
echo "<tr class='odd gradeX'>";
echo "<td>".$r->empname."</td>";
echo "<td>".$r->deptfname."</td>";
if(!empty($r->empdeptid)){
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/empdept/delete/".$r->empdeptid."'>Delete</a></td>";
} else {
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Add New' style='color: #000000;' href = '".base_url()."index.php/empdept/add/'>Add</a></td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
You have to use union to achieve this. But code-igniter doesn't support union
So you can try like following . Change the query in model
$this->db->query("Select empdeptid from empdept union Select empname from empinfo union Select deptfname from deptinfo where empinfo.empid = empdept.empid and deptinfo.deptid = empdept.deptid");
i have this script bellow to open table2 when clicking on the buttom 'more details'
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
and this my code
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
$sql2=..
$sql3 = blabla ;
while($row3 =mysql_fetch_array($sql3)){
$sql4 = mysql_query (" SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink > here </blink>" ;
$wnumber = $row3[$ww] ;
echo "<tr id= '".$wnumber."'><td style= 'text-align : center ;'>Week ".$row3[$ww]."
</td>" ;
echo "<td >".(int) $row3["percent"] ."% </td>";
echo "<td > "?><?php if($row4['place'] ==
$row3[$ww] and $row2['id'] == $userid ){ echo $string ; } else { echo "";} ;?><?php
"</td>";
echo "<td ><button class='showr'>More Details</button> </td></tr>";
//More Details when clicking on this buttom it open the table2
}
</tr>
</table>
this is second table
<?php
echo "<div id= 'Table2' style= 'display:none;'>";
echo "<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
echo "<th> Week ".$wnumber."</th>";
echo "<th>try2</th>";
echo "<tr ><td>day</td>";
echo "<td>fff</td></tr>";
echo "</table></div>";
?>
*what i have now 5 rows with 5 buttoms .
*what it happen now is when clicking on every bottom it echo same '$wnumber' lets say 6.
however it defers from row to row ,
- script works good with alert of the id of which row is clicked.
- only the last buttom who works with the last id of row.
*what i want is every bottom works with its row id which echo the right '$wnumber'
* what i have tried is (make variable in the div)
echo "<div id= '".$wnumber."' style= 'display:none;'>";
instead of
echo "<div id= 'Table2' style= 'display:none;'>";
but didnt work.
hope its clear and there is solution of it.
EDIT : this source code
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
<br />
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<th >Weeks</th>
<th ><p></p></th>
<th > Your place</th>
<th > More Details</th>
<tr>
<tr id= '1'><td style= 'text-align : center ;'>Week 1</td><td style= 'text-align :
center ;'>33% </td><td style= 'text-align : center ;'> <td style= 'text-align :
center ;'><button class='showr'>More Details</button></td></tr><tr id= '6'><td
style= 'text-align : center ;'>Week 6</td><td style= 'text-align : center ;'>33%
</td><td style= 'text-align : center ;'> <td style= 'text-align : center
;'><button
class='showr'>More Details</button></td></tr><tr id= '13'><td style= 'text-align:
center ;'>Week 13</td><td style= 'text-align : center ;'>33% </td><td style=
'text-align : center ;'> <blink style= 'color:#990000 ;font-weight: bolder;' > 69
here </blink><td style= 'text-align : center ;'><button class='showr'>More
Details</button></td></tr></tr>
</table>
<br />
<div id= 'Table2' style= 'display:none;'><table width='100%' border='1'
cellspacing='0' cellpadding='0'><th> Week 13</th><th>try2</th><tr ><td>day</td>
<td>fff</td></tr></table></div>
<br /><br /> <br />
I tried your html code after correcting the missing td, tr.
And then clicking on each row / button displays the div.
Ensure you form proper html code in your php echo
Try something like this:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#details').find('#week' + $(this).attr('id')).show();
// alert($(this).attr('id'));
});
});
</script>
<?php
$result = mysql_query($sql);
// save the results to an array so you can loop them twice
while($row = mysql_fetch_array($result)) :
$rows[] = $row;
endwhile;
?>
<table id="table1">
<tr>
<th>heading</th>
<th>heading</th>
<th>heading</th>
<th>heading</th>
</tr>
<?php foreach ($rows as $row) : // loop #1 to output the main table ?>
<tr id="<?php echo $row['ww'] ?>">
<td>value</td>
<td>value</td>
<td>value</td>
<td><button type="button">More details</button></td>
</tr>
<?php endforeach; ?>
</table> <!-- end table 1 -->
<div id="details">
<?php foreach ($rows as $row) : // loop #2 to output a table for each set of details ?>
<table id="week<?php echo $row['ww'] ?>" style="display: none">
<tr>
<th>Week <?php echo $row['ww'] ?></th>
<th>try2</th>
</tr>
<tr>
<td>value</td>
<td>value</td>
</tr>
</table>
<?php endforeach; ?>
</div> <!-- end details -->
I copy / pasted your code into a jsFiddle and it seems to work as expected. Clicking on the row sends an alert with the correct ID.
http://jsfiddle.net/JeeNN/
Is there something I'm missing in your intent here?
Note : Best practice would be to skip the inline CSS and add external styling for your tables. Also, valid HTML is a must.
Update:
I went ahead and formatted the php code you posted so it's a bit easier to read. There's some variables that aren't defined and a couple other issues, but I'm sure you have it correct in your php file.
I think that you're going to want to run the loop a second time to create the second table. In this second loop, echo out the second table once per loop - you'll end up with a bunch of tables (one per row plus the first table). Then simply swap the visibility of the tables as user's click each id.
Is that what you're looking for? If not, perhaps try to rephrase your question.
Here's that formatted code:
<table id="Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<?php
$sql2 = [..];
$sql3 = [..];
$ww = ?;
while($row3 = mysql_fetch_array($sql3)){
$sql4 = mysql_query ("SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink> here </blink>" ;
$wnumber = $row3[$ww];
echo "<tr id='".$wnumber."'>";
echo "<td style='text-align:center;'>Week ".$row3[$ww]."</td>";
echo "<td>".(int) $row3["percent"] ."% </td>";
echo "<td>":
if($row4['place'] == $row3[$ww] and $row2['id'] == $userid ){
echo $string;
} else {
echo " ";
}
echo "</td>";
//More Details when clicking on this buttom it open the table2
echo "<td ><button class='showr'>More Details</button></td>";
echo "</tr>";
} ?>
</table>
<div id= 'Table2' style= 'display:none;'>
<table width='100%' border='1' cellspacing='0' cellpadding='0'>
<tr>
<th> Week <?php echo $wnumber; ?></th>
<th>try2</th>
</tr>
<tr>
<td>day</td>
<td>fff</td>
</tr>
</table>
</div>