用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");
    }
}