I have an HTML table, where if you click on the “<?php echo $row["title"]; ?>“, for each row, it will display the exercise text corresponding to that title.
This is working fine, it toggles correctly but I don’t know why the doesn’t work. The title only displays in one of the columns, but I would like to display on the 3 of them. Below I post a picture with what I get.
Can you help me solve this?
This is my code:
<tr>
<td><?php echo $num_ex; ?></td>
<!---Click Toggle Exercise-->
<td><a onclick="myFunction(<?php echo $row["exercise_id"] ?>)" role="button" class="btn" target="_blank" ><?php echo $row["title"]; ?></a>
</td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<!--- Toggle --->
<tr id="toggle<?php echo $row["exercise_id"] ?>" style="display:none">
<td colspan="3"> <!---THIS COLSPAN IS NOT WORKING-->
<?php
$sql = "SELECT * FROM exercises WHERE exercise_id='".$row["exercise_id"]."'";
$result_ej = $conn->query($sql); /*Check connection*/
$row_ej = $result_ej->fetch_assoc();
?>
<p><?php echo $row["exercise_id"] . ". " . $row["text"]?></p>
</td>
</tr><!---Finish Toggle --->
This is my script:
<script>
function myFunction(ejer_id) {
var x = document.getElementById("toggle" + ejer_id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
Since you are changing the display of a <tr> you need to set it properly when you need to show it or else it does not obey table structure rules.
The issue is:
x.style.display = "block";
It should be:
x.style.display = "table-row";
Or, better yet just unset it if you don't want to keep track of the "correct" value:
x.style.display = "";
disclaimer: I do not know if all browsers will properly support this third option.
See https://stackoverflow.com/a/7971565/2191572 for other options.
You can also use:
x.style.display = "initial";
but it may not work in all browsers such as IE 11 and earlier per https://developer.mozilla.org/en-US/docs/Web/CSS/initial#Browser_compatibility
Related
I have a foreach loop on a tale row. Each row has an "edit" button to edit that row only. Using JS, I want to get a hidden open and edit the row.
Bellow is my code that I have tried but it works on one element only.
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction()" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction() {
<?php
foreach($sofas as $sofa)
{
?>
var x = document.getElementById("<?php echo $sofa["id"];?>");
<?php
}
?>
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
I'm still learning, so please help me resolve my issue. I'm expecting the code to work on each individual table row.
Why don't you use something like this:
<?php
foreach($sofas as $sofa)
{
?>
<tr>
<td><?php echo $sofa["name"];?></td>
<td><?php echo $sofa["zirkar"];?></td>
<td><?php echo $sofa["parche"];?></td>
<td><?php echo $sofa["fiparche"];?></td>
<td><?php echo $sofa["mizvasat"];?></td>
<td><?php echo $sofa["asaly"];?></td>
<td>
<input onclick="myFunction('<?php echo $sofa["id"];?>')" type="button" value="ویرایش">
</td>
</tr>
<tr style="display:none;" id="<?php echo $sofa["id"];?>">
<td>
This is my DIV element.
</td>
</tr>
<?php
}
?>
<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
Code is very simple, you only need to send the element id to the js function.
I have the following code
<table width="100%" border="1" cellpadding="15" align="center">
<?php
$res = $dbcon->query("SELECT * FROM actor");
while($row=$res->fetch_array())
{
?>
<tr>
<td><?php echo $row['actor_id']; ?></td>
<td><a href="?del=<?php echo $row['actor_id']; ?>" onclick="return confirm('sure to delete !'); " >delete</a></td>
</tr>
<?php
}
?>
This works fine in every way but the problem is that i cant seem to set the onclick to work onside an echo in this next piece of code.
I'm learning mysql/php so i'm experimenting with various codes.
FOR ($j=0 ; $j < $rows; ++$j) {
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_ASSOC);
echo 'Actor ID: ' . $row['actor_id'] . '<br>';
echo 'Delete'. '<br><br>';
}
$result->close();
$conn->close();
?>
The delete button works i just don't have a warning anymore, i can't figure out
how to properly insert onclick="return confirm('sure to delete !')
You will have to escape the quotes. This is what you need
echo 'Delete'. '<br><br>';
If you use PHP for templates maybe you should consider more template oriented syntax
<?php while($row=$res->fetch_array()): ?>
<tr>
<td><?= $row['actor_id'] ?></td>
<td><a href="?del=<?= $row['actor_id'] ?>" onclick="return confirm('sure to delete !'); " >delete</a></td>
</tr>
<?php endwhile ?>
It will make escaping a lot easier.
Yo can make it in another way:
<td><a class="delete" href="?del=<?= $row['actor_id'] ?>" >delete</a></td>
<script>
$(document).ready(function){
$('.delete').click(function(){
var id = $(this).attr('del');
var confirmText = "Are you sure you want to delete this object?";
if(confirm(confirmText)) {
$.ajax({
type:"POST",
url:url,
data: 'id=' + id,
success:function () {
// Here goes something...
},
});
}
return false;
});
});
</script>
I need a search box that would throw a result just like how the CTRL+F works.
Right now, on my index.php page I have the ff format:
<table width="100%">
<thead>
<tr>
<th><strong>Client Name:</strong></th>
<th><strong>Nationality:</strong></th>
<th><strong>Birthday:</strong></th>
<th><strong>Address:</strong></th>
<th><strong>Gender:</strong></th>
<th><strong>Birthplace:</strong></th>
</tr>
</thead>
<?php
$sql=mysql_query(" select * from tenant order by id asc");
$count=0;
while($row=mysql_fetch_array($sql))
{
$client_id = $row['id'];
$clientName = $row['cname'];
$cbday = $row['bday'];
$cadd = $row['address'];
$cgender = $row['gender'];
$cbirth = $row['birthplace'];
if($count%2)
{
?>
<tbody>
<?php } else { ?>
<tr id="<?php echo $id; ?>">
<?php } ?>
<td>
<span class="text"><?php echo $client_id.". ".$clientName; ?></span>
</td>
<td>
<span class="text"><?php echo $cbday; ?></span>
</td>
<td>
<span class="text"><?php echo $cadd; ?></span>
</td>
<td>
<span class="text"><?php echo $cgender; ?></span>
</td>
<td>
<span class="text"><?php echo $cbirth; ?></span>
</td>
</tr>
</tbody>
<?php
$count++;
}//while-end
?>
</table>
I've already tried many JQuery and Ajax tutorials but none of them seems to work fine with a value. So I just got into conclusion that perhaps those tutorials could only work only if you have a pre-defined value for the table rows. Like this one for example:
<th>ClientName</th>
Anyway I can have a CTRL+F like search on my index page for the table rows?
Javascript, especially in combination with jQuery is very easy to learn and understand. No need for plugins or tutorials.
How to search the table?
Here is a jsfiddle I just wrote for you:
http://jsfiddle.net/j9U52/
$('#search-submit').on('click', function(event) {
var v = $('#search-value').val(); // get the search value
var t = $('.searchable td'); // WHERE to search
var c = 'highlight'; // css class to add to the found string
// for each td in the table
$.each(t, function(idx, el) {
// find the keyword
var regex = new RegExp("\\b" + v + "\\b", "gi");
// replace it and add a span with the highlight class
el.innerHTML = el.innerHTML.replace(regex, function(matched) {
return "<span class=\"" + c + "\">" + matched + "</span>";
});
});
});
I did not add to reset the highlighted matches when you enter a new search keyword, I leave this up to you :-) Hint: Add something like t.removeClass(c);
I have a script that would change the value of a input field:
<script language="javascript">
function klientid() {
var getid = document.getElementById("getidfromsearch");//results
var klid = document.getElementById("idklienta"); //input field
if(getid != "0") {
klid.value = getid.textContent;
}
</script>
a search engine and this is how results are printed:
<?php
while($rowmilion = mysqli_fetch_array($resultmilion)) {
?>
<a href="javascript:klientid()">
<table>
<tr>
<td style="font-weight:bold; width: 160px; text-align:right;">ID:</td>
<td style="width:200px;" id="getidfromsearch"><?php echo $rowmilion['id']; ?></td>
</tr>
</table>
</a>
<?php } ?>
Now, when as the result there is only one record and I click on the link, it does change the value of a input field fine.
When result consists of more than one record, whichever link I would click, it will always get the value from the first record for example:
ID: 100
ID: 200
ID: 300
So when I click on the third position, it will still change the value of a input field to 100 instead of 300.
I quess, this is because all records get the same "ID"... But still, I can't make it work.
You should give an unique ID for each result or it will screw up your getElementById search.
Your JS code :
<script language="javascript">
function klientid(domElmtId) {// ** I added a parameter
var getid = document.getElementById(domElmtId);//results
var klid = document.getElementById("idklienta"); //input field
if(getid !== null) {// ** This is better
klid.value = getid.textContent;
}
</script>
Your Php code
<?php
$count = 0;//** I added a variable to give each result a different ID
while($rowmilion = mysqli_fetch_array($resultmilion)) {
?>
<a href="javascript:klientid('result<?php echo $count; ?>')">
<table>
<tr>
<td style="font-weight:bold; width: 160px; text-align:right;">ID:</td>
<td style="width:200px;" id="result<?php ?>"><?php echo $rowmilion['id']; ?></td>
</tr>
</table>
</a>
<?php
$count += 1;//** to make different IDs
} ?>
im having trouble getting a popup using javascript to show table data onmouseover when mousing over images returned in a table from the DB, now the problem i have isnt with actualy getting the onmouseover working (a previous question solved that) but getting the data displayed onmouseover the change for each image moused over.
the query's im using works fine and and display the correct data but on the onmouseover only ever shows the data which should only show for the first row in the table, the 2nd and subsequent rows should show different data as the query calls (which works, just doesnt show onmouseover)
keep in mind this page is included in my index.php which has all my .js and .css calls
page code:
<table border='0' cellpadding='0' cellspacing='0' class="center2">
<tr>
<td width='60'><img src="images/box_tl.png"></td>
<td style="background: url(images/box_tm.png)" align="center"><img src="images/news.png"></td>
<td width='25'><img src="images/box_tr.png"></td>
</tr>
<tr>
<td style="background: url(images/box_ml.png)"><h2>.</h2></td>
<td style="background: url(images/box_mm.png)">
<?php
include 'connect.php';
$query = mysql_query("SELECT * FROM tbl_img") or die(mysql_error());;
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Mob Name</th> <th>Id</th> <th>Health</th> <th>Body</th> <th>Effects</th> <th>Spawn</th></tr></thead><tbody>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $query )) {
$mob_id = $row['mob_id'];
$mob = $row['mob'];
$body = $row['body'];
$mob_name = $row['mob_name'];
$health = $row['health'];
$level = $row['level'];
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src='/testarea/include/mobs/$mob' />";
echo "</td><td>";
echo $mob_name;
echo "</td><td>";
echo $level;
echo "</td><td>";
echo $health;
echo "</td><td>";
echo "
<a onmouseover='ShowPop()' href=''><img src='/testarea/include/mobs/dead/$body' /></a>
";
echo "
<div id='hidden-table' style='display:none;'>
<table border='0' cellpadding='0' cellspacing='0' class='center3'>
<tr>
<td width='14'><img src='images/info_tl.png'></td>
<td style='background: url(images/info_tm.png)' align='center'></td>
<td width='14'><img src='images/info_tr.png'></td>
</tr>
<tr>
<td style='background: url(images/info_ml.png)'><h2>.</h2></td>
<td style='background: url(images/info_mm.png)'>
";
$query2 = mysql_query("SELECT * FROM tbl_drop WHERE mob_name='$mob_name'") or die(mysql_error());
echo "<table border='0' cellpadding='1' cellspacing='1' width='250' id='2' class='tablesorter'><thead>";
echo "<tr> <th> </th> <th>Item Name</th> <th>Qty</th></thead><tbody>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $query2 )) {
$id = $row['id'];
$item_img = $row['item_img'];
$qty = $row['qty'];
$item_name = $row['item_name'];
// Print out the contents of each row into a table
echo "<tr><td width='50'>";
echo "<img src='/testarea/item/$item_img' />";
echo "</td><td width='150'>";
echo $item_name;
echo "</td><td width='50'>";
echo $qty;
echo "</td></tr>";
}
echo "</tbody></table>";
echo "
</td>
<td style='background: url(images/info_mr.png)'><h2>.</h2></td>
</tr>
<tr>
<td width='14'><img src='images/info_bl.png'></td>
<td style='background: url(images/info_bm.png)' align='center'><h2>.</h2></td>
<td width='14'><img src='images/info_br.png'></td>
</tr>
</table>
</div>"
;
echo "</td><td>";
echo "test";
echo "</td><td>";
echo "test";
echo "</td></tr>";
}
echo "</tbody></table>";
?>
</td>
<td style="background: url(images/box_mr.png)"><h2>.</h2></td>
</tr>
<tr>
<td width='60'><img src="images/box_bl.png"></td>
<td style="background: url(images/box_bm.png)" align="center"><h2>.</h2></td>
<td width='25'><img src="images/box_br.png"></td>
</tr>
</table>
</html>
popup.js code:
// create the popup box - remember to give it some width in your styling
document.write('<div id="pup" style="position:abolute; display:none; z-index:200;"></div>');
var minMargin = 15; // set how much minimal space there should be (in pixels)
// between the popup and everything else (borders, mouse)
var ready = false; // we are ready when the mouse event is set up
var default_width = 200; // will be set to width from css in document.ready
function ShowPop()
{
popup($('#hidden-table').html(), 400);
}
/* Prepare popup and define the mouseover callback */
jQuery(document).ready(function(){
$('#pup').hide();
css_width = $('#pup').width();
if (css_width != 0) default_width = css_width;
// set dynamic coords when the mouse moves
$(document).mousemove(function(e){
var x,y;
x = $(document).scrollLeft() + e.clientX;
y = $(document).scrollTop() + e.clientY;
x += 10; // important: if the popup is where the mouse is, the hoverOver/hoverOut events flicker
var x_y = nudge(x,y); // avoids edge overflow
// remember: the popup is still hidden
$('#pup').css('top', x_y[1] + 'px');
$('#pup').css('left', x_y[0] + 'px');
});
ready = true;
});
/*
The actual callback:
Write message, show popup w/ custom width if necessary,
make sure it disappears on mouseout
*/
function popup(msg, width)
{
if (ready) {
// use default width if not customized here
if (typeof width === "undefined"){
width = default_width;
}
// write content and display
$('#pup').html(msg).width(width).show();
// make sure popup goes away on mouse out
// the event obj needs to be gotten from the virtual
// caller, since we use onmouseover='popup(msg)'
var t = getTarget(arguments.callee.caller.arguments[0]);
$(t).unbind('mouseout').bind('mouseout',
function(e){
$('#pup').hide().width(default_width);
}
);
}
}
/* Avoid edge overflow */
function nudge(x,y)
{
var win = $(window);
// When the mouse is too far on the right, put window to the left
var xtreme = $(document).scrollLeft() + win.width() - $('#pup').width() - minMargin;
if(x > xtreme) {
x -= $('#pup').width() + 2 * minMargin;
}
x = max(x, 0);
// When the mouse is too far down, move window up
if((y + $('#pup').height()) > (win.height() + $(document).scrollTop())) {
y -= $('#pup').height() + minMargin;
}
return [ x, y ];
}
/* custom max */
function max(a,b){
if (a>b) return a;
else return b;
}
/*
Get the target (element) of an event.
Inspired by quirksmode
*/
function getTarget(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
return targ;
}
All of the hidden tables have the same id, but IDs have to be unique.
provide the link as argument to ShowPop()
<a onmouseover='ShowPop(this)' ....>
then you'll be able to access the intended target inside ShowPop():
function ShowPop(o)
{
popup($(o).next('div').html(), 400);
}
Edit:
Regarding to the comment:
Currently I think the popup will not disapear onmouseout in any browser(except IE), because you didn't provide an argument to ShowPop() , what in other browsers is needed to return anything from getTarget, because they didn't know window.event.
Change this line:
var t = getTarget(arguments.callee.caller.arguments[0]);
to
var t = arguments.callee.caller.arguments[0];
...because when you take on my suggestion the link is already provided as argument to ShowPup(), there is no need to call getTarget() .