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);

}
potashin
  • 42,140
  • 11
  • 76
  • 100
dlofrodloh
  • 1,589
  • 2
  • 19
  • 34
  • 3
    You do not POST variables using url parameters, they are sent through the [send method](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#send()) – Patrick Evans Apr 11 '14 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. – dlofrodloh Apr 11 '14 at 20:08
  • see this link -> http://stackoverflow.com/questions/2891574/how-do-i-resolve-a-http-414-request-uri-too-long-error – Saman Gholami Apr 11 '14 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. – dlofrodloh Apr 11 '14 at 20:12

2 Answers2

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);

}
potashin
  • 42,140
  • 11
  • 76
  • 100
  • @tadman: I was just editing that thing.Fast!! – Insane Coder Apr 11 '14 at 20:10
  • 2
    Couldn't let that mistake get in the way of your great answer. Heh. – tadman Apr 11 '14 at 20:13
  • 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. – dlofrodloh Apr 11 '14 at 20:22
  • Sorry, it is a block from my code, I've updated it – potashin Apr 11 '14 at 20:23
  • Ah thanks, I'll try it out now – dlofrodloh Apr 11 '14 at 20:24
  • No response so far, do I have to change the php part? – dlofrodloh Apr 11 '14 at 20:31
  • The alert(str) gives me something like: q=Paia%5E!keyur.paa%40yahoo.co.uk%5E!%3C ... which is the csv data. I changed the php to var_dump($post), but I can't see the result because it hasn't returned? – dlofrodloh Apr 11 '14 at 20:40
  • Yep, that's what I put. I edited my question to show the function that I'm using for the xmlhttp.. not sure if something is wrong there. – dlofrodloh Apr 11 '14 at 20:44
  • Are you checking var_dump() output in page resources window ? – potashin Apr 11 '14 at 20:46
  • Sorry I'm not sure how I do that? I only thought I could see the result when it gets returned to the javascript – dlofrodloh Apr 11 '14 at 20:48
  • open safari or google chrome,then open Development->Show page resources in Safari or Tools->Development Tools->Network in Chrome. There you can see pages that you are requesting via ajax and it's output. – potashin Apr 11 '14 at 20:51
  • Ok, I got that open. I'm on the javascript page and running the function to call the xmlhttp, but I don't see any result of the vardump, on the console it says :" Uncaught ReferenceError: xmlhttp is not defined "... not sure if that has anything to do with it. – dlofrodloh Apr 11 '14 at 20:56
  • So can you see output by clicking on "testpost.php" in Resources(it is common both for Chrome and Safari) panel? – potashin Apr 11 '14 at 20:59
  • No, it has the html files and the js files there but no php – dlofrodloh Apr 11 '14 at 21:01
  • It means that you are not using (or using it incorrectly) your js function SendPHP – potashin Apr 11 '14 at 21:03
  • Hmm, it was working fine when I was using GET. I'll change it back to the old GET code and see what's wrong. – dlofrodloh Apr 11 '14 at 21:05
  • Then at last try your old php script with your new js function.It should work out).It kinda getting me crazy , so I really want to help at least to figure out what the problem was. – potashin Apr 11 '14 at 21:09
  • Ok, I used the old javascript code and I can see the vardump in chrome resources with that. So I'll try and figure out what went wrong with the new js code and why it doesnt show the vardump – dlofrodloh Apr 11 '14 at 21:13
  • try to set `content-type` to `"text/csv; charset=utf-8"`.Try to use with and without encodeURIComponent() and post your results. – potashin Apr 11 '14 at 21:19
  • I changed it (shown at the bottom of my edited question), still no joy. – dlofrodloh Apr 11 '14 at 21:23
  • I edited my question to show the function I was using that was working before when I used GET. Could it be because there is no " xmlhttp = new XMLHttpRequest();" in the new one? – dlofrodloh Apr 11 '14 at 21:29
  • Aha! I added that and now it returns "NULL" – dlofrodloh Apr 11 '14 at 21:32
  • Change `content-type` to the previous value. Sorry, I've used this function to auto detect xmlhttp object depending on browser type. – potashin Apr 11 '14 at 21:32
  • Ok, still returning Null ... the str string is fine before being sent "q=This%20is%20a%20test" – dlofrodloh Apr 11 '14 at 21:36
  • So I just put JSON.stringify(str), and how do I assign to key 'q'? – dlofrodloh Apr 11 '14 at 22:01
  • I really can't get what the problem is, cause when i run it on my machine with string `"This is test"` I get correct var_dump in my php script – potashin Apr 11 '14 at 22:01
  • Hmm, strange. I might have to give up on this and just use a function to parse the csv in chunks using GET. Thanks a lot for your help though, sorry you wasted so much of your time! – dlofrodloh Apr 11 '14 at 22:03
  • Just tried that and it still returns null – dlofrodloh Apr 11 '14 at 22:19
  • I see ok, hmmm. I wonder why the data is invalid on my end but not yours? I also tried the http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest solution, just copying and pasting the function but that returns null as well. – dlofrodloh Apr 11 '14 at 22:23
  • I am writing another answer and after that you just copy it and post the output,OK? – potashin Apr 11 '14 at 22:24
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"
}
potashin
  • 42,140
  • 11
  • 76
  • 100