2016-05-16 153 views
0

我目前正在處理日曆。在日曆旁邊,我想要一個描述框,其中當用戶單擊日曆中的日期時,應該顯示有關該事件的更多信息。通過AJAX傳輸變量

我直到現在: HTML/PHP:

// Variables from MySQL request:  
$event 
$date 
$place 
$start 
$end 

<button type="button" onclick="event_description()">$day</button> 

<div id="details"></div> 

AJAX:

function event_description() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     document.getElementById("details").innerHTML = xhttp.responseText; 
    } 
    }; 
    xhttp.open("GET", "kalender/description.php", true); 
    xhttp.send(); 
} 

所以我能做些什麼,所以我可以使用MySQL變量在description.php?

感謝您的幫助!

+0

你被點名功能'kalendar_description'和綁定'event_description' –

+0

對不起,這是一個錯誤。在我的腳本中是正確的,它只是在其他語言,所以我犯了一個錯誤翻譯:) –

回答

0

您可以使用這樣的事情..

// Variables from MySQL request:   
$event 
$date 
$place 

///pass all the parameters in onclick function as arguments 

<button type="button" onclick="event_description('<?php echo $event ?>','<?php echo $date ?>','<?php echo $place ?>')">$day</button> 
<div id="details"></div> 

Script代碼

<script> 
///get all the params here in function 
function event_description(event_name,date,place) { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     document.getElementById("details").innerHTML = xhttp.responseText; 
    } 
    }; 

    /////create a string of all the parameters and pass this with requested url as query string and get all these params in target file using `$_GET[]` 

    var params = 'event_name='+event_name+'&date='+date+'&place='+place; 
    xhttp.open("GET", "kalender/description.php?"+params, true); 
    xhttp.send(); 
} 
</script> 
+0

你幫了我很多!謝謝! –

+0

@DanGreen不客氣,很高興幫助你...;)爲我的答案投票,如果它真的對你有幫助.. Thankx .. –