【问题标题】:Modifying the AJAX PHP database example修改 AJAX PHP 数据库示例
【发布时间】:2012-04-15 12:48:49
【问题描述】:

我想在这里制作类似这样的节目:

http://www.w3schools.com/PHP/php_ajax_database.asp

但是不是示例中显示的下拉列表,是否可以将其更改为示例格式的表格,当我单击第 1 类时,它将显示第 1 类的详细信息...详细信息在我的数据库中它来自 phpmyadmin:

在此先感谢...非常感谢您的帮助

这对吗?

<?php
include ('staffheader.php');
?>
<div id="head">Permit Structure</div>
<div class="contents">
<div id="class_data">
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>Road Based</td>
<td>Proving Ground PG</td>
<td>Off Road OR</td>
<td>Towing TT</td>
</tr>
<tr id="Class_1">
<td> <a href='#' class='classlink' title='1'>Class 1</a></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Class 2</td>
<td>CAT 2PG</td>
<td>CAT 1OR</td>
<td>CAT 2TT</td>
</tr>
<tr>
<td>Class 3</td>
<td>CAT 3PG</td>
<td>CAT 2OR</td>
<td>CAT 3TT</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>CAT 4PG</td>
<td>CAT 3OR</td>
<td>&nbsp;</td>
</tr>
</table>
</div>
<div id="instruction">Click on the Class or Category to view information on it</div>
</div>
<div id='detailtable'></div>
<?php
include('allfooter.php');
?>

loadergetclassinfo.php:

<?php
$class_id = empty($_POST["class_id"]) ? 1 : $_POST["class_id"];
   $con = mysql_connect("localhost", "root", "cailing8195") or die ("Unable to connect to MySQL Server " . mysql_error()); 
if(is_resource($con))
    $db = mysql_select_db("jlr", $con) or die( "Unable to select database " . mysql_error());
$query = "SELECT PTYPE, TYPE, PREREQ, DES FROM type WHERE TYID=" . $class_id;
$res = mysql_query($query);
$arr_data = mysql_fetch_assoc($res);
mysql_close($con);

foreach ($arr_data as $data)
$html = "<table>\n";
$html .= "<tr><th>Type ID</th><th>Permit Type</th><th>Categories</th><th>Pre-Requisisite</th><th>Description</th></tr>\n";
$html .= "<tr><td>" . $class_id . "</td><td>" . $data['PTYPE'] . "</td><td>" .    $data['TYPE'] . "</td><td>" . $data['PREREQ'] . "</td><td>" . $data['DES'] . "</td>    </tr>\n";
 $html .= "</table>\n";

echo $html;
?>

JQuery(在 javascript.js 中):

 $(function() {
$('a.class_link').click(function() {
    var class_id = $(this).attr('title');
    $.post("loadergetclassinfo.php", {class: class_id}, function(result){
    $('#detail_table').html(result);
    });
   });
});

我也在我的头 php 文件中添加了这个:

  <script src="javascript.js"></script>
 <script type='text/javascript'     src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>

【问题讨论】:

  • 我发现了更多错误 - 请参阅下面的更新。
  • 将 jQuery 代码中的 $('#detail_table') 更改为 $('#detailtable')
  • 我的 jQuery 代码中的一个错误:将 $('a.class_link') 更改为 $('a.classlink')
  • 还要更改脚本标签的顺序:jQuery 库源代码应该在标题中的最前面,在脚本文件之前。
  • 不要将 jQuery 添加到您的 php 文件的标题中 - 它只需要在您的 HTML 文件(带有链接的文件)中。

标签: php ajax database html


【解决方案1】:

如果您希望单击链接所在的行填充数据库中的数据,请执行以下操作(未经测试,但这是要点):

HTML:

<tr id='class_1'>
    <td><a href='#' class='classlink' title='1'>Class 1</a></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>

jQuery:

$(function() {
    $('a.classlink').click(function() {
        var class_id = $(this).attr('title');
        $.post("loadergetclassinfo.php", {class_id: class_id}, function(result){
            $('#class_' + class_id).html(result);
        });
    });
});

loadergetclassinfo.php:

