0

I can not get the PHP echo in my Ajax.

This is my test01.html code:

In my HTML code, I quote the jQuery, and I bind the event in my "sub01" button, I use Ajax provide the POST request:

$(".sub01").bind("click", function() {
  $.ajax({
    type: "POST",
    url: "cms/test01.php?action=test01",
    dataType: "json",
    data: {
      "var_01": "var_01_value",
      "var_02": "var_02_value",
    },
    success: function(response) {
      console.log(response) // there I want to console the response from PHP.
    }
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <form>
    <input type="text" value="var_01">
    <input type="text" value="var_02">
    <input id="sub01" type="submit" value="click me!">
  </form>
</div>

And this is my PHP test01.php code:

in my PHP code I want to echo the $_POST, then I want the AJAX code get the response.

<?php
echo $_POST;

I want the code

success: function(response) {
  console.log(response) // there I want to console the response from PHP.
}

shows the PHP echo, but there console nothing.

5
  • 2
    If you press F12 in your browser and go to network analysis -> run your ajax request -> what does your request send as response? Commented Aug 30, 2019 at 9:31
  • 1
    You can't echo $_POST as $_POST is an array, thus this will result in a notice. Go with var_dump($_POST) Commented Aug 30, 2019 at 9:32
  • Btw echo $_POST doesnt make sense, it will just print Array. You´d have to use $_POST['var_01'] or var_02 Commented Aug 30, 2019 at 9:32
  • you need to add in form method=post Commented Aug 30, 2019 at 9:33
  • @vikas “you need to add in form method=post” - no they don’t, because that form itself does not get submitted. The $.ajax call is what sends the data to the server, and in there it is specified that the request method is to be POST. Commented Aug 30, 2019 at 9:35

2 Answers 2

8

Problem 1

You never call your JS function

$(".sub01").bind

You are binding to the click event of elements with the class sub01.

<input id="sub01" type="submit" value="click me!">

Your button doesn't have that class!

Aside: bind is deprecated in favour of on.

$("#sub01).on("click" ....

Problem 2

You aren't letting the JS run

Since you are (trying) to run the JS when a submit button is clicked, the form is submitting.

The browser is leaving the page before the Ajax response comes back and triggers the success function.

You need to prevent the default behavior of the submit button.

$("#sub01").on("click", function (event) {
    event.preventDefault();
    // etc
});

Problem 3

You aren't parsing the response correctly

You said dataType: "json", which means "Tell the server I only Accept JSON and ignore the Content-Type and parse the response as JSON regardless".

Since the server is not responding with JSON, the attempt to parse the response fails and it errors.

Option 1

Remove dataType: "json".

This will process the response as invalid HTML (because PHP sends Content-Type: text/html by default).

Then you will echo the response (which is Array … probably not the response you want PHP to send though).

Option 2

Change the PHP so it responds with JSON

<?php
    header("Content-Type: application/json");
    echo json_encode($_POST, JSON_PRETTY_PRINT);
Sign up to request clarification or add additional context in comments.

2 Comments

Add a hint about browser's dev tools to see the response, and this should be full answer.
@s-n-2 — I've updated the answer with some notes on the other problems I spotted when I went looking past the obvious error.
0

You need to output a json.

header('Content-Type: application/json');
echo json_encode($_POST);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.