Cla*_*nga 0 html javascript php jquery
如果用户点击按钮,我想更改php变量($ start和$ end由var b1和var b2调用).现在我知道php是服务器端,但我该怎么做呢?我读了一些关于使用$ get的内容,但我不知道如何实现它:
<?php if ( is_user_logged_in() ) { ?>
<input type="submit" value="Start Chat" id="start_chat" style="position: absolute; top: 30px; left: 10px;" />
<?php
} ?>
<script>
jQuery('#start_chat').click(function(){
$(this).data('clicked', true);
});
var b1 = '<?php echo $start; ?>';
var b2 = '<?php echo $end; ?>';
if(jQuery('#start_chat').data('clicked')) {
// change var b1 and b2
} else {
// do not change anything
}
</script>
<div id="eu_la">
<?php
$start = strtotime('9:30');
$end = strtotime('12:30');
$timenow = date('U');
if((date('w') == 4) && ($timenow >= $start && $timenow <= $end)) { // day 2 = Tuesday
include('facut_mine.php');
}
else {
do_shortcode('[upzslider usingphp=true]');
} ?>
Run Code Online (Sandbox Code Playgroud)
在这里你当前的代码,添加:
// change var b1 and b2
$.ajax({
type: "POST",
url: "chat.php",
data: { b1: "123", b2: "456" }
});
Run Code Online (Sandbox Code Playgroud)
上chat.php:
$start = $_POST['b1'];
$end = $_POST['b2'];
Run Code Online (Sandbox Code Playgroud)
更新:
如果你需要在javascript中加载这里的数据:
在chat.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);
Run Code Online (Sandbox Code Playgroud)
在您的客户端代码:
// change var b1 and b2
$.ajax({
type: "POST",
url: "chat.php",
dataType: 'json',
data: { b1: "123", b2: "456" }
}).done(function(data) {
b1 = data.b1;
b2 = data.b2;
});
Run Code Online (Sandbox Code Playgroud)
我没有测试过,但希望它有效!