0

I want to delete file contents on the click of a button. I make an ajax request which sends 2 variables, the filename and the name of person who deleted the contents. The PHP function works completely fine, with no errors, but it cannot read the ajax request.

My HTML:

<button onclick="del()">Delete Chat</button>

My JavaScript:

<script>
 function del(){
   var del = new XMLHttpRequest();
   var url = "dc.php?f=" + "<?php echo $mf[0]."-".$mf[1].".txt" ?>"+"&n="+ "<?php echo $me ?>";
   del.send();
 }
</script>

My dc.php file:

<?php 
  $f = $_REQUEST['f'];
  $n = $_REQUEST['n'];
  $del=fopen($f,'w');
    fwrite($del, "Chat Deleted by ".$n."\n<br>");
  fclose($del);
?>
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Mrigank Pawagi
  • 892
  • 1
  • 8
  • 26
  • 1
    What can you see in your Developer tools in `Network` tab? You can track your request there. – Manwal Jun 29 '17 at 12:35
  • @Manwal I get that `f` is `undefined index` – Mrigank Pawagi Jun 29 '17 at 12:37
  • What is the actual resulting URL of the request being made? Note also that you're attempting to make *two* requests. And they define different query string values. Which one is actually being made? – David Jun 29 '17 at 12:38
  • Shouldn't be `$_GET` instead of `$_REQUEST` ? – Hamza Abdaoui Jun 29 '17 at 12:38
  • 1
    @HamzaAbdaoui `$_REQUEST` can be used for both `$_GET` & `$_POST`. So its not the issue – B. Desai Jun 29 '17 at 12:40
  • Calling `del.open` twice does not mean it will make two requests ... you are only _overwriting_ the address that the request will be made to here. – CBroe Jun 29 '17 at 12:41
  • It's a `.txt.` file. If I run the `dc.php` alone, with the SAME file, I get the output, but not with the ajax request – Mrigank Pawagi Jun 29 '17 at 12:41
  • @CBroe maybe You are right, I combined to single request, but still no help – Mrigank Pawagi Jun 29 '17 at 12:41
  • @MrigankPawagi: If you've corrected that problem in the code, update the question with the new code. Also, you still haven't indicated what the actual resulting URL of the request is. – David Jun 29 '17 at 12:43
  • @David the resulting url is, for instance, `JAMES-ADAM.txt` and stores the messaging of the 2 people. – Mrigank Pawagi Jun 29 '17 at 13:00
  • @MrigankPawagi: It doesn't make sense that the requesting URL is `JAMES-ADAM.txt`, since there's no code here modifying the `dc.php?f=` component of the URL. If your code is indeed requesting only a text file then the PHP code isn't being invoked at all. But clearly that's not going to happen with the code shown. So either you're showing us the wrong client-side code, or you're just not telling us what the actual URL of the AJAX request is. Either way, that arbitrarily makes it difficult to help in any way. – David Jun 29 '17 at 13:03
  • @David See, my PHP file has 2 variables, `$me` and `$other`. I create an array out of them and sort it. This is because I am working on a chatting website. Both the people should refer to the same `text` file, that's why I sort the array and then generate the URL. If MY name is JAMES and the other person is ADAM, the path will be JAMES-ADAM.txt And that is what my request is. Everything in the question (AFTER UPDATE) is correct. – Mrigank Pawagi Jun 29 '17 at 13:08
  • 1
    @MrigankPawagi: That's nice. But my point is that if, as the original problem description in the question states, a value on an HTTP request isn't being seen by the server-side code, then the first thing you should do is *actually look at the request*. You keep explaining to us what the request is *supposed to do*, but we're trying to determine what it's *actually doing*. You aren't showing the actual resulting client-side code which generates the AJAX request, or even the actual resulting URL being requested by that code. Stop assuming that your code must be correct and *actually look at it*. – David Jun 29 '17 at 13:12
  • @David Man, 1st of all, you should be more polite. Secondly, If I was thinking my code was correct, I wouldn't have joined StackOverflow at all. And that's because I am just a middle school student with a passion of coding, and I don't call myself an EXPERT. Now, Coming to the point, when I run my `dc.php` file INDEPENDENTLY, and define the variables myself, like `$f="JAMES-ADAM.txt"; $n="JAMES";` then it runs COMPLETELY fine. But when I rely on the `$_REQUEST` superglobal, it doesn't do Anything at all, and That is why I want your help. There is NO code that I am not showing you guys. – Mrigank Pawagi Jun 29 '17 at 13:18
  • @MrigankPawagi: The code you're **not showing** is the client-side code which is generated by your PHP. The actual resulting `var url = "...";` line that's in your browser's page source. The actual values being used to build the URL. There could be an errant character breaking the URL, there could be no values at all being emitted (which means you have an incorrect assumption about whatever `$mf[0]` and other values are, assuming they must be correct when they might not be), etc. You're making many assumptions of correctness without validating them, and you're not providing debugging info. – David Jun 29 '17 at 13:23
  • Open your browser’s developer tools, network panel, and check what request is actually send when you click that button. – CBroe Jun 29 '17 at 13:29
  • @David Actually these variables are completely correct, and that's for sure, because they DO NOT depend on any other variable, and are defined using CONSTANTS. Also, I use these variables to WRITE to the same file, whose URL is mf[0]+mf[1]+".txt" And this provides COMPLETELY correct output. Apart, I READ with the SAME URL, and that too with AJAX, and that also happens without any problem. Therefore, It is sure that the problem is with the AJAX code, for SURE. – Mrigank Pawagi Jun 29 '17 at 16:43
  • @MrigankPawagi: Even if we are to take all of that reasoning as a given, at the very end there you insist that the problem *must* be with the AJAX code. You have been asked several times to show us that code. (Not the PHP which generates it, but the actual code that's executing in the browser.) Whenever you feel like showing us the code that you're certain is failing, do let us know. – David Jun 29 '17 at 16:49
  • But the actual code will be just simple HTML, so what will be the use? @David – Mrigank Pawagi Jun 29 '17 at 16:50
  • @MrigankPawagi: No, it would be JavaScript. HTML and JavaScript are two very different things. It's the JavaScript code which is actually building and invoking the AJAX request. You've in fact already shown *most* of that code. But haven't included the dynamic output that builds part of it. Honestly, I really don't know how else to explain this. If code is failing, look at the code. You keep insisting that it *must* be correct and there's no use in showing it to us, but also asking why it isn't working. That's fundamentally unanswerable. – David Jun 29 '17 at 16:53
  • @David I admit my Ajax haS bugS, and that's why I seek help. And yes, other JS in client side code is in NO way associated to this function. It is about ANOTHER Ajax request that READS the SAME file whose contents I want to delete with THIS Ajax, and some other Jquery functions for animations. – Mrigank Pawagi Jun 29 '17 at 16:55
  • @MrigankPawagi: Maybe there's another incorrect assumption here... When you view the page source in your browser, are you *actually seeing* `"dc.php?f=" + ""+"&n="+ ""`? Because if so, then your PHP isn't executing at all (at least not this part) and what you have is an entirely invalid URL. And this question becomes a duplicate of: https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page – David Jun 29 '17 at 16:59
  • @David Actually you right, I didn't understand what you meant. Now when I see the client side code, It displays My file name, which is JAMES-ADAM.txt and that is correct, I guess – Mrigank Pawagi Jun 29 '17 at 17:02

1 Answers1

1

Try this: You dont have to call different request. Combine them in one:

<script>
     function del(){
       var del = new XMLHttpRequest();
       var url = "dc.php?f=" + "<?php echo $mf[0]."-".$mf[1].".txt" ?>"+"&n="+ "<?php echo $me ?>";
       url = encodeURIComponent(url);
       del.open("GET", url , true);
       del.send();
     }
</script>
B. Desai
  • 16,414
  • 5
  • 26
  • 47