Re: [请益] 把数据库字段资料用PHP自动传给javascript中

楼主: shadowjohn (转角遇到爱)   2015-11-11 09:10:27
※ 引述《mikejhou (Mike)》之铭言:
: 我主要是不懂下面这段如何改用PHP+MYSQL传值
: nodes = [
: {id:1, value: 3, label: 'Data mining' },
: {id:2, value: 3, label: 'Case-based' },
: ];
如果你要改用 ajax 也很简单
建一支 webservice
<?php
//pdo db connect
switch($_GET['mode'])
{
case 'webservice1':
echo 上一篇写的 json_encode($ra,true);
exit();
break;
}
然后你原本的程式如果有用jquery
加个function 如下
function myAjax(url,postdata)
{
var tmp = $.ajax({
url: url,
type: "POST",
data: postdata,
dataType: 'html',
async: false
}).responseText;
return tmp;
}
那你就可以
var nodes = myAjax("mywebservice.php?mode=webservice1","");
不过这种写法笔数少还ok,笔数多服务器回应慢,画面会稍卡
另外有另一种
function myAjax_async(url,postdata,func)
{
$.ajax({
url: url,
type: "POST",
data: postdata,
async: true,
dataType: 'html',
success: function(html){
func(html);
}
});
}
但这种或许不太适合你用
myAjax_async("mywebservice.php?mode....","",funciton(data){
var nodes = data;
//之后的code写在这
});
二种ajax简单的同步与异步参考一下 :)

Links booklink

Contact Us: admin [ a t ] ucptt.com