gpt4 book ai didi

javascript - 通过 AJAX 传输变量

转载 作者:行者123 更新时间:2023-11-28 06:01:15 25 4
gpt4 key购买 nike

我现在正在处理日历。在日历旁边,我想要一个描述框,当用户单击日历中的日期时,应在其中显示有关事件的更多信息。

到目前为止我所拥有的: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();
}

那么我该怎么做才能在description.php中使用MySQL变量呢?

感谢您的帮助!

最佳答案

你可以使用这样的东西..

// 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>
///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>

关于javascript - 通过 AJAX 传输变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37254992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com