我想使用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>";
形式
<html> <script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script> <script src="./action/scripts/script1.js" type="text/javascript"> </script> <div> <input id="name" type="text" name="name" onkeyup="updatefrm2('name')" /> </div> <div id="mytable"></div> </html>
这是我要修改的jquery脚本以显示表
script1.js
function updatefrm2($name) {
var name = $("#" + $name).val();
if ($.trim(name) !='') {
$.post('http://exemple.com/action/subs/name6.php', {name: name}, function(data) {
$('#member_id').val(data['id']);
$('#member_id2').val(data['id']);
$('#member_id_clog').val(data['id']);
$('#member_id').val(data['id']);
$('.price2').val(data['id']);
});
}
}
这是我用来从mysql收集dada的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);
}
?>
答案 0 :(得分:1)
我认为你的 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';
}
}
script1.js 应为:
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);
});
}
}
希望这有帮助。
答案 1 :(得分:0)
使用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>');
});
}
}