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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.