上一篇是用Javascript存成JSON到SQL
現在是從SQL讀取資料給Javascirpt作evaluate
PHP:(可以省略SQL部份~~~直接看$response)
$sql = "SELECT * FROM `equipments` WHERE `id` = '".$id."'";
$r = db_query($sql);
$x = db_fetch($r);
$response[] = array('project_id' => $x['1'], 'operate_unit' => $x['2'], 'equipment_number' => $x['3'], 'process_description' => $x['4'], 'unit' => $x['5'], 'toxicity' => $x['6'], 'part_a' => stripslashes($x['7']), 'part_b' => stripslashes($x['8']), 'part_c1' => stripslashes($x['9']), 'part_c2' => stripslashes($x['10']), 'part_d' => $x['11']);
if($response)
{
print json_encode($response);
}
這樣用AJAX去取這個PHP檔~~~~他就可以取到JSON出來~~~
然後Javascript部份要作eval
Javascript:
function loadEquipment()
{
var url = "ajax/get_equipment.php";
var par ="";
new Ajax.Request(url,{method:'post',parameters: par, onComplete: loadEquipmentResponse});
}
function loadEquipmentResponse(r)
{
var x = eval(r.responseText);
var a = eval('[' + x[0].part_a + ']');
//span
for ( var key in a[0].spans )
{
$(key).innerHTML = a[0].spans[key];
}
//input
for ( var key in a[0].inputs )
{
$(key).value = a[0].inputs[key];
}
//radio
for ( var key in a[0].radios )
{
$(key).checked = a[0].radios[key];
}
then.........東西出來了.........哭哭
只是我覺得很奇怪的是.......var a = eval('[' + x[0].part_a + ']');
要多加中括號把他包起來他才可以eval~~~~在PHP中也是~~~$response[];
總之這個程式~~~~~~~就是把之前網頁的一些數據都存起來到SQL~~~
然後在透過Javascript eval的方式~~~再把一整串JSON變成一個一個變數放到各各id裡
沒有留言:
發佈留言