douzhi2017 2016-01-19 21:25
浏览 53
已采纳

php ajax javascript / ecmascript html写入文本文件

I'm not overly experienced with the aforementioned technologies, but need to resolve the issues i'm experiencing with the POST function.

<!DOCTYPE html>
<html>

<head>
    <title>ajax</title>
    <meta charset='UTF-8'>
    <script src='lib/ajaxget.js'></script>
    <script src='lib/ajaxput.js'></script>
</head>

<body>
    <h1>blah</h1>
    <div>AJAX uploads go here.</div>
    <div id="grabphpdiv"> AJAX uploads from PHP go here.</div>
    <br>
    <textarea id="comment" rows="5" cols="40"></textarea>
    <br>
    <button id="put">put</button>
    <br>
    <br>
    <button id="get">get</button>
    <script src='dyn.js'></script>
</body>

</html>

The JS 'GET' function is working, so here's the POST that doesn't work (no errors in the console) the text file doesn't update though...

function AjaxPut(URL, callback)
{   var ajaxObj = new XMLHttpRequest();
    ajaxObj.open("POST", URL, true);
    ajaxObj.onreadystatechange = function()
        {   if (ajaxObj.status == 200)
                if (ajaxObj.readyState == 4)
                    callback(ajaxObj.responseText);
        };
    ajaxObj.send("somestuff");
};

And the PHP for the post (though titled PUT here)

<?php
$jothandle = fopen("jots.txt","w");
fwrite($jothandle,$_REQUEST['line']);

Lastly, here's the JavaScript that's entitled 'dyn.js' at the bottom of the HTML. (though for brevity, i've only pasted in the POST section.

var y = document.getElementById("put");
y.addEventListener("click", runapi1);
y.addEventListener("click", grabphp1);

function runapi1()
    {   AjaxPut('api/put.php', grabphp1);}
function grabphp1(response)
    {   document.getElementById('grabphpdiv').innerHTML = response; }

Any help or pointers would be very much appreciated! thanks!

  • 写回答

1条回答 默认 最新

  • dongren1011 2016-01-19 21:34
    关注

    It looks like you aren't sending a parameter called line from your JS, but you are expecting one in your PHP code. Try sending this instead: line=somestuff e.g.

    ajaxObj.send("line=somestuff");
    

    Send POST data using XMLHttpRequest

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果