Php 如何创建jquery表?

Php 如何创建jquery表?,php,jquery,mysql,Php,Jquery,Mysql,我想使用jquery在表中显示来自mysql的数据。 我在下面创建了一个php表,但我想使用jquery instate在#mytable的表单上显示该表。 我希望我的代码不会太混乱 > echo "<table class=\"optable\"> <tr> <th>Device ID</th> <th>Name</th> <th>Type</th> <th>Make &

我想使用jquery在表中显示来自mysql的数据。 我在下面创建了一个php表,但我想使用jquery instate在#mytable的表单上显示该表。 我希望我的代码不会太混乱

> echo "<table class=\"optable\">
<tr>
<th>Device ID</th>
<th>Name</th>
<th>Type</th>
<th>Make & Model</th>
<th>Memory</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['device_id'] . "</td>";
  echo "<td>" . $row['device_name'] . "</td>";
  echo "<td>" . $row['device_type'] . "</td>";
  echo "<td>" . $row['make_model'] . "</td>";
  echo "<td>" . $row['memory'] . "</td>";
  echo "</tr>";
}
echo "</table>";
下面是我用来从mysql收集数据的php代码

name6.php

<?php

if (!empty($_POST['name'])) {
    require '../db/connect6.php';
    $safe_name = mysql_real_escape_string(trim($_POST['name']));
    $query = mysql_query("
      SELECT * FROM `oz2ts_users`
      WHERE  `oz2ts_users`.`id` = '". $safe_name ."'
  ");

    if (mysql_num_rows($query) > 0) {
      $row = mysql_fetch_assoc($query);
      json($row);
   } else {
      json(null);
  }
 }

function json ($array) {
    header("Content-Type: application/json");
    echo json_encode($array);
}

?>
name6.php

我认为您的name6.php代码应该是:

<?php

if (!empty($_POST['name'])) {
        require '../db/connect6.php';
        $safe_name = mysql_real_escape_string(trim($_POST['name']));
        $query = mysql_query("
              SELECT * FROM `oz2ts_users`
              WHERE  `oz2ts_users`.`id` = '". $safe_name ."'
        ");

        if (mysql_num_rows($query) > 0) {
            $row = mysql_fetch_assoc($query);
            echo "<table class=\"optable\">
                <tr>
                <th>Device ID</th>
                <th>Name</th>
                <th>Type</th>
                <th>Make & Model</th>
                <th>Memory</th>
            </tr>";

            while($row = mysqli_fetch_array($result)) {
                  echo "<tr>";
                  echo "<td>" . $row['device_id'] . "</td>";
                  echo "<td>" . $row['device_name'] . "</td>";
                  echo "<td>" . $row['device_type'] . "</td>";
                  echo "<td>" . $row['make_model'] . "</td>";
                  echo "<td>" . $row['memory'] . "</td>";
                  echo "</tr>";
            }

            echo "</table>";
       } else {
          echo 'null';
      }
 }

希望这有帮助。

使用
append

function updatefrm2($name) {
    var name = $("#" + $name).val();
    if ($.trim(name) !='') {
    $.post('http://exemple.com/action/subs/name6.php', {name: name},     function(data) {

    $('#yourtableid').append('<tr>\
                               <td>'+data.device_id+'</td>\
                               <td>'+data.device_name+'</td>\
                               <td>'+data.device_type+'</td>\
                               <td>'+data.make_model+'</td>\
                               <td>'+data.memory+'</td>\
                              </tr>');
    });
 }    
}
函数updatefrm2($name){
var name=$(“#”+$name).val();
如果($.trim(名称)!=“”){
$.post($)http://exemple.com/action/subs/name6.php“,{name:name},函数(数据){
$('#yourtableid')。追加('\
“+data.device_id+”\
“+数据。设备名称+”\
“+数据。设备类型+”\
“+数据。制作_模型+”\
“+数据.内存+”\
');
});
}    
}

我认为OP希望向div添加一个完整的表,而不是向表中添加一行。哦,我想是这样的,因为在name6.php中,他只获取第一行,然后将其作为Json返回。。。所以Zakaria的回答可能对他有帮助Zakaria的回答很有效。它显示了我想要的所有行的表格。
<?php

if (!empty($_POST['name'])) {
        require '../db/connect6.php';
        $safe_name = mysql_real_escape_string(trim($_POST['name']));
        $query = mysql_query("
              SELECT * FROM `oz2ts_users`
              WHERE  `oz2ts_users`.`id` = '". $safe_name ."'
        ");

        if (mysql_num_rows($query) > 0) {
            $row = mysql_fetch_assoc($query);
            echo "<table class=\"optable\">
                <tr>
                <th>Device ID</th>
                <th>Name</th>
                <th>Type</th>
                <th>Make & Model</th>
                <th>Memory</th>
            </tr>";

            while($row = mysqli_fetch_array($result)) {
                  echo "<tr>";
                  echo "<td>" . $row['device_id'] . "</td>";
                  echo "<td>" . $row['device_name'] . "</td>";
                  echo "<td>" . $row['device_type'] . "</td>";
                  echo "<td>" . $row['make_model'] . "</td>";
                  echo "<td>" . $row['memory'] . "</td>";
                  echo "</tr>";
            }

            echo "</table>";
       } else {
          echo 'null';
      }
 }
function updatefrm2($name) {
    var name = $("#" + $name).val();

    if ($.trim(name) !='') {
        $.post("http://exemple.com/action/subs/name6.php", {name: name}, function(data) {

            $("#mytable").html(data);

        });
    }    
}
function updatefrm2($name) {
    var name = $("#" + $name).val();
    if ($.trim(name) !='') {
    $.post('http://exemple.com/action/subs/name6.php', {name: name},     function(data) {

    $('#yourtableid').append('<tr>\
                               <td>'+data.device_id+'</td>\
                               <td>'+data.device_name+'</td>\
                               <td>'+data.device_type+'</td>\
                               <td>'+data.make_model+'</td>\
                               <td>'+data.memory+'</td>\
                              </tr>');
    });
 }    
}