Javascript 如何使用html、jQuery构建控件数组
我对html-php-jquery-ajax组合的基本知识了解不多,但我在VB方面有很长的时间。我可以用VB术语最好地描述我正在尝试做的事情,但我需要在一个已经使用jQuery、ajax和php接口的html/php页面中进行描述 我正在查询数据库并返回id和描述。我想动态构建一个表示返回数据的控制数组 此函数调用从按钮onclick事件读取的dbJavascript 如何使用html、jQuery构建控件数组,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我对html-php-jquery-ajax组合的基本知识了解不多,但我在VB方面有很长的时间。我可以用VB术语最好地描述我正在尝试做的事情,但我需要在一个已经使用jQuery、ajax和php接口的html/php页面中进行描述 我正在查询数据库并返回id和描述。我想动态构建一个表示返回数据的控制数组 此函数调用从按钮onclick事件读取的db <script type="text/javascript"> function loadio(){ v
<script type="text/javascript">
function loadio(){
var request = $.ajax({
url: "/db/loadio.php", type: "POST", datatype: "html"
}); //request
request.done(function(result) {
$('#status').html("Result: " + result );
}); //request.done
request.fail(function(jqXHR, textStatus) {
$('#status').html("Request failed: " + textStatus + " " + jqXHR.status );
}); //request.fail
}; //loadio
</script>
函数loadio(){
var请求=$.ajax({
url:“/db/loadio.php”,类型:“POST”,数据类型:“html”
});//请求
请求完成(函数(结果){
$('#status').html(“结果:+Result”);
});//请求完成
请求失败(函数(jqXHR,textStatus){
$('#status').html(“请求失败:“+textStatus+”“+jqXHR.status”);
});//请求失败
}; //负荷
它调用此php文件并从中返回数据
<?php
$con = new mysqli('localhost', 'user', 'password', 'dbtable');
$stmt = $con->prepare('select id, description from iotable where iotype = 1');
$stmt->execute();
$stmt->bind_result($id, $description);
while ($stmt->fetch()){
echo $id, $description, "<br/>";
}
$stmt->close();
$con->close();
?>
所以我想做的是:
构建表示返回的$description的标签数组,然后
构建一个相应的按钮数组,以某种方式分配#id,这样当单击时,它们可以使用#id作为变量传递给公共函数
(例如,警报(“单击了“+#id+”按钮”))
如果必须在.NET中执行此操作,我会创建一个包含控件和事件的类,并根据需要创建该类的新实例,将所有必需的变量存储在该实例中
所以我的问题是:
我应该如何使用html、php、jQuery和ajax来实现这一点?
我可以创建动态控制阵列吗?
我需要考虑一种完全不同的方法吗
提前感谢。您要做的是用php填充数组,然后以JSON格式发送回去:
<?php
header('Content-type: application/json'); //we tell the browser that we are going to send json
$con = new mysqli('localhost', 'user', 'password', 'dbtable');
$stmt = $con->prepare('select id, description from iotable where iotype = 1');
$stmt->execute();
$stmt->bind_result($id, $description);
$results = array(); //array declaration
while ($stmt->fetch()){
$results[$id] = $description; //associative array : key = id, value = description
}
$stmt->close();
$con->close();
echo json_encode($results); //we send (ie print) json-encoded array $results
exit(); //not mandatory, just to precise that the script has to stop here
?>
谢谢你,奥利弗。我要试一试。这是否限制了添加其他变量的范围?例如,假设我转到id、description、type等,那么最好的解决方案是创建一个PHP类来描述从数据库返回的数据。然后为得到的每一行实例化这个类,并将所有对象放入结果数组中。您以同样的方式解码它,您将得到一个javascript对象数组,可以按原样使用。@Arius007有什么消息吗?奥利弗,我已经尝试过了,并对其进行了一些修改,但到目前为止,我还没有让它工作。$。parseJSON($results)根本不起作用(似乎导致了一个阻止函数完成的错误),但json_decode(result)似乎做了一些事情,即让函数运行。我希望明天再回来。第一部分工作正常。它正在构建阵列,我查看了阵列以验证这一点。这个阶段的问题是我不知道如何正确访问返回的数据。我用这个函数调用数据,但我不知道var应该包含什么结果:[code]var request=$.ajax({url:/db/loaddata3.php],type:“POST”,datatype:“json”,data:str});request.done(函数(结果){file\u put\u contents('php://stderr,print_r(result,TRUE));});[/code]我试图将结果作为变量读取,但它不起作用。
request.done(function(result) {
var results = $.parseJSON(result);
$.each(results, function(){
//create here your buttons
});
});