I have a html page which links to a php function page, with numerous functions on.
It appears that when the refresh button is clicked in Chrome, the text (only from the functions) disappears. Why would this be caused? This is the same with my update buttons which perform a meta refresh...
function Add_Remove_Topics(){
//CONECT TO MYSQL
$username = "root";
$password = "";
$database = "user_test";
$id = ($_SESSION["user_id"]);
$connect = mysql_connect('localhost',$username,$password);
# mysql_select_db($database) or die( "Unable to select database");
//GET VALUES
$sql = "SELECT * FROM topics";
$result = mysql_query($sql);
//echo "<div id='add-remove-topics'>";
//Table headlines - NOT A PHP
echo "<center>Simply type into the fields to change your current topic information - click update once done editing. If you no longer want one of your topics, click the delete button.</center>";
echo "<form action='' method='POST'><table id='artopics'>
<col width='30%'>
<col width='70%'>
<col width='70px'>
<col width='70px'>
<tr height='40px'>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
<td style='background-color: #BD4040;'></td>
<td style='background-color: #BD4040;'></td>
</tr>";
// output data of each row
while($row = mysql_fetch_assoc($result))
{
echo
"
<tr height='40px'>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='up_topic_name[{$row['topic_id']}]' value='" . $row['topic_name'] . "'></td>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='up_topic_address[{$row['topic_id']}]' value='" . $row['topic_address'] . "'></td>
<td style='background-color: white;' align='center'><button type='submit' name='updateTopic' value='".$row['topic_id']."'>Update</button></td>
<td style='background-color: white;' align='center'><button type='submit' name='deleteTopic' value='".$row['topic_id']."'>Delete</button></td>
</tr>
";
}
if(isset($_POST['updateTopic'])){
$updateID = $_POST['updateTopic'];
$updateTopicName = $_POST['up_topic_name'][$updateID];
$updateTopicAddress = $_POST['up_topic_address'][$updateID];
$updateTopic = mysql_query("UPDATE topics SET topic_name='$updateTopicName', topic_address='$updateTopicAddress' WHERE topic_id='$updateID'") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
if(isset($_POST['deleteTopic'])){
$deleteID = $_POST['deleteTopic'];
$deleteFromTopics = mysql_query("DELETE FROM topics WHERE topic_id = $deleteID ") or die(mysql_error());
$deleteFromUserTopic = mysql_query("DELETE FROM user_topic WHERE topic_id = $deleteID ") or die(mysql_error());
echo "<meta http-equiv='refresh' content='0'>";
}
echo "</table></form>";
echo "<br><center>To add a new topic, simply fill in the fields below, clicking insert once completed.</center>";
echo "<form action='' method='POST'><table id='artopics'>
<col width='30%'>
<col width='70%'>
<col width='140px'>
<tr height='40px'>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Name</td>
<td style='background-color: #BD4040; color: white; padding-left: 5px;'>Topic Address</td>
<td style='background-color: #BD4040;'></td>
</tr>";
// output data of each row
echo
"
<tr height='40px'>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_name'></td>
<td align='center' style='font-weight: normal;background-color: white;'><input type='text' name='new_topic_address'></td>
<td align='center' style='background-color: white;'><button style='width:130px;' type='submit' name='InsertTopic'>Insert</button></td>
</tr>
";
if(isset($_POST['InsertTopic'])){
$topicName = $_POST['new_topic_name'];
$topicAddress = $_POST['new_topic_address'];
$insertIntoTopics = mysql_query("INSERT INTO topics (topic_name, topic_address) VALUES ('$topicName', '$topicAddress') ") or die(mysql_error());
$grabNewTopicId = mysql_query("SELECT MAX(topic_id) AS topic_id FROM topics") or die(mysql_error());
while($row2 = mysql_fetch_array($grabNewTopicId)) {
$NewTopicId = $row2['topic_id'];
$insertIntoUserTopic = mysql_query("INSERT INTO user_topic (user_id, topic_id) VALUES ('$id', '$NewTopicId') ") or die(mysql_error());
}
echo "<meta http-equiv='refresh' content='0'>";
}
echo "</table></form>";
}
(Screenshots included)
Before refresh/button click
After refresh/button click
Related
my problem may be easy for experienced users, but I'm stucked on it. So, here's the problem. I have submit buttons in while loop, they draw, but they don't react on click. Here's my piece of code.
`<?php
$zap_cmember = count_query("SELECT * FROM `users` WHERE `clan` = '".$member['id']."' AND clan_stat != 'Призывник'");
while ($cmember = mysql_fetch_array($zap_cmember))
{
$zap_gold = mysql_result(mysql_query("SELECT SUM(gold) FROM klog WHERE name = '".$cmember['name']."'"), 0);
$zap_online = mysql_num_rows(count_query("SELECT * FROM `online` WHERE `id_session`='".$cmember['id_user']."'"));
if ($zap_online > 0)
{
$img_on = "<td align='center' valign='middle' ><img src='images/ico_online.png' alt='онлайн' class='png' ></td>";
}
else
{
$img_on = "<td align='center' valign='middle' ><img src='images/ico_offline.png' alt='оффлайн' class='png' ></td>";
}
$i = 'set';
echo '<tr>
<td width="141" height="25" align="left" valign="middle" >'.$cmember['name'].' ['.lvl($cmember['exp']).']</td>
<td align="center" valign="middle" ><span class="text_main_4">'.$cmember['clan_stat'].'</span></td>
<td align="center" valign="middle" ><span class="text_main_4">-</span></td>
<td align="center" valign="middle" ><span class="text_main_4">'.$zap_gold.'</span></td>
<td align="center" valign="middle" ><span class="text_main_4">'.$cmember['glory'].'</span></td>
'.$img_on.'';
if($boss)
{echo '<td align="center" id = "'.$i.'" valign="middle"><input type="submit" name="bSet" value="Назначить"></span></td>'; }
$i++;
$player = $cmember;
'</tr>';
if (isset($_POST['bSet']))
{
echo 'works';
count_query("UPDATE users SET clan_stat='Вождь' WHERE id_user='".$player['id_user']."'");
$time = date('j.n.y H:i');
count_query("INSERT INTO `message` (`time`, `to`, `text`, `metka`) VALUES ('".$time."', '".$player['name']."', 'Вас выбрали главой клана <a href=clan.php>".$clan['name']."</a>', '6')");
echo "<script>location.href='clan.php'</script>";
}
}
?>
`
You need to put everything that you want to submit to a <form id="yid">... </form>, after that to put on the button the onclick="javascript:$('#yid').submit()" and the yid= the particular id of the form you want the button to submit.
I have displayed sql table in html table, made a hyperlink near all fields, when i click it the whole field details should be shows in other page(ie; i show only 2 fields of sql in the table and want to show rest in another page).
admin.php
<?php
$con= mysql_connect("localhost","root","");
mysql_select_db("main",$con);
echo"<form action=\"post\" class=\"form-horizontal\" role=\"form\">";
echo "<table width='700' height='150' onclick='myFun(event)'>";
echo" <tr>
<td width='100' align='center'></td>
<td width='100' align='center'><b><u>NAME</u></b></td>
<td width='100' align='left'><b><u>E-MAIL</u></b></td>
</tr>
";
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
echo"</form> ";
?>
viewadmin.php
<?php
$name = $_GET['name'];
$result=mysql_query("SELECT NAME,DOB,MOB,EMAIL, FROM admin WHERE NAME = $name");
if (false === $result) {
echo mysql_error();
}
else {
$row=mysql_fetch_row($result);
}
echo" <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>Name</td>
<td align='left'>".$row['NAME']."</td>
</tr>
<tr>
<td align='left'>E-mail</td>
<td align='left'>".$row['EMAIL']."</td>
</tr>
<tr>
<td align='left'>D.O.B</td>
<td align='left'>".$row['DOB']."</td>
</tr>
<tr>
<td align='left'>Mobile</td>
<td align='left'>".$row['MOBILE']."</td>
</tr>
<tr>
<td align='left'>Photo</td>
<td ><img src='uploads/grumpy.jpg' height='200' width='200'></td>
</tr>
</table>";
echo"</form> ";
?>
do something like this:
admin.php
$result=mysql_query("select NAME,EMAIL from admin order by AID");
while($row=mysql_fetch_array($result)) {
echo "<tr>";
echo"<td width='100' align='center'><a href='viewadmin.php?name=".$row['NAME']."'>Select</a></td>";
echo"<td width='100' align='center'>".$row['NAME']."</td>";
echo"<td width='100' align='left'>".$row['EMAIL']."</td>";
echo"</tr>";
}
echo"</table>";
and in viewadmin.php
$name = $_GET['name'];
$result=mysql_query("SELECT * FROM admin WHERE name = $name");
$row=mysql_fetch_row($result);
echo " <form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>...";
first rename the html page by php page, then you can pass the primary key or any key of the row from first page to admin page with the help of GET.
for eg:
first.php
<?php
$result=mysql_query("select ID,NAME,EMAIL from admin order by AID"); while($row=mysql_fetch_array($result)){
?><a hre='admin.php?id="$id=<?php $row[0] ?>"'></a>
<?php
}
?>
and in the admin.php page
you can access the value like
echo $_GET['id'];
stop using MySQL and use MySQLi, this code should work
<?php
$db_connect = mysqli_connect('localhost', 'root', 'pass', 'database');
if (mysqli_connect_errno($db_connect)) {
die('Some error occurred during connection to the database');
}
$name = mysqli_real_escape_string($db_connect,$_REQUEST['name']);
if($stmt = mysqli_prepare($db_connect, 'SELECT * FROM admin WHERE name = ?')){
mysqli_stmt_bind_param($stmt, 's', $name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) !== 0){
$row = mysqli_fetch_assoc($result);
echo "<form class=\"form-horizontal\" role=\"form\">
<table width='400'>
<tr>
<td align='left'>".$row['NAME']."</td>
<td align='left'></td>
</tr>
<tr>
<td align='left'>".$row['EMAIL']."</td>
<td align='left'>..."
}
else{
echo 'not found';
}
}
else{
trigger_error('error:' . mysqli_errno($db_connect) . mysqli_error($db_connect));
}
?>
I want to display the data loaded from database after it is inserted using the function add_data() but it's not displaying the data on the table.
if(isset($_POST['btn_add_details'])) {
add_data();
include("db_PSIS.php");
$sql2="SELECT * FROM sample_barcode WHERE IDRec='".$row['IDRec']."'";
$result2=mysql_query($sql2);
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
while($row2=mysql_fetch_array($result2)) {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
?>
Here's the function for adding data on the database:
function add_data() {
include("db_PSIS.php");
$sql="INSERT INTO sample_barcode (LotTraveller, ShipmentLotNumber) VALUES ('".$_POST['traveller']."', '".$_POST['datecode']."')";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
mysql_close($link);
}
Here's the code for database connection:
<?php
$username="****";
$password="****";
$database="****";
$hostname="****";
$link=#mysql_connect($hostname,$username,$password)
or die ("...cannot connect database, using $username for $hostname");
mysql_query("set names 'utf8'"); //指定数据库字符集
mysql_select_db($database,$link);
?>
Here's the form input:
<table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<tr>
<th align="right">Parent Traveller: </th>
<td><input type="text" name="traveller" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else { echo $row2['LotTraveller']; } ?>"/></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else { echo $row2['ShipmentLotNumber']; } ?>" /></td>
</tr>
<tr>
<th align="right">Traveller 1: </th>
<td><input type="text" name="traveller1" size="20" /></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode1" size="20" /></td>
</tr>
<tr>
<th align="right">Traveller 2: </th>
<td><input type="text" name="traveller2" size="20" /></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode2" size="20" /></td>
</tr>
<tr>
<th align="right"> </th>
<td><input type="hidden" name="id" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else {echo $row2['IDRec'];} ?>"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btn_add_details" id="btn_add_details" onClick="return check_submit();" value="Add the Detail(s)" onfocus="setStyle(this.id)"
style="color: #000000;
padding: 2px 5px;
border: 2px solid;
border-color: #7bf #07c #07c #4AA02C;
background-color: #09f;
font-family: Georgia, ..., serif;
font-size: 18px;
display: block;
height: 30px;
width: 200px;" /> </td>
</tr>
</table>
Since you stated that IDRec is your PK and is set to auto-increment, AND you are pulling records based on a single number... you will never get more than one result in your resultset.
Change this code block...
$sql2="SELECT * FROM sample_barcode WHERE IDRec='".$row['IDRec']."'";
$result2=mysql_query($sql2);
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
while($row2=mysql_fetch_array($result2)) {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
To this...
$sql2="SELECT * FROM sample_barcode WHERE IDRec=".$row['IDRec']."";
$result2=mysql_query($sql2);
$row2=mysql_fetch_assoc($result2); // added this line
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
In the first line I removed the single quotes around your IDRec - this is an integer so single quotes are not needed.
Removed your while loop - you will only ever get one result so a loop is not needed.
Also, this doesn't appear to actually be a form so you can also remove your <form> </form> tags.
Finally, start learning pdo_mysql. my_sql has been deprecated. Anyone still using this code will wake up one day to find out that their website has magically stopped functioning.
EDIT
In your add_data function, change it to this...
function add_data() {
include("db_PSIS.php");
$sql="INSERT INTO sample_barcode (LotTraveller, ShipmentLotNumber) VALUES ('".$_POST['traveller']."', '".$_POST['datecode']."')";
$result=mysql_query($sql);
$sql="SELECT * FROM sample_barcode ORDER BY IDRec DESC"; // added this line
$result=mysql_query($sql); // added this line
$row=mysql_fetch_assoc($result); // changed this line from array to assoc
mysql_close($link);
}
This will get the result of the data you just entered. I added a query to get the data you need to display.
I'm having problems with a "google like" search engine .
When the page loads, it expects a character in the input box to display inside a div the results.
What can I do to display all the database contents without typing any character in the input box?
Below are the three files:
Index.html
<html>
<head>
<title></title>
</head>
<body>
Caută analizele în baza noastră de date.<br>Taxa de urgență este de 35 Ron
<p align ="right">
<input type="text" id="go" class="field" name="go" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Caută analiza dorită':this.value;" value="Caută analiza dorită"/>
<input type="submit" name="search" id="submit" value="Cauta" /><br/>
<div id="search_query"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/instant.js"></script>
</p>
</body>
</html>
Search.php
<?php
$conn = mysql_connect("localhost","user","pass");
mysql_select_db("db");
mysql_set_charset("UTF8", $conn);
if(isset($_POST['search_query'])){
$search_query = mysql_real_escape_string(htmlentities($_POST['search_query']));
$search_query_x = explode(",",$search_query);
foreach($search_query_x as $search_each){
#$x++;
if($x==1)
#$construct.="Den LIKE '%$search_each%'";
else
$construct.="AND Den LIKE '%s$search_each%'";
}
$construct ="SELECT * FROM biochimie1 WHERE $construct";
$run = mysql_query($construct) or die(mysql_error());
$foundnum = mysql_num_rows($run);
// Define $color=1
$color="1";
if ($foundnum==0){
echo "Ne pare rau dar nu există rezultate pentru <strong><font color='red'>$search_query</font></strong></br></br> Vă rugăm să verificați dacă ați scris corect";
}
else{
echo'<p align="center">';
echo "$foundnum \n";
echo ' rezultate găsite în urma căutării!</p>';
echo "<table id='table1' border='0'width='500'>
<tr>
<th width='150' align='center' valign='middle'>Denumire Analize</th>
<th width='50' align='center' valign='middle'>Preț</th>
<th width='100' align='center' valign='middle'>Zile de recoltare</th>
<th width='100' align='center' valign='middle'>Termen de eliberare a rezultatelor</th>
<th width='100' align='center' valign='middle'>Se poate lucra în regim de urgență</th>
</tr></table>";
while($runrows = mysql_fetch_assoc($run))
{
$NrCrt = $runrows ['NrCrt'];
$denanalize = $runrows ['Den'];
$pret = $runrows ['Pret'];
$rec = $runrows ['Rec'];
$rez = $runrows ['Rez'];
$obs = $runrows ['Obs'];
echo "<table border='0'>";
// If $color==1 table row color = #FFC600
if($color==1){
echo "
<tr bgcolor='#efefef'>
<td width='150' align='center' valign='middle'>$denanalize</td>
<td width='50' align='center' valign='middle'>$pret</td>
<td width='100' align='center' valign='middle'>$rec</td>
<td width='100' align='center' valign='middle'>$rez</td>
<td width='100' align='center' valign='middle'>$obs</td>
</tr>
</table>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='#eaeaea'>
<td width='150' align='center' valign='middle'>$denanalize</td>
<td width='50' align='center' valign='middle'>$pret</td>
<td width='100' align='center' valign='middle'>$rec</td>
<td width='100' align='center' valign='middle'>$rez</td>
<td width='100' align='center' valign='middle'>$obs</td>
</tr>";
// Set $color back to 1
$color="1";
}echo "
</table>";
}
}
}
else{
echo 'An ERROR has occured';
}
?>
and instant.js file
$('#go').keyup(function(){
var search_query=$(this).val();
$.post('search.php', {search_query: search_query},function(searchq){
$('#search_query').html(searchq);
});
});
Thank you all in advance.
It will work on load:
$(document).ready(function(){
var search_query=$(this).val();
$.post('search.php', {search_query: search_query},function(searchq){
$('#search_query').html(searchq);
});
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>