Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
Uncategorized

jQuery Ajax Call to PHP Script with JSON Return

By techsupport
June 6, 2019 3 Min Read
Comments Off on jQuery Ajax Call to PHP Script with JSON Return

User experience plays a vital role in excellent web design. Ajax allows you to run server-side validations, form submissions, data retrieval, and other server-side stuff in the background (asynchronously) without interfering with the existing page where the request was made. It’s a beautiful thing, but best of all it’s not very difficult to do.

Try the Demo

HTML

We’ll first start with a form that you’d like to process with Ajax.

<form action="return.php" class="js-ajax-php-json" method="post" accept-charset="utf-8">
  <input type="text" name="favorite_beverage" value="" placeholder="Favorite restaurant" />
  <input type="text" name="favorite_restaurant" value="" placeholder="Favorite beverage" />
  <select name="gender">
    <option value="male">Male</option>
    <option value="female">Female</option>
  </select>
  <input type="submit" name="submit" value="Submit form"  />
</form>

You’ll add the form somewhere in the <body>. I added two input fields and one dropdown field for this demo to give you a preview of how the information is processed and returned.

Now we’ll add just a placeholder <div> that we’ll use to give you a visual of the information returned.

<div class="the-return">
  [HTML is replaced when successful.]
</div>

PHP

This is where the information that is given from the form will be passed through and processed. In this demo, we’ll save the file as response.phpin the same location of the HTML file above.

<?php
if (is_ajax()) {
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $_POST["action"];
    switch($action) { //Switch case for value of action
      case "test": test_function(); break;
    }
  }
}

//Function to check if the request is an AJAX request
function is_ajax() {
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function test_function(){
  $return = $_POST;
  
  //Do what you need to do with the info. The following are some examples.
  //if ($return["favorite_beverage"] == ""){
  //  $return["favorite_beverage"] = "Coke";
  //}
  //$return["favorite_restaurant"] = "McDonald's";
  
  $return["json"] = json_encode($return);
  echo json_encode($return);
}
?>

Line 2: First, check that the request that’s being made is an Ajax request with is_ajax().

Line 6: Check the value of action with a switch statement, iterating through the values you declared.

Once a switch value match is found for action, in this example test, it will run test_function(), which is where you’ll run things like validating, database storing, or anything else you want run before anything is returned.

Line 17: Store $_POST to $return, and make any changes to $returnso the original values of $_POST aren’t tampered with.

Line 25: Encode $return to JSON, set it as $return["json"], and return the JSON in the next line.

JavaScript

This is where the magic of processing the form and getting the returned values by response.php asynchronously happens. You’ll put the following in the <head>.

<script type="text/javascript">
$("document").ready(function(){
  $(".js-ajax-php-json").submit(function(){
    var data = {
      "action": "test"
    };
    data = $(this).serialize() + "&" + $.param(data);
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "response.php", //Relative or absolute path to response.php file
      data: data,
      success: function(data) {
        $(".the-return").html(
          "Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: " + data["favorite_restaurant"] + "<br />Gender: " + data["gender"] + "<br />JSON: " + data["json"]
        );

        alert("Form submitted successfully.\nReturned json: " + data["json"]);
      }
    });
    return false;
  });
});
</script>

For this demo, you’ll be pointing to response.php relatively because it should be in the same location.

Line 5: This is where you set the value of action which will be matched up against the case values in the switch statement from response.php. This way you can have multiple Ajax scripts pointing to response.phpand separate different functions by adding additional action values set corresponding functions in response.php.

When the form is processed successfully, it’ll run the scripts in lines 14-18. You can access individual returned values by data["value_name"]. For example to get the favorite beverage, since the name of the input is favorite_beverage, it’ll be data["favorite_beverage"]. For the full JSON value, use data["json"].

Demo

Here’s a demo for you to try and also download the source code so you can test it out for yourself and manipulate it to your liking. Enjoy!

Author

techsupport

Follow Me
Other Articles
Previous

How to create an automated task using Task Scheduler on Windows 10

Next

[어플제작] 하이브리드앱 개발 프레임웍 framework7

Categories

  • Android
  • Blog
  • Favorite Link
  • linux
  • Open Source
  • opencart
  • social
  • Tech
  • Uncategorized

Archives

  • January 2025
  • March 2024
  • August 2023
  • March 2023
  • February 2023
  • November 2021
  • August 2021
  • April 2021
  • March 2021
  • December 2020
  • October 2020
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • September 2019
  • August 2019
  • June 2019
  • October 2018
  • August 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
Copyright 2026 — Technical Personnal Blog. All rights reserved. Blogsy WordPress Theme