dsvcqvp139098 2015-08-11 21:17
浏览 159
已采纳

通过javascript更改php变量

I have a button with onclick function. It passes a variable $x into the java script. Shortly what i want to do is to pass the $x="aaa" to the javascript, and make the function to change this variable into lets say $x="bbb" without refreshing the website. So i would be able to click it all the time and the js could change the value of given variable. I mean the content variable going from button to javascript would be changed depending on js function code. This may sound complicated i am really sorry for that. Any ideas? (the code below i just made up because it would be to long to explain here all my code that is very long now)

<?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>
  • 写回答

2条回答 默认 最新

  • doushan6692 2015-08-11 21:21
    关注

    You can't change server side language value from client script

    All you can do is to pass that php value to javascript

    Like this

    <button onClick="someFunction(<?php echo $x; ?>)"></button>
    

    But you can do like it

    Try like this

    js

    $.ajax({
      method: "GET",
      url: "some.php",
      data: { x: "John" }
    })
      .done(function( msg ) {
        <?php x=$_COOKIE["X"]; ?>
    });
    

    some.php

    $_COOKIE["X"]=$_GET["x"];
    

    EDIT

    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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算