<?php
    $class_id = empty($_POST["class_id"]) ? 1 : $_POST["class_id"];
    $con = mysql_connect("localhost", "your_MySQL_username", "your_MySQL_password") or die ("Unable to connect to MySQL Server " . mysql_error()); 
    if(is_resource($con))
        $db = mysql_select_db("your_MySQL_database", $con) or die( "Unable to select database " . mysql_error());
    $query = "SELECT data1, data2, data3 FROM your_data_table WHERE class=" . $class_id;
    $res = mysql_query($query);
    $arr_data = mysql_fetch_assoc($res);
    mysql_close($con);

    $html = "<td><a href='#' class='classlink' title='$class_id'>Class $class_id</a></td>";
    foreach ($arr_data as $data)
        $html .= "<td>" . $data . "</td>\n";

    echo $html;
?>

更新:

如果您希望“Class x”数据出现在 HTML 页面的其他位置,您可以执行以下操作:

将此添加到您的 HTML:

<div id='class_data'></div>

像这样改变上面的jQuery:

$.post("loadergetclassinfo.php", {class: class_id}, function(result){
    $('#class_data').html(result);
});

把上面的 php 代码改成这样(或者你可以用一个列表或者你想在这里看到的任何东西):

$html = "<table>\n";
$html .= "<tr><th>Class Number</th><th>Data 1</th><th>Data 2</th><th>Data 3</th></tr>\n";
$html .= "<tr><td>" . $class_id . "</td><td>" . $data['data1'] . "</td><td>" . $data['data2'] . "</td><td>" . $data['data3'] . "</td></tr>\n";
$html .= "</table>\n";

echo $html;

这是假设您有名为data1 等的列,并且您的主索引称为“类”。只需将其更改为您的情况即可。

更新对您的编辑的回应:

HTML 代码的结尾是:

<div id='detailtable'></div>

编辑这个 jQuery 语句:

$.post("loadergetclassinfo.php", {class: class_id}, function(result){
    $('#detailtable').html(result);
});

最后,从您的 HTML 表格下方删除 php 代码,并将其放入与 HTML 文件位于同一目录的名为“loadergetclassinfo.php”的自己的文件中。

另外,这是错误的(抱歉,我的代码中有错误):

$class_id = empty($_POST["class_id"]) ? 1 : $_POST["class"];

应该是:

$class_id = empty($_POST["class_id"]) ? 1 : $_POST["class_id"];

还将详细信息表代码更改为:

$html = "<table>\n";
$html .= "<tr><th>Type ID</th><th>Permit Type</th><th>Categories</th><th>Pre-    Requisisite</th><th>Description</th></tr>\n";
$html .= "<tr><td>" . $class_id . "</td><td>" . $data['PTYPE'] . "</td><td>" .     $data['TYPE'] . "</td><td>" . $data['PREREQ'] . "</td><td>" . $data['DES'] . "</td></tr>\n";
$html .= "</table>\n";

【讨论】:

  • 感谢您的帮助...嗯,但您的意思是单击时信息将填充新表格还是填充当前表格?再次感谢您的帮助
  • @Hubert:我的意思是,当前表 - 因为“1 类”行目前是空的。但是您可以在 jQuery 结果回调函数中选择任何特定元素的 id,以使用您从 php 脚本传递的任何 html 更新该元素。
  • 嗯...对不起,因为我不太了解 jquery 代码...如果我希望它出现一个新的表格底部...我该如何编辑代码?跨度>
  • 对不起这个菜鸟问题...但是我如何在 php 文件中添加 jquery ???我需要使用像&lt;/script&gt; 这样的标签吗?
  • @Hubert:你把$('#detail_table')改成$('#detailtable'),把$('a.class_link')改成$('a.classlink')了吗?
【解决方案2】:

你可以尝试使用 jquery 的 ajax 函数,它可能是这样的,你可以修改它。

 <table width="100%" border="1" cellspacing="0" cellpadding="0">
            <tr>
            <td>Road Based</td>
            <td>Proving Ground PG</td>
            <td>Off Road OR</td>
            <td>Towing TT</td>
            </tr>
            <tr>
            <td onclick="getPage(this)" >Class 1</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            </tr>
            <tr>



        <div id='content'></div>
        <script type="text/javascript">
        function getPage(class) {
            //generate the parameter for the php script
            var data = 'page=' + document.location.hash.replace(/^.*#/, '');
            $.ajax({
                url: "loadergetclassinfo.php", 
                type: "GET",       
                data: (class).innerText,    
                cache: false,
                success: function (html) { 
                    //add the content retrieved from ajax and put it in the #content div
                    $('#content').html(html);  
                    //display the body with fadeIn transition
                    $('#content').fadeIn('slow');      
                }      
            });
        }
</script>

【讨论】:

    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多