2

I am trying to use ajax to post the text from one text area to a php page to echo back into another textarea. When I do this it works for about 5 characters and then stops completely. I've tried with and without encoding doesn't seem to make a difference since I'm just trying simple plan text like "ddddddddddd".

in example.html

<!DOCTYPE html>
<html>
<body>
<style>
.center{
margin: 0 auto;
margin-left: auto;
margin-right: auto;
text-align:center;
}

</style>

<div class="center">

<textarea   onkeyup="changed()" onkeydown="changed()" onkeypress="changed()"  id="b1" rows="30" cols="81">
</textarea>

<textarea id="b2" rows="30" cols="81">
</textarea>

</div>

</body>
</html>
<script>
function changed(){

var text =document.getElementById("b1").value;

if(!text){
text ="";
}

     var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {

                document.getElementById('b2').value =xhr.responseText;

        }
    }

    xhr.open('POST', 'echo.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("text=" + text);



}

</script>

In echo.php

<?php 
 $mytext = $_POST["text"];
 echo $mytext;

?>

I've looked at several similar posts but none of them fix my issue. I'm not using a form. Do I have to? Textarea not POSTing with form

Community
  • 1
  • 1
Arthur Putnam
  • 1,026
  • 1
  • 9
  • 26

2 Answers2

2

I don't know what you're trying to do, but you can achieve the same output with less requirement, if you change your script to this:

<script>
function changed(){
    var text =document.getElementById("b1");
    if(!text){
       text ="";
    }
    else
    {
        text=text.value;
    }
    var text2 =document.getElementById("b2");
    if(!text2)
    {
       text2.value=text;
    }
}
</script>

Hope it helps.

EDIT

Found out the error now, seems like you're ajax request returns randomly.

  1. I typed 'asd' = it will fire an ajax
  2. Typed 'asdf' = it will fire another ajax

Now the error occur when the second ajax respond first, so the value of second text-area will be 'asdf' at first, but then later on the first ajax respond, it will overwrite the current value to 'asd'.

to solve this, change you're script to this:

    <script>
    var pending_request=false;
    function changed(){
    if(pending_request==false)
    {
        var text =document.getElementById("b1").value;
        if(!text){
            text ="";
        }
        var xhr;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        }
        else {
            throw new Error("Ajax is not supported by this browser");
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                document.getElementById('b2').value =xhr.responseText;
                pending_request=false;
            }
        }
        pending_request=true;
        xhr.open('POST', 'echo.php');
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send("text=" + text);
    }
    else
    {
      setTimeout(function(){ changed(); }, 3000);
    }
   }
   </script>

demo here

The idea is to wait for pending ajax request to respond before sending another :D

PS: Excuse me for my bad English.

VMcreator
  • 833
  • 8
  • 17
0

I made the changes suggested by VMcreator and saw major improvements but it still would "break". Preventing any other characters to be received. I tryed the same steps on his demo. It worked perfectly. I decided to try hosting my page and php script on another hosting provider and it WORKED!

I called my hosting provider (godaddy) and they said that since the server was receiving fast requests from the same ip it was rejected by their spam filter. Although my ip wasn't blocked many of my requests were thus, causing even worse race conditions.

Immediate solution - switch hosting provider

long term solution - (hasn't happened yet) change security settings to accept requests.

Arthur Putnam
  • 1,026
  • 1
  • 9
  • 26
  • Hi sir, I suggest you use websocket for this. Use only ajax with client->server , but if you need to send vice versa, then you need websocket. This may be the long term solution you're looking for. - I'm glad I can help :) – VMcreator Aug 03 '15 at 02:18