2012-03-17 80 views
0

我有一個PHP函數來獲取從MySQL數據庫的一些數據:PHP變量javascript變量的jqGrid集成

function get_persons() { 
    $result=dbquery("SELECT id,name,surname FROM ".DB_PERSONS." ORDER BY id ASC"); 
    while ($person = dbarray($result)){ 
     $id=$person['id']; 
     echo "<option value='$id'>$person['name'] - $person['surname']</option>"; 
    } 
} 

,我已經一個JavaScript代碼:

$edit_buttonoptions = array("#pager", 
    array("title"=>"Takım Seç","buttonicon"=>"ui-icon-mail-open","caption"=>"Takım Seç", "onClickButton"=>"js: 
var jspersonvar; 
jspersonvar = "<?php get_persons();?>" 
function(){ 
    var selr = jQuery('#grid').jqGrid('getGridParam','selrow'); 
    var rowData = jQuery('#grid').jqGrid('getRowData', selr); 
    var kelr = jQuery('#grid').jqGrid('getCell', selr, 'bilinen_adi'); 
    var fotograf = jQuery('#grid').jqGrid('getCell', selr, 'logo'); 
    if(selr) 
     miktar=miktar+1, 
     pp='takimlar', 
     nm=pp + miktar, 
     jQuery('#takimlar').append('<div id=' + nm + '>'), 
     jQuery('#'+ nm +'').append('<div style=text-align:center;>'+fotograf+'<input type=hidden name=takim'+ miktar +' value=' + selr + ' />'), 
     jQuery('#'+ nm +'').append('<select name=sezon'+ miktar +' id=sezon'+ miktar +'>'+jspersonvar+'</select>'), 
     jQuery('#'+ nm +'').append('' + kelr + ''), 
     jQuery('#'+ nm +'').append('</div>'), 
     jQuery('#'+ nm +'').append('</div><br />'), 

     alert('' + kelr + ' Takımı Seçildi') 
    else 
    alert('Lütfen Bir Takım Seçiniz!') 
    return false; 
    }"), 
); 
$grid->callGridMethod("#grid", "navButtonAdd", $edit_buttonoptions); 

它不工作,我已經嘗試了幾件事,但沒有運氣......

+0

預計什麼?什麼不工作? – 2012-03-17 16:07:45

+1

''''''''''get_persons();?>' – 2012-03-17 16:07:51

+0

更新問題 – 2012-03-17 16:24:58

回答

1

您需要將變量放在引號中,因爲當服務器呈現頁面時,它將如下所示:

jspersonvar = <option value='$id'>...... 

當你有引號時,jspersonvar將被視爲一個字符串,你可以做你會用它。

jspersonvar = "<?php get_persons();?>" 

一定要小心,在你的數據庫中得到顯示會搞砸了你的JavaScript任何"

+0

問題更新 – 2012-03-17 16:25:04

+0

非常感謝您的信息我已修復它:) – 2012-03-17 17:04:28