1

I'm sending a string via xmlhttp in javascript using the following code:

    function SendPHP(str, callback) {
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {


                         callback(xmlhttp.responseText); //invoke the callback
        }
    }
        xmlhttp.open("GET", "testpost.php?q=" + encodeURIComponent(str), true);

    xmlhttp.send();

}

and some test php:

$q = $_GET['q'];
echo $q;

That worked fine until I started sending a larger string in which case I get the "HTTP/1.1 414 Request-URI Too Long" error.

After a bit of research I found out I need to use "POST" instead. So I changed it to:

xmlhttp.open("POST", "sendmail.php?q=" + str, true);

And:

$q = $_POST['q'];
echo $q;

But this does not echo anything when using POST. How can I fix it so it works like when I was using GET but so it can handle a large string of data?

edit I'm now trying it with:

function testNewPHP(str){


    xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

  alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState == 4){
     if(xmlhttp.status == 200){
                            alert (xmlhttp.responseText);
     }
    }
  };
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}
4
  • 3
    You do not POST variables using url parameters, they are sent through the send method Commented Apr 11, 2014 at 20:06
  • I'm basically using it to send a CSV file turned into a string to my php script which does some processing on it. So it's just a long string that needs to be processed by the php script. Commented Apr 11, 2014 at 20:08
  • see this link -> stackoverflow.com/questions/2891574/… Commented Apr 11, 2014 at 20:09
  • @Saman I read that before to figure out I need to use POST instead of GET, but it doesn't say the difference on how to use it vs GET. Commented Apr 11, 2014 at 20:12

2 Answers 2

5

You should not provide your href with URL parameters, instead of this you should send() them. For addition, you should always encode your parameters with encodeURIComponent() (At least when your request is using the Content-type "application/x-www-form-urlencoded").

your javascript function :

function testNewPHP(){
var str = "This is test";

xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState == 4){
        if(xmlhttp.status == 200){
            alert (xmlhttp.responseText);
        }
    }
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}
Sign up to request clarification or add additional context in comments.

29 Comments

@tadman: I was just editing that thing.Fast!!
Couldn't let that mistake get in the way of your great answer. Heh.
The str string in my code is the contents of a CSV file. In this answer is it overwritten with something? I'm not sure I understand the json part.
Sorry, it is a block from my code, I've updated it
No response so far, do I have to change the php part?
|
1

javascript :

 function testNewPHP(){
var str = "This is test";

xmlhttp = new XMLHttpRequest();

str = "q=" + encodeURIComponent(str);

alert (str);

xmlhttp.open("POST","testpost.php", true);
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState == 4){
        if(xmlhttp.status == 200){
            alert (xmlhttp.responseText);
        }
    }
};
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(str);

}

testpost.php in your home directory :

<?php
 var_dump($_POST);

OUTPUT :

array(1) {
["q"]=>
string(12) "This is test"
}

4 Comments

What was the problem in the end?
I have not fixed out what the problem was, Could you?)
Cool, done! Haha, no idea what the problem was in the end.
Glad to help you,anyway)))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.