Tags down


How to console the PHP echo in my javascript?

By : Suman
Date : May 03 2020, 08:12 AM

I try to get PHP echo in my ajax function.

This is my page1.html code:

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

$(".sub01").bind("click", function() {
    type: "POST",
    url: "cms/page1.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>
    <input type="text" value="var_01">
    <input type="text" value="var_02">
    <input id="sub01" type="submit" value="click me!">

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.

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  the console empty .

Answer :

issue 1

You D'not call your Java script function


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" ....



You D'not 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) {
    // etc



You D'not parsing the response correct :

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 ajax JSON good:

    header("Content-Type: application/json");
    echo json_encode($_POST, JSON_PRETTY_PRINT);

Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • php function return an array
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • php array_push empty
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql insert into existing row
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org