I am trying to make the entire row of a table clickable. I have looked at a bunch of tutorials and they all seem pretty simple but I can't seem to get it to work. I tried DoNav and that didn't work, the simple onclick attribute doesn't work, what am I doing wrong? I am using mysql, php, and html. Thank you for your help!
echo '<table>';
echo '<tr>';
echo '<th>Name</th><th>Checked In?</th><th>ID</th>';
echo '</tr>';
while($row = $CheckedIn->fetch_assoc()){
echo '<tr onmouseover="ChangeColor(this, true);" onmouseout="ChangeColor(this,false);" onclick="document.location="www.engineering.us/gena/details.php";">';
echo '<td align="left">';
echo $row['ChildName'];
echo '</td>';
echo '<td align="center">';
;
echo $row['checkedin'];
echo '</td>';
echo '<td align="center">';
echo $row['P_Id'];
echo '</td>';
echo '</tr>';
}
echo '</table>';
onclick="document.location="www.engineering.us/gena/details.php";"
this should be
onclick="document.location='www.engineering.us/gena/details.php';"
Also another tip, you have more HTML than php, so it is better to use multiple php tags rather than echo called many times. That way it would be little easier to find such mistakes.
Edit
You should also escape the apostrophe in case you are still going with php
Picking up from what georoot said, use PHP within your HTML. It makes the markup a lot more readable and usually allows your program to add syntax highlighting, making it easier to read. I've added two additional classes ("js-table" and "table-tr") which I'll explain in a bit. Another important point is that the URL is in a new attribute on the table row: data-url.
<table class="js-table">
<thead>
<tr>
<th>Name</th>
<th>Checked In?</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<?php while ($row = $CheckedIn->fetch_assoc()): ?>
<tr class="table-tr" data-url="http://www.engineering.us/gena/details.php">
<td><?php echo $row['ChildName']; ?></td>
<td><?php echo $row['checkedin']; ?></td>
<td><?php echo $row['P_Id']; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
The "table-tr" class allows us to target the table rows in CSS. There is no need to use JavaScript for something so simple. Even changing the colour of the text within the table cell is very easy in CSS. This technique has the other advantage that it can be easily changed without having to modify any HTML, PHP or JavaScript - it's a purely stylistic thing.
.table-tr:hover {
background-color: red;
}
Finally, since you're using jQuery, we can take advantage of that new "js-table" class. The "js-" prefix states that the class is used for binding functionality and it should not be styled. This script simply bind a click event to the table that listens out for a user clicking on a table row with the data-url attribute. When that happens, the page is re-directed (using window.location instead of document.location - it may work either way, but I've always used window.location) to that URL. This allows you to change the URL later on or change it per row without having to change any of your JavaScript.
$(function () {
$(".js-table").on("click", "tr[data-url]", function () {
window.location = $(this).data("url");
});
});
I hope that helps.
Consider using the following code:
$("#Table tr").click(function () {
$(this).addClass('selected').siblings().removeClass('selected');
var value = $(this).find('td:first').html();
window.location.href = "url";
});
Where Table is the your table name and url is the address you want to navigate to
I think you should try to do that with JQuery.
HTML
<tr class="table-tr"></tr>
JQuery
$('.table-tr').on('click', function(){
window.location = "your url here";
});
Please Change your code with this
<tr onclick="window.location.href=www.engineering.us/gena/details.php">
Related
echo "<tr onclick='window.location=("www.google.com")'>
<td>something</td>
<td>something</td>
</tr>"
i have written code like this but it is not working.i dont know where to put single quotes and double qoutes.
I dont know how to write onclick for in php
please suggest me
Avoid using echo for HTML. Leave PHP mode and go into output mode.
Avoid using nested literal values. Write each language as a separate variable, use suitable escaping functions to add whatever quotes you need and then put them together.
By keeping everything as separate layers and dealing with them one at a time, and by using functions instead of trying to write your escapes manually, you make things much easier to maintain.
$url = "http://www.google.com";
$js_string_literal_url = json_encode($url);
$js = "window.location = $js_string_literal_url";
$html_safe_js = htmlspecialchars($js);
?>
<tr onclick="<?php echo $html_safe_js; ?>">
<td>something</td>
<td>something</td>
</tr>
That said, you should also avoid:
Features which depend entirely on JS
onclick attributes
Write HTML that works, and then enhance with JS.
If you want to link somewhere: use a link:
<tr>
<td>something</td>
<td>something</td>
</tr>
If you want to make that link work (using JS) for the whole table row, bind an event listener that looks for clicks, and then find the first link in the row that was clicked on.
document.querySelector("table").addEventListener(follow_link_in_row);
function follow_link_in_row(event) {
var table_row = event.target;
while (table_row && table_row.tagName.toLowerCase() !== "tr") {
table_row = table_row.parentNode;
}
if (!table_row) { return; }
var link = table_row.querySelector("a[href]");
var url = link.href;
window.location = url;
}
I am a beginner and somehow made to get the query (php & Mysql) I want and using echo i got the output as few lines without difficulty. But now I want the output inside the cell of a table. I tried something like this:
This does not work:
<tr>
<th>subject</th>
<th>grade</th>
</tr>";
echo "<tr>";
echo "<td>".$Row['name1']."</td>;
echo "<td>".$Row['subject1'].</td>";
echo "</tr>";
echo "</table>";
Whereas this work:
echo $line['name1']."<tr></td>"."";
echo $line['subject1']."<tr></td>"."";
The echo $line statement echoes the value of name1 and subject1 without any difficulty. but the echo Row is not showing the output. As my data has only one row I dont have to use any loop. I actually want two fields in first row (name1 and subject1) and then in next row the fields of name2 and subject2 and till name7, subject7. It looks like the format inside the table is wrong. Could someone help me plz?
First of all replace
echo "<td>".$Row['name1']."</td>;
with
echo "<td>".$Row['name1']."</td>";
you are missing (") at the end before (;)
Updated with the missing table tag. Try this
<?php
echo '<table>';
echo '<tr>';
echo '<th>subject</th>';
echo '<th>grade</th>';
echo '</tr>';
echo "<tr>";
echo "<td>".$Row['name1']."</td>";
echo "<td>".$Row['subject1']."</td>";
echo "</tr>";
echo "</table>";
?>
Just to expand the current answers, I'd suggest you use a single echo and concatenate the strings or even better, just use one single string and concatenate only the necessary variables:
<?php
echo '
<table>
<tr>
<th>subject</th>
<th>grade</th>
</tr>
<tr>
<td>'.$Row['name1'].'</td>
<td>'.$Row['subject1'].'</td>
</tr>
</table>';
?>
This of course works better if the amount of PHP code is greater than the amount of HTML code. But if you were to write more HTML than PHP, it'd make more sense to just open and close <?php?> tags and echoing the variable you want.
I used an answer instead of a comment for the sake of the example. Feel free to try this approach when you are dealing with several html elements and need to insert your values within them.
I want to create a site, which displays a lot of records from a database. To make it more reader-friendly, I want to use a styling. One record is white background, the next blue, the next hhite again.
So I tried this:
<?php while ($info = mysql_fetch_array($data)){
PRINT "<tr>";
PRINT "<td>" .$info['articlenr']. "</td>";
PRINT "<td>" .$info['stock']. "</td>";
PRINT "</tr>";
PRINT "<tr>";
PRINT "<td bgcolor=#0066FF>" .$info['articlenr']. "</td>";
PRINT "<td bgcolor=#0066FF>" .$info['stock']. "</td>";
PRINT "</tr>";
}
?>
This works for the view, but the problem is, the blue record is the same as the white, not the next one, it just doubles the record and make it another color.
How can I do this right?
Use :nth-of-type(even) to get even/odd combination of color's.
Here is a demo example:
html:
<table>
<tr><td>item1</td>
</tr>
<tr><td>item2</td>
</tr>
<tr><td>item3</td>
</tr>
<tr><td>item4</td>
</tr>
</table>
css:
tr:nth-of-type(even) { background-color: #0066FF; }
Demo
If you want to do this in PHP, you could do it like this :
<?php
$iter = 0;
$color1 = 'red'; //can se hex code too, like #0066FF;
$color2 = 'blue';
while ($info = mysql_fetch_array($data))
{
echo '<tr style="background-color:'.( ($iter%2==0) ? $color1 : $color2).';">';
// rest of the printing stuff
$iter++;
}
?>
Statement
($iter%2==0) ? $color1 : $color2
does this : it asks the question whether iterator (or row number) is even. If yes, the it takes color1. If not (row is uneven) it takes the second color.
PHP Smarty is good for doing this kind of stuff (iterating over colors and styles), but it may be difficult for beginners.
Please go through this links:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started
http://css-tricks.com/complete-guide-table-element/
Including CSS in .php file
Can I offer some advice on your code? Your approach mixes PHP and HTML in a way that makes it difficult for your development environment to parse your HTML, and you can achieve the same with less keystrokes! Consider this approach instead:
<?php while ($info = mysql_fetch_array($data)): ?>
<tr>
<td><?php echo $info['articlenr'] ?></td>
<td><?php echo $info['stock'] ?></td>
</tr>
<?php endwhile ?>
Changes I've made:
Removed the duplicate row
Rendered everything in HTML mode, and opened PHP tags just for PHP code
Switched the loop to the colon form, which is often thought to be clearer in templates. Do carry on using the brace approach, however, in large chunks of code (e.g. classes)
Written PHP keywords in lower case
Used echo rather than print
Combine this with Joke_Sense10's answer for a full solution.
Below is an order form that I have been working on to learn how html, javascript, php and mysql interact with each other.
https://www.dropbox.com/s/7zpok07w1bygu60/Screen%20Shot%202012-08-12%20at%2010.28.41%20AM.png
The goal of this table is to have the user input a number in the quantity field and on the change, javascript will automatically update the total cost of that particular product. The kicker is that I want the list to be populated from a mysql database table.
Here is the html, javascript and php code that I am using.
HTML
<table>
<tr>
<td>
Product
</td>
<td>
Description
</td>
<td>
Quantity
</td>
<td>
Price
</td>
<td>
Total Cost
</td>
</tr>
<?php include("pop_prodlist.php"); ?>
<tr>
<td>Totals</td>
<td></td>
<td>TotalQuantity</td>
<td></td>
<td>TotalCost</td>
</tr>
</table>
PHP
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['P_Name'] . "</td>";
echo "<td>" . $row['P_Description'] . "</td>";
echo "<td>
<input type='text' size='3' name='" . $row['P_Name'] . "_Quantity'
onChange='calcCost()'/>
</td>";
echo "<td>" . $row['P_Cost'] . "</td>";
echo "<td id='" . $row['P_Name'] . "_Cost'>0.00</td>";
echo "</tr>";
}
Javascript calcCost() function
function calcCost()
{
var theForm = document.forms['productform'];
/*
COMMENTED OUT FOR TIME BEING
var pquan = theForm.elements[productname + '_Quantity'].value;
var costperquan = '15.99';
var ptotalcost = costperquan * pquan;
*/
var divobj = document.getElementById('Basketball_Cost');
divobj.innerHTML = "helloworld";
}
The issue that I am currently having is that I cannot figure out how to place an unique argument into the javascript function calcCost from the php script echo depending on the product name decided from the mysql query.
Also, for the sake of educating myself, I am curious as to what a better solution would be/look like. I doubt that this is the most elegant solution to the problem and I would like to see some better solutions.
Jay, it seems like you understand how PHP, MySQL and JavaScript work together - that is good.
Personally, I can really reccommend using jQuery (JavaScript Library). It is widely used all over the web, very common, powerful and easy to understand and use. Although there are people that say it is not good to use a library of a language, before you know the language, jQuery helped me to understand JavaScript better because of its easy to follow syntax.
So, to use jQuery, you just need to add the source scripts in your html file. Why do I recommend jQuery? It will also allow you to use ajax (loading content, also from the database, without having the page to refresh) very easily.
jQuery will also allow you to separate your click events from your HTML. Basically you assign an id or class attribute to the HTML elements and this is your reference in your js, so no onclick="" required.
As for your HTML: it is highly recommended to not use table-layouts. In a time of HTML5 and CSS3 there are excellent replacements for tables.
If you need more help let me know in the comment, I'm glad to help...
Is there a way I can use Jquery to insert '' tags after every three dynamically generated table cells so that I end up with a dynamic three column table?
Please excuse my lack of knowledge, I'm literally trying to write my first jquery script ever, so I know absolutely nothing. I know php and I have a table that has a loop within it that is dynamically creating <td></td> with the information inside each tag. In other words it is dynamically creating the table cells within a static <tr></tr> tag. The problem is that it keeps outputing tables without breaking them up into rows which leaves me with a bunch of columns. I've read other articles on this but none seem to have the exact same problem as I do, and I am still struggling to understand how to write custom Jquery code.
The php code is very long and is full of numerous if statements and other functions so I'm not going to post it here but just to make it a little simpler, I made miniature mockup of what I'm trying to do.
<table id="mytable" width="266" border="1" cellspacing="10" cellpadding="10">
<tr>
<?php
$x=0;
while (have_products($x)) {
echo '<td>' . somelongassfunction() . '</td>';
$x++;
if (fmod($x,3) == 0) {
echo '</tr><tr>';
continue;
}
if ($x==20){
echo '</tr>';
}
}
function somelongassfunction(){
return 'Hello';
}
function have_products($a){
return $a<=20;
}
?>
</table>
This code loops and dynamically adds table cells up to the limit I give it which would represent my database items. Every three rows, it adds either a <tr></tr> or just a </tr> depending on whether the loop continues or not. This creates a 3 column table. I can't apply this code for my script because it is a very long and complex script that has a lot of if statements and functions. There is no way of doing it like this without breaking the code or having to rewrite everything from scratch all over again.
Is there anyway I can append the tr tags dynamically with Jquery and how would I go about to applying this to?
The jQuery approach would be to loop through all of the tabs, and add them to newly created tags, which themselves are added to the html of the table. Roughly:
var thisCount=0;
var currenttag="<tr />";
var table=$("table");
$("td").each(function ()
{
if(thiscount==2)
{
table.appendChild(currenttag);
thisCount=0;
currenttag="<tr />";
}
currenttag.appendChild(this);
}
( this is just to give an idea, not intended as a formal JQ answer. If anyone wants to edit it so it works fully, feel free ).
You can use a selector to select every third row:
$('#table_id > tr:nth-child(3n)').whatever_function()
However if you are trying to append end /tr tags, try doing it in PHP using a counter that resets itself (this code should get you started):
echo "<tr>";
$x = 0;
$y = 0;
while (have_products($x)) {
echo '<td>' . somelongassfunction() . '</td>';
$y++;
if ($y == 3) {
$y = 0;
echo "</tr><tr>";
}
$x++;
}
echo "</tr>";