我以为我在前面的问题中解决了这个问题,但它仍然不起作用。如果管理员单击按钮,我想更改 php 变量(由 var b1 和 var b2 调用的 $start 和 $end)。在此之后,每个访问者都应该可以使用新代码,因此它应该发出服务器请求。这是我更新的代码:
<?php
if(date('w') == 4){ // day 2 = Tuesday
$start = strtotime('9:30');
$end = strtotime('12:30');
$timenow = date('U');
if ($timenow >= $start && $timenow <= $end) {
echo'<div id="iar_eu">';
echo quick_chat(200, 'default', 1, 'left', 0, 0, 1, 1, 1);
echo'</div>';
} }
?>
</div>
<?php if ( is_user_logged_in() ) { ?>
<input type="submit" value="Start Chat" id="start_chat" style="position: absolute; top: 30px; left: 10px;" />
<?php
} ?>
<script type="text/javascript">
if(jQuery('#start_chat').data('clicked')) {
// change var b1 and b2
$.ajax({
type: "POST",
url: "/web/htdocs/www.fattorefamiglia.com/home/wp-content/themes/child_care_creative/chat.php",
dataType: 'json',
data: { b1: "1:00", b2: "23:00" }
}).done(function(data) {
b1 = data.b1;
b2 = data.b2;
});}
else {
$.ajax({
type: "POST",
url: "/web/htdocs/www.fattorefamiglia.com/home/wp-content/themes/child_care_creative/chat.php",
dataType: 'json',
data: { b1: "9:00", b2: "18:00" }
}).done(function(data) {
b1 = data.b1;
b2 = data.b2;
});}
jQuery('#start_chat').click(function(){
$(this).data('clicked', true);
var b1 = '<?php echo $start; ?>';
var b2 = '<?php echo $end; ?>';
});
</script>
聊天.php:
<?php
// variables
if (!empty($_POST)) {
$data['b1'] = $_POST['b1'];
$data['b2'] = $_POST['b2'];
}
// to not lose them
$_SESSION['chat'] = $data;
// to keep it compatible with your old code
$start = $data['b1'];
$end = $data['b2'];
// send the JSON formatted output
echo json_encode($data);
?>
当我单击按钮时没有任何反应。我究竟做错了什么?