-1

I am trying to display a database table in html. I have the following php code that will get the table and return in an html table format and it has no errors:
function viewPlane() {

if(!$this->DBLogin())
    {
        $this->HandleError("Database login failed!");
        return false;
    }

$qry = "select * from airplane";
$result = mysql_query($qry,$this->connection); 

echo "<table border='1'>
<tr>
<th>Registration Number</th>
<th>Model Number</th>
</tr>";

while($row=mysql_fetch_array($result))
{

echo "<tr>";
echo "<td>" . $row['Registration_Number'] . "</td>";
echo "<td>" . $row['Model_Number'] . "</td>";
echo "</tr>";

}
echo "</table>";

}  

In the html file, I have the following javascript code that calls the php function and Supposedly adds it to the div:

  <script type="text/javascript">  
  $(document).ready(function(){
    $("#menu_active").click(function(){  
       $("#table").after('<?php $test->viewplane(); ?>')  
    });
});  
</script>

The code:

<?php $test->viewplane(); ?>  

works when put among the html, so why is not working ?

Primata Lógico
  • 725
  • 4
  • 12

3 Answers3

0

There could be other problems (it helps if you look at your generated JavaScript and your JavaScript error console), but at the very least, this:

<table border='1'>
<tr>

… contains ' characters which cannot be used unescaped in a JavaScript string literal delimited with ' characters and new line characters which cannot be used unescaped in any JavaScript string literal.

You need to add some \s, or possibly switch to using the json_encode function instead of building your string literal by hand.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

I think it's not correct to write $("#table").after('<?php $test->viewplane(); ?>') the javascript is executed in the browser, so during the js executing process, it just treat <?php $test->viewplane(); ?> as a simple string.

I mean when you click "#menu_active", the after method is invoked, but the after method is NOT responsible to send a http request to the server side to get the content.

To achieve what you want, you might need a Ajax call when the "#menu_active" is clicked. Something like $("selector here").load method will be helpful.

Merlin
  • 125
  • 7
0

I don't see a table with an id of table (#table) in your code:

$("#table").after('<?php $test->viewplane(); ?>')

If a selector doesn't work in jQuery, there's no error it just doesn't do anything.

Also, this is probably irrelevant here, but sometimes PHP doesn't evaluate variables contained inside of single quotes. Not sure if that matters inside of HTML.

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109