通过AJAX传输变量

Transmitting variables via AJAX

本文关键字:变量 传输 AJAX 通过      更新时间:2023-09-26

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

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