0

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!

i76970
  • 7
  • 6

1 Answers1

0

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

Community
  • 1
  • 1
pherris
  • 17,195
  • 8
  • 42
  • 58