Jump to content

Sortable table in PHP using Javascript


fractal5

Recommended Posts

So I'm trying to use a php form to query results from an sql database and generate a sortable table. For the table sorting I am using a javascript from here:

 

 

The problem is that the javascript isn't working on the table generated from the php. But if I copy the output of the php into an html, the javascript sort works perfectly.

 

This is a basic sample code I made to test the function:

 

<?php

echo "<html><head><title>new page</title>";
echo '<script src="sortable.js"> </script></head>';

echo '<body>';		

echo "<table border='0' id='table' class='sortable'>";
echo "<thead>"; 
echo "<tr>";
echo "<th>Col1</th>";
echo "<th>Col2</th>";
echo "</tr></thead>";
echo "<tbody>";
echo "<tr>";
echo "<td>11</td>";
echo "<td>12</td>";
echo "</tr>";
echo "<tr>";
echo "<td>21</td>";
echo "<td>22</td>";
echo "</tr>";
echo "</tbody></table>";

echo "</body></html>";	
?>

 

So I'm confused as to whether I'm doing something wrong or missing something, or if there's a reason why a javascript function won't work through php.

 

I hope I have explained it clearly. Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/257351-sortable-table-in-php-using-javascript/
Share on other sites

Javascript works on HTML. HTML is the output of a PHP script (usually - sometimes the output may be XML, JSON or something else). So the fact that the HTML was generated by a PHP script is irrelevant. Javascript can't tell how it was generated one way or another.

 

So this says to me that you have some issue somewhere else in your code. Unfortunately, you didn't show us any other code, so I can't really say much more.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.