-3

I am trying to create something like system of posts, which could allow users to write as much as they want in each post. But I have a problem with transferring a long sting through AJAX. It always says that (the length of the link is too large) so I tried to divide my string into some segments and transfer them one by one and write them into a text file (I don't want to use database to store what users will write) by using PHP (fopen) function with (a) flag to continue writing like below

fopen($directory,"a")

also, I used (encodeURIComponent) with each part of the long string to remain (\n, spaces, ... etc.), but what I got as results was like this

e.g. what I wrote

bla bla bla bla bla bla bla 

what I got

bla bla bla SSbla bla bla b

how to solve this problem ??

my JS code where (d) is the string

function sendDetails(t){
    t = encodeURIComponent(t);
    var x = new XMLHttpRequest();
    x.open("POST","src/writeDetails.php?t="+t,true);
    x.send();
}
function writeDetails(d){
    var r = Math.floor(d.length / 1000);
    var m = d.length % 1000;
    for(i=0;i<r;i++){
        var p = d.slice(i*1000,i*1000+999);
        sendDetails(p);
    }
    if(i > 0){
        sendDetails(d.slice(i*1000,i*1000+m));
    }else{
        sendDetails(d.slice(0,m))
    }
}

my PHP code

$t = $_REQUEST['t'];
$f = fopen($fileLink,"a");
fwrite($f,$t);
fclose($f);
Anas
  • 75
  • 2
  • 9
  • 1
    Instead of vaguely describing what your code does, please provide a minimal and complete example which demonstrates the problem. Also, it sounds like the problem would be moot if you just use POST instead of GET. GET is not meant for, well, POSTing large amounts of data. – David Dec 19 '18 at 18:59
  • I already using POST and I got this results ... – Anas Dec 19 '18 at 19:01
  • Show your code, or we're just going to be guessing. – aynber Dec 19 '18 at 19:04
  • @Anas: Your use of `encodeURIComponent` and `"the length of the link is too large"` suggests otherwise. – David Dec 19 '18 at 19:04
  • @aynber I added my code, you can check it now – Anas Dec 19 '18 at 19:14
  • 1
    `?t="+t` is where you're having the problem. You're effectively sending it as GET, even though you're using POST as the method. See https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest – aynber Dec 19 '18 at 19:15
  • @aynber and how to solve this problem ?? – Anas Dec 19 '18 at 19:19
  • 2
    Possible duplicate of [Send POST data using XMLHttpRequest](https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest) – 943A Dec 19 '18 at 19:20

3 Answers3

0

As far as I understand, your problem lies in the URI being too long. Please see this question related to maximum URI length:

If you keep URLs under 2000 characters, they'll work in virtually any combination of client and server software.

To solve the problem, you will need to send the data in the request's body, instead of the query part of the URI:

function sendDetails(t){
    t = encodeURIComponent(t);
    var x = new XMLHttpRequest();
    x.open("POST", "src/writeDetails.php", true);

    var fd = new FormData();
    fd.append("t", t);
    x.send(fd);
}

Here is the API specification for XMLHttpRequest.send().

Note that I used a FormData object as the request's body, but you can choose other ways that are more suitable for you (such as JSON, maybe).

Victor
  • 13,914
  • 19
  • 78
  • 147
0

@Alexandros Bantzos I tried your code out and with one minor tweak got it working. I don't know why, but it works for me...

if (isset($_REQUEST['t'])) {
    $t = $_REQUEST['t']; // This is your variable with the t param you passed with AJAX
}

I hope this helps at least someone... (Unfortunately I am not allowed to comment yet - no idea why they'd do that)

marud
  • 1
-1

Your code is wrong. Check this code instead!

function sendDetails(t){
    t = encodeURIComponent(t);
    var x = new XMLHttpRequest();
    x.open("POST","src/writeDetails.php",true);
    x.send("t=" + t);
}

And your PHP should be like this:

if (isset($_POST['t']))
{
   $t = $_POST['t']; // This is your variable with the t param you passed with AJAX
}
Alex Bntz
  • 124
  • 9
  • Probably something on your code might be wrong. Post request doesn't pass the values from your url. So only with GET request you could get this error. – Alex Bntz Dec 19 '18 at 19:08
  • but this message appear to me every time even with POST request and I don't know why ?? I was using GET before and I changed it but the error remain :( – Anas Dec 19 '18 at 19:18