开发者

Problem getting results from mysql database via php and jquery

I try to realize the following:

I generate via php a table of two fields (about training lessons) - lessons' name and location. This table has several rows and i implemented an like this:

        <table border='0' id='testing'>
            <thead>
                <tr class='small_title_bold'>
                    <th>Formation</th><th align='center'>Niveau</th><th align='center'>Ville</th>
                </tr>
            </thead>
            <tbody>        
              <?php
              db_connect();

              if (isset($etablissement)){
                $sql_find_id_etablissement = "select id_etablissement, subdesignation, address1, address2, zip, city, town, department, phone, fax, www from etablissements where designation = '$etablissement' and id_region='$id_region'";
                $result_find_id_etablissement = mysql_query($sql_find_id_etablissement, $connection) or die('error');
                while ($row_find_id_etablissement = mysql_fetch_row($result_find_id_etablissement)){
                $id_etablissement  = $row_find_id_etablissement[0];
                $subdesignation = $row_find_id_etab开发者_运维问答lissement[1];            
                $address1 = $row_find_id_etablissement[2];
                $address2 = $row_find_id_etablissement[3];          
                $zip = $row_find_id_etablissement[4];
                $city = $row_find_id_etablissement[5];
                $town = $row_find_id_etablissement[6];          
                $department = $row_find_id_etablissement[7];            
                $phone = $row_find_id_etablissement[8];
                $fax = $row_find_id_etablissement[9];
                $www = $row_find_id_etablissement[10];

                print "<tr class='small_subtitle_bold'><td colspan='3' align='center'>".$id_etablissement."-". $subdesignation."</td></tr>";        
                $sql_find_master = "select id_master, designation, master_level from masters where id_etablissement = '$id_etablissement' order by designation, master_level";
                $result_find_master = mysql_query($sql_find_master, $connection) or die('error');

                $count=mysql_num_rows($result_find_master);

                for($i = 0; $i < $count; $i++) {                
                    $row_find_master = mysql_fetch_row($result_find_master);
                    $id_master = $row_find_master[0];   
                    $designation = $row_find_master[1]; 
                    $level = $row_find_master[2];
                    if($i % 2) {
                        print "<tr class='univ' bgcolor='#A4D2FD'>";
                        print "<td><a class='univ' href='master-details.php?master-id=".$id_master."&etablissement-id=".$id_etablissement."' onclick='popup('master-details.php?master=".$designation."')>".$designation."</a></td><td class='small_text' align='center'>".$level."</td><td class='small_text' align='center'>".$town."</td>";
                        print "</tr>";
                    }
                    else {
                        print "<tr class='univ' bgcolor='#FFFFFF'>";
                        print "<td><a class='univ' href='#' onClick='javascript:swapContent(".$id_master.",".$id_etablissement.")'>".$designation."</a></td><td class='small_text' align='center'>".$level."</td><td class='small_text' align='center'>".$town."</td>";
                        print "</tr>";          
                    }
                }
            }
        }
        ?>
        </tbody>
    </table>

When a user clicks a row, i want to run a mysql query and present the results (the lesson's details) in a separated located in the same page without reloading the page.

I placed the following script in my head section:

<script language="JavaScript" type="text/javascript">
function swapContent(id_master, id_etablissement) {
var url = "master-details.php";
$.post(url, {id_master: id_master, id_etablissement: id_etablissement} ,function(data) {
          var content = $( data ).find( '#content', function () {alert('Query was performed.');} );
      $( "#queryResults" ).empty().append( content );
});
}
</script>

I want my jquery script to run a php file named master-details with the parameters id_master and id_etablissement and returns the results in a DIV section named queryResults.

However, when I clicked on a row in my table, nothing happened.

Any help would be greatly appreciated.


Something like this:

jQuery("tr").live("click", function(){
   $.post///what you already have.
});

You just need a way to tell it to do something when you click.


...when I clicked on a row in my table, nothing happened.

Something happened, even if it was not apparent. The task is to determine exactly what happened, and why it didn't produce the expected results.

There are several steps you can take to determine what really happened:

  1. Check for Javascript errors. You can use Firebug for Firefox or the built in Developer Tools for Chrome to view the Javascript console. If a Javascript error occurred, it will be shown there.

  2. Determine if the PHP script was executed. You can do this by checking your webserver's logs, or by looking at the "Network" tab of the aforementioned tools.

  3. If the PHP script is successfully executing, determine what output it produces. You can do this by running the script directly. Find the URL that your AJAX call references, and paste it into your browser's address bar. Is the output what you expect it to be?

  4. If the PHP script executes and returns appropriate results, check your Javascript selectors. Are you sure they reference the correct div? Does that div exist?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