javascript 在单击事件上更改 php 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12509268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change php variable on click event
提问by Claudiu Creanga
I want to change php variable ($start and $end called by var b1 and var b2) if the user clicks on button. Now I know that php is server side, but how can I do it? I read something about using $get but I don't know how to implement it:
如果用户单击按钮,我想更改 php 变量(由 var b1 和 var b2 调用的 $start 和 $end)。现在我知道 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]');
} ?>
回答by Mahdi
on your current code here, add:
在您当前的代码上,添加:
// change var b1 and b2
$.ajax({
type: "POST",
url: "chat.php",
data: { b1: "123", b2: "456" }
});
on chat.php
:
上chat.php
:
$start = $_POST['b1'];
$end = $_POST['b2'];
Update:
更新:
if you need to load back the data here in javascript:
如果您需要在 javascript 中加载这里的数据:
on chat.php
make these changes:
在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);
on your client-side code:
在您的客户端代码上:
// 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;
});
I didn't test that, but hope it works!
我没有测试过,但希望它有效!
回答by Vlad
First of all, the input tag must be enclosed in a form tag. As such,
首先,输入标签必须包含在表单标签中。像这样,
<?php if ( is_user_logged_in() ) { ?>
<form method="GET">
<input type="submit" value="Start Chat" id="start_chat" style="position: absolute; top: 30px; left: 10px;" />
<input type="hidden" name="start" value="[WHATEVER YOU WANT GOES HERE]" />
<input type="hidden" name="end" value="[WHATEVER YOU WANT GOES HERE]" />
</form>
<?php
} ?>
Then, in the PHP code
然后,在PHP代码中
if (isset($_GET['start']))
$start = $_GET['start'];
if (isset($_GET['end']))
$start = $_GET['end'];
That will set the $start and $end variables to whatever values you submitted through the form. However, your JavaScript code won't work as intended
这会将 $start 和 $end 变量设置为您通过表单提交的任何值。但是,您的 JavaScript 代码不会按预期工作
jQuery('#start_chat').click(function(){
$(this).data('clicked', true);
});
if(jQuery('#start_chat').data('clicked')) {
// change var b1 and b2
} else {
// do not change anything
}
The first piece of code adds an event listener to the #start_chat element. The problem, however, is that the latter piece of code is read and executed(by the interpreter) right after, when data('clicked') is not set. So it will never enter the ifbranch.
第一段代码向#start_chat 元素添加了一个事件侦听器。然而,问题是当 data('clicked') 未设置时,后一段代码会立即被读取和执行(由解释器)。所以它永远不会进入if分支。
回答by Martin Müller
As you say correctly, PHP is server side. This is why you would have to call an Ajax-Send to the server, pushing the new values to the php script. Then you'd have to load the answer that is delivered from PHP via Javascript (Ajax onready) and replace the page (parts) with the newly loaded.
正如您所说的那样,PHP 是服务器端。这就是为什么您必须向服务器调用 Ajax-Send,将新值推送到 php 脚本的原因。然后,您必须加载通过 Javascript (Ajax onready) 从 PHP 提供的答案,并用新加载的页面(部分)替换。
Seems awfully complicated for me just to change start and end times. Just do it directly in JS ;)
仅仅改变开始和结束时间对我来说似乎非常复杂。直接在 JS 中执行即可;)