douchan4674 2014-08-25 12:19
浏览 56

使用POST更改php变量

trying to change a php variable via js and echo back but its not working, been on it for days!!

I have a function handleServerResponse() called below, that alerts connection status, and getting the ok at status 200 so cant understand why my php variabe is not changing! driving me insane!

 function process() // send the info to the server 
  {
    if(xmlHttp) 
   {
     try
       {
       xmlHttp.open("GET","myFile.php?phpvar="+jsvar,true);
       xmlHttp.onreadystatechange = handleServerResponse;  //alerts connection status = all ok   
       xmlHttp.send(null);
     }
   catch(e)
     {
       alert(e.toString());
     }
   }
 }


     // server side script
     <?php
     echo '<?xml version="1.0" encoding = "UTF-8" standalone = "yes" ?>';
     $phpvar;
     $phpvar = $_GET['phpvar'];
     echo "new phpvar value  = ".$phpvar;
     ?>

Any help at all would be greatly appreciated. Cheers

  • 写回答

1条回答 默认 最新

  • duanfen2008 2014-08-25 12:41
    关注

    The following code achieves the aim you've stated. Not sure what your problem is, since you've neglected to post a complete example.

    phpVarTest.html

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    "use strict";
    function byId(e){return document.getElementById(e);}
    
    window.addEventListener('load', onDocLoaded, false);
    
    function myAjaxGet(url, callback)
    {
        var ajax = new XMLHttpRequest();
        ajax.onreadystatechange = function()
        {
            if (this.readyState==4 && this.status==200)
                callback(this);
        }
        ajax.onerror = function()
        {
            console.log("AJAX request failed to: " + url);
        }
        ajax.open("GET", url, true);
        ajax.send();
    }
    
    
    function onDocLoaded()
    {
        byId('testBtn').addEventListener('click', onTestBtnClicked, false);
    }
    
    function onTestBtnClicked(e)
    {
        var jsvar = 'some arbitrary text';
        var url = 'phpVarTest.php?phpvar='+jsvar;
        myAjaxGet(url, onAjaxDone);
    }
    
    function onAjaxDone(ajaxObj)
    {
        byId('outputDiv').innerText = ajaxObj.response;
    }
    
    </script>
    <style>
    </style>
    </head>
    <body>
            <button id='testBtn'>Initiate ajax request</button>
            <div id='outputDiv'></div>
    </body>
    </html>
    

    phpVarTest.php

    // server side script
    <?php
        echo '<?xml version="1.0" encoding = "UTF-8" standalone = "yes" ?>';
        $phpvar;
        $phpvar = $_GET['phpvar'];
        echo "new phpvar value  = ".$phpvar;
    ?>
    

    output

    // server side script 
    <?xml version="1.0" encoding = "UTF-8" standalone = "yes" ?>new phpvar value = some arbitrary text
    

    Note: you should place the comment in the php file inside of the php tags - otherwise, you'll see it in the output, as shown above.

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致