用java脚本修改php变量
Changing php variable by java script
我有一个按钮与onclick功能。它将变量$x传递到java脚本中。简而言之,我想做的是将$x="aaa"传递给javascript,并使该函数将该变量更改为让我们说$x="bbb"而不刷新网站。所以我将能够点击它所有的时间和js可以改变给定变量的值。我的意思是从按钮到javascript的内容变量会根据js函数代码而改变。这可能听起来很复杂,我真的很抱歉。什么好主意吗?(下面的代码是我编的,因为在这里解释我所有的代码太长了,现在很长)
<?php x = "aaa"; ?>
<button onClick="someFunction(<?php $x ?>)"></button>
<script>
function someFunction (x){
//what can i do here to make this "aaa" string in variable change
//into for example "bbb" on clicking it again?
}
</script>
不能从客户端脚本更改服务器端语言值
你所能做的就是将这个php值传递给javascript
这样的<button onClick="someFunction(<?php echo $x; ?>)"></button>
但是你可以这样做
试试这样
js
$.ajax({
method: "GET",
url: "some.php",
data: { x: "John" }
})
.done(function( msg ) {
<?php x=$_COOKIE["X"]; ?>
});
some.php
$_COOKIE["X"]=$_GET["x"];
编辑
How I can cut it off from button arguments
HTML <button id="myButton" onclick="someFunction(<?php $x ?>)"></button>
JS
$("#myButton").on("click",function(){
var onclick=$(this).attr("onclick");
var str="Hellow world";
var someFunction="someFunction('"+str+"')";
$(this).attr("onclick",someFunction);
});
考虑到上面的代码…不知道如果它是特定的字符串,你希望用javascript操作,但你可以使用php/服务器端语言生成的变量值基于任何计算你需要,然后使用javascript在客户端玩它。
<button onClick="someFunction()">Click me now to see what happens to x</button>
<script>
<?php echo "var x = 'a';"; ?>
function someFunction(){
x=parseInt( x.charCodeAt(0) );
x++;
x=String.fromCharCode( x );
alert(x)
}
</script>
经过研究和其他人的帮助,这是可行的:
//This gets the page's width on the first load
let sWidth = window.screen.width;
//This says : execute "myfunction" when "resize" event occurs
window.addEventListener("resize", myFunction);
//details of myFunction
function myFunction() {
//first get the page's width again
let sWidth = window.screen.width;
//show me the page's width within console which is within inspect
console.log(sWidth);
//now this is for differentiating Mobile and Desktop version
//which you can change the statement by need
if (sWidth <= 576){
console.log("Mobile version");
}else{
console.log("Desktop version");
}
}
相关文章:
- 将PHP变量传递给jQuery时遇到问题
- 通过javascript重定向html传递php变量
- 如何在php变量中嵌入JQuery代码
- 在javascript函数中使用php变量
- 将地理编码结果转换为php变量以发布到mysql数据库
- 如何在提交表单时将PHP变量传递到Javascript cookie中
- 从Javascript和Php变量创建Html模板文档
- 如何将php变量作为javascript中列表的元素
- 将PHP变量分配给PHP数组
- 如何在没有ajax的情况下将javascript动态数据发送到php变量
- 在Javascript甜甜圈图的PHP变量中引用PHP变量
- 使用javascript访问php变量
- 在Javascript文件中获取PHP变量
- 如何为Javascript数组()的使用准备PHP变量
- 如何获取PHP变量数据并将其存储在JavaScript变量中
- Javascript变量到PHP变量-如果语句不起作用,则发布
- 通过Javascript验证PHP变量
- 如何将Ajax数据值分配给PHP变量
- 如何在 bootsrap 完整日历中使用 PHP 变量
- 在javascript中调用PHP变量