Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

JavaScript - how to make jQuery AJAX POST request with PHP?

0 contributions
2 points

1. Overview

In JavaScript it is possible to make AJAX POST request with jQuery to PHP backend in following way.

2. jQuery AJAX POST request to PHP backend example

ajax.htm file:

<!doctype html>
<html lang="en">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<pre id="response"></pre>
<script>

    var handle = document.getElementById('response');

    $.ajax({
        type: 'POST',
        url: '/backend.php',
        data: {
            name: 'John',
            age: 25
        },
        success: function (data) {
            handle.innerHTML = 'Response:\n' + data;
        },
        error: function (jqXHR) {
            handle.innerText = 'Error: ' + jqXHR.status;
        }
    });

</script>
</body>
</html>

backend.php file:

<?php

	if ($_SERVER['REQUEST_METHOD'] === 'POST')
	{
		echo "  Sent data to backend:\n";
		echo "   + Name: " . $_POST['name'] . "\n";
		echo "   + Age: " . $_POST['age'] . "\n";
	}
	else
	{
		echo "  Incorrect request method!";
	}

Note: ajax.htm and backend.php files should be placed on php server both.

Result:

jQuery AJAX POST request to PHP backend
jQuery AJAX POST request to PHP backend

3. jQuery AJAX POST request with alternative API to PHP backend example

ajax.htm file:

<!doctype html>
<html lang="en">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<pre id="response"></pre>
<script>

    var handle = document.getElementById('response');

    var data = {
        name: 'John',
        age: 25
    };

    $.post('/backend.php', data)
        .done(function (data) {
            handle.innerHTML = 'Response:\n' + data;
        })
        .fail(function (jqXHR) {
            handle.innerText = 'Error: ' + jqXHR.status;
        });

</script>
</body>
</html>

backend.php file:

<?php

	if ($_SERVER['REQUEST_METHOD'] === 'POST')
	{
		echo "  Sent data to backend:\n";
		echo "   + Name: " . $_POST['name'] . "\n";
		echo "   + Age: " . $_POST['age'] . "\n";
	}
	else
	{
		echo "  Incorrect request method!";
	}

Note: ajax.htm and backend.php files should be placed on php server both.

Result:

jQuery AJAX POST request with alternative API to PHP backend
jQuery AJAX POST request with alternative API to PHP backend

References

  1. jQuery.post method - jQuery Docs 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more