Iai*_*son 8 javascript php ajax jquery
好吧,这是我对阿贾克斯的第一次尝试,它让我疯狂,因为我真的无法理解它.我想要做的是从数据库填充客户的第一个框,然后使用customerID从select.php脚本中选择数据库中的所有vehicleID.正在发生的事情是客户框正在被选中,但是当选择客户时没有任何事情发生.
这是我的Test.php文件:
<?php include 'connectmysqli.php'; ?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Select test</title>
<script src="./js/jquery/jquery.js"></script>
<script type="text/javascript" charset="utf-8">
$$('#customer')
.on('change', function () {
$.getJSON('select.php', { customerId: $(this)
.val() }, function (data) {
var options = '';
for (var x = 0; x < data.length; x++) {
options += '<option value="' + data[x][
'id'] + '">' + data[x]['reg'] +
'</option>';
}
$('#vehicle')
.html(options);
});
});
</script>
</head>
<body>
<select id="customer">
<?php
$sql = <<<SQL
SELECT *
FROM `customers`
SQL;
if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']');}
while($row = $result->fetch_assoc()){
if ($row['bussinessname'] == ''){$name = $row['title'].' '.$name = $row['firstname'].' '.$name = $row['surname'];}else
{$name = $row['bussinessname'];}
echo '<option value="'.$row['customerID'].'">'.$name.'</option>';
}
echo '</select></p>'; ?>
</select>
<select id="vehicle">
</select>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我的select.php文件:
<?php include 'connectmysqli.php'; ?>
<?php
$id = $_GET['customerId'];
$sql = 'SELECT * FROM vehicles WHERE customerID = ' . $id;
$result = $db->query($sql);
$json = array();
while ($row = $result->fetch_assoc()) {
$json[] = array(
'id' => $row['vehicleID'],
'reg' => $row['reg'] // Don't you want the name?
);
}
echo json_encode($json);
?>
Run Code Online (Sandbox Code Playgroud)
我试图修改本教程以使用数据库,但到目前为止我都没有成功. http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/
在Chrome控制台中,我收到错误消息:
Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:235
chromeHidden.Port.dispatchOnDisconnect
Run Code Online (Sandbox Code Playgroud)
MrC*_*ode 19
在页面上呈现选择之前,正在分配您的客户选择更改事件.将事件处理程序移动到document.ready中:
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#customer').on('change', function (){
$.getJSON('select.php', {customerId: $(this).val()}, function(data){
var options = '';
for (var x = 0; x < data.length; x++) {
options += '<option value="' + data[x]['id'] + '">' + data[x]['reg'] + '</option>';
}
$('#vehicle').html(options);
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
我也换$$('#customer')了$('#customer').最后,修复SQL注入漏洞:
$sql = 'SELECT * FROM vehicles WHERE customerID = ' . (int)$id;
Run Code Online (Sandbox Code Playgroud)
将ID转换为int将阻止SQLi,但您应该考虑使用Prepared Statement.
您在问题中提到的错误与您的代码无关.它看起来与Chrome扩展程序有关.
不是问题的一部分,但这是一个改进的代码版本,用于构建车辆选项:
$.getJSON('select.php', {customerId: $(this).val()}, function(data){
var vehicle = $('#vehicle');
for (var x = 0; x < data.length; x++) {
vehicle.append(new Option(data[x].reg, data[x].id));
}
});
Run Code Online (Sandbox Code Playgroud)
改进是:
new Option(...)和.append()创建选项并将其添加到车辆选择中.像这样构建元素比创建原始HTML更好,因为这样,您不必担心数据中的字符<和>数据 - 这会破坏您当前代码的HTML.data[x].id)而不是data[x]['id'].| 归档时间: |
|
| 查看次数: |
32132 次 |
| 最近记录: |