2011-10-31 15 views
3

嗯...我試圖在jQuery中使用php變量,在我的edit.php中:我想用jquery來顯示php變量,使用「rel」屬性,但它給了我變量的「名稱」,而不是它的內容。如何顯示從jQuery的PHP變量「內容」? (它顯示了我的名字本身,而不是它的內容)

我使用PHP來檢索XML文件中的文本:(編輯:一按就提交不更新的內容,但2次點擊辦)

/* READ */ 
$dom = new DomDocument(); 
$dom->load('edition.xml'); 

$_home = $dom->getElementsByTagName('home')->item(0); 
$home = $_home->firstChild->nodeValue; 

$_rhizo = $dom->getElementsByTagName('rhizo')->item(0); 
$rhizo = $_rhizo->firstChild->nodeValue; 

$_disco = $dom->getElementsByTagName('disco')->item(0); 
$disco = $_disco->firstChild->nodeValue; 

/* array */ 
$rels = array('home' => $home, 
'rhizo' =>$rhizo, 
'disco' =>$disco); 

echo '<script>var rels = ' . json_encode($rels) . '</script>'; 


/* WRITE */ 
if (isset($_POST['cache'])){ 

    if (isset($_POST['home'])){ 
    $home = stripslashes($_POST['home']); 
    $_home->firstChild->nodeValue = $home; 
    } 

    if (isset($_POST['rhizo'])){ 
    $rhizo = stripslashes($_POST['rhizo']); 
    $_rhizo->firstChild->nodeValue = $rhizo; 
    } 

    if (isset($_POST['disco'])){ 
    $disco = stripslashes($_POST['disco']); 
    $_disco->firstChild->nodeValue = $disco; 
    } 

    $dom->save('edition.xml'); 

} 
?> 

(文本只是所示一個textarea,在這裏我只是把PHP變量) 然後使用jQuery顯示從PHP變量的內容:(編輯與數組對象:)

$('#left a').bind('click', function(){ 

    var text_from_link = $(this).text(); 

    var rel = $(this).attr('rel'); 
    $('#textarea1').attr('name', rel); 

    $('#textarea1').text(rels[''+rel+'']);//output is "$home" for example, which will retrieve the "home" part from the xml 

    return false; 
}) 

如果我雙擊提交這份工作,如果我只是點擊一次,文本不會更新...我有點困惑與th在

感謝您的幫助

+0

相關:http://stackoverflow.com/q/2145727/212218 – 2011-10-31 23:02:51

回答

1

您需要做一個ajax調用來做到這一點。

或者你可以寫在你的HTML JavaScript對象:

$rels = array('rel23' => 'Hello world'); 
echo '<script>var rels = ' . json_encode($rels) . '</script>'; 

然後使用JavaScript訪問它們:

alert(rels.rel23); 
+0

感謝El Boletaire Underave!所以如果我有我的PHP頁面中的數組,我怎麼能在我的jQuery文件中使用它?我應該像這樣使用它:'alert(rels [0]);'?再次感謝 – Paul

+0

嗯,在我的例子中應該是'alert(rels.rel23)'或'alert(rels ['rel23'])' – elboletaire

+0

謝謝!對不起,我覺得我有點累了!好吧,我有一個小問題:當我提交更新文本時,如果點擊「一次」,它會給我舊文本。但是,如果我雙擊提交,它會更新文本...我有點困惑,我編輯了上面的代碼,你會知道爲什麼以及如何解決這個問題嗎?我嘗試模擬'window.location.href(「edition.php」);'在點擊後的submit方法中,但它不會改變任何東西 – Paul

1

提到的一樣,你需要使用Ajax這個.. jQuery提供AJAX看起來您已經在使用它soooo ....

if(isset($_POST["getHome"])) { 
$dom = new DomDocument(); 
$dom->load('edition.xml'); 

$_home = $dom->getElementsByTagName('home')->item(0); 
$home = $_home->firstChild->nodeValue; 
echo $home; // anything returned by this page will become 'data' 
} 

$(function(){ 

    $('a').bind('click', function(){ 
     $.post("mypage.php",{getHome : 1}, function(data) { 
      alert(data); 
     }); 
    }) 

}); 

確定假設你有一個數組在PHP

if(isset($_POST["varName"])) { // will check if the POST variable with the name 'varName' is set 
    $arr = array(1,2,3); // your array 
    echo json_encode($arr); // encode your output as a json string 
} 

然後,在JavaScript

$(function(){ 

    $('a').bind('click', function(){ 
     $.post("mypage.php",{varName: 1}, function(data) { // the value of 1 is trivial. and is likely not needed, you can post to a page without passing any values. In which case the use if isset() in php is not required. 
      var json_obj = JSON.parse(data); 
      console.log(json_obj); // assuming you have a console at your disposal. 
     }); 
    }) 

}); 
+0

感謝rlemon,我有像頁面頂部的15個PHP變量,所以...我不能真正「回聲」他們,你能向我解釋一下你的代碼嗎?謝謝 – Paul

+0

你想把它們還給客戶嗎? – rlemon

+0

嗨rlemon,謝謝你好,我明白了一點。事情是我用我自己的edit.php頁面顯示文本(從XML),並寫入文本。所以我有一個隱藏的輸入,檢查表單是否被髮送,在你的例子中,似乎我需要用XML文件的結果創建另一個php頁面嗎?我還編輯了我的第一篇文章,以便更多地解釋我正在使用的內容。它與php代碼塊中創建的數組效果相當好,但只有當我點擊兩次提交時纔會更新文本,而不是如果我只點擊「一次」...並且我沒有真正的解釋 – Paul

相關問題