1

I have:

Index.php

      <?php
   if(isset($_POST["id"])){
     echo($_POST["id"]);
   }    
       ?>

JavaScript:

 let xhttp = new XMLHttpRequest();
        xhttp.open("POST", "index.php", true);
        xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
        xhttp.onreadystatechange = function (){
            if(xhttp.readyState == 4 && xhttp.status == 200)
            {
                let result = xhttp.responseText;
                console.log(result)
            }
        }
        xhttp.send("id=195");

But the console prints the whole HTML code for the php page. I thought it should print just the ID.

ADyson
  • 57,178
  • 14
  • 51
  • 63
vytasK
  • 47
  • 6
  • 3
    It should be in the console when you execute the JavaScript. It won't be on the web page itself, since that's not being run through AJAX. – Barmar Feb 16 '21 at 20:28
  • The only problem for you seems to be reading the response from your Ajax request. console.log prints to the browser's console, not the page itself. Do you understand how to open your browser's Console? Press F12 in most browsers. Alternatively, change it to `alert(result)` to see it in a pop-up. If you want to put the response back into your page, then use DOM functions to add it to the relevant place (we can't be more specific since we don't know the location where you want to put it? – ADyson Feb 16 '21 at 20:41
  • I want on a button click to send a post/get request to the php page with a list of IDs for SQL to delete. So the post/get has to be processed by php? I know how to open the console. – vytasK Feb 16 '21 at 20:47
  • use curl request for example https://stackoverflow.com/questions/30261830/how-to-emulate-ajax-request-using-php-curl – Vasiliy Letuyev Feb 16 '21 at 20:52
  • 1
    @vytasK your code should already be doing that. It should send the ID, and then echo it. Are you saying that the console prints nothing when you run this code? Or there is some other error? Have you checked the browser's Network tool to be sure that the Ajax request definitely starts, and definitely returns? Based on what you've shown, this code should work. It's now a bit unclear what your specific problem is. – ADyson Feb 16 '21 at 20:54
  • the console prints the whole html code for the php page. I thought it should print just the ID – vytasK Feb 16 '21 at 20:56
  • @vasiliyletuyev you can't use cURL for an AJAX request. I don't understand your suggestion. – ADyson Feb 16 '21 at 20:56
  • @vytask do you have other code in index.php, then? If you only want it to print the ID, add an `exit();` command just after it echoes the ID, so that the rest of the script is not processed in that situation. (That's assuming that the PHP code comes before the HTML in the file). – ADyson Feb 16 '21 at 20:58
  • Does this answer your question? [jQuery Ajax POST example with PHP](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Lucas Henrique Feb 16 '21 at 21:00
  • P.s. Most developers tend to separate the PHP which responds to Ajax requests from the PHP which deals with loading HTML pages. This a) prevents the type of problem you're experiencing, b) keeps the functionality clean and clear and separate, making unit testing a lot easier, and b) makes it more like an API which just deals with data and can potentially be called from other applications too, if that's useful. – ADyson Feb 16 '21 at 21:00
  • 1
    @LucasHenrique the OP isn't using jQuery as far as we know, so that's probably going to cause more confusion than help. This is not the time to introduce a whole new library (which they don't need anyway, just for making an AJAX request) – ADyson Feb 16 '21 at 21:01
  • @ADyson thanks, adding exit() worked. – vytasK Feb 16 '21 at 21:06
  • No problem. I have added it as a proper Answer below, so you can mark it as accepted / upvote - thanks. Next time please explain the real problem from the beginning. Your original explanation made it sound like it wasn't printing anything at all. You will get better answers faster, with less effort, if you make the description as clear, accurate and detailed as possible. I've added the real situation back into your main question, so that it (and the answer) makes sense to future readers :-) – ADyson Feb 16 '21 at 21:09

1 Answers1

1

It sounds like you have other code in index.php, then?

If you only want it to print the ID, add an exit(); command just after it echoes the ID, so that the rest of the script is not processed in that situation. (That's assuming that the PHP code comes before the HTML in the file, of course.)


P.s. Most developers tend to separate the PHP which responds to Ajax requests from the PHP which deals with loading HTML pages. This

a) prevents the type of problem you're experiencing,

b) keeps the functionality clean and clear and separate, making unit testing a lot easier, and

c) takes a step to it being more like a fully-fledged API which just deals with data - and can potentially be called from other applications, services or front-ends too, if that's useful.

ADyson
  • 57,178
  • 14
  • 51
  • 63