Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
    
What does the generated JavaScript look like? What does the JavaScript error console say? –  Quentin Jan 20 at 0:02
    
there is no error, it just doesn't display anything. If I replace the code: <?php $test->viewplane(); ?> with a normal string it works. –  gNazi Jan 20 at 0:04

3 Answers 3

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.

share|improve this answer
    
you are right about the ' character , but it is not the problem. Even after removing it or editing it. –  gNazi Jan 20 at 0:24

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.

share|improve this answer
    
yes, I figured it out ! ur right. Ajax was the answer ! –  gNazi Jan 20 at 2:53

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.