ItSolutionStuff.com

PHP JQuery Ajax Post Request Example

By Hardik Savani • May 14, 2024
PHP jQuery JSON Ajax

In this example, i will share with you how to write simple ajax request example with jquery php. we will see jquery ajax post data example with php. you can simply form submit with pass ajax post data and get return all data with success.

I will give you very simple example of ajax post request with php. you can also write server side validation using php logic. you can also pass form serialize ajax data to post method with php.

After this example you can easily write Ajax Get Request, Ajax Post Request, Ajax Put Request and Ajax Delete Request with jquery ajax and php.

You need to just follow bellow step to create ajax post request:

index.php

<!DOCTYPE html>

<html>

<head>

<title>Php Ajax Form Validation Example</title>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

</head>

<body>


<div class="container">

<h1>Php Ajax Form Validation Example</h1>

<form role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake">

<div class="alert alert-danger display-error" style="display: none">

</div>

<div class="form-group">

<div class="controls">

<input type="text" id="name" class="form-control" placeholder="Name">

</div>

</div>

<div class="form-group">

<div class="controls">

<input type="email" class="email form-control" id="email" placeholder="Email" >

</div>

</div>

<div class="form-group">

<div class="controls">

<input type="text" id="msg_subject" class="form-control" placeholder="Subject" >

</div>

</div>

<div class="form-group">

<div class="controls">

<textarea id="message" rows="7" placeholder="Massage" class="form-control"></textarea>

</div>

</div>

<button type="submit" id="submit" class="btn btn-success"><i class="fa fa-check"></i> Send Message</button>

</form>

</div>


</body>


<script type="text/javascript">

$(document).ready(function() {


$('#submit').click(function(e){

e.preventDefault();


var name = $("#name").val();

var email = $("#email").val();

var msg_subject = $("#msg_subject").val();

var message = $("#message").val();


$.ajax({

type: "POST",

url: "/formProcess.php",

dataType: "json",

data: {name:name, email:email, msg_subject:msg_subject, message:message},

success : function(data){

if (data.code == "200"){

alert("Success: " +data.msg);

} else {

$(".display-error").html("<ul>"+data.msg+"</ul>");

$(".display-error").css("display","block");

}

}

});


});

});

</script>

</html>

formProcess.php

<?php


$errorMSG = "";


/* NAME */

if (empty($_POST["name"])) {

$errorMSG = "<li>Name is required</<li>";

} else {

$name = $_POST["name"];

}


/* EMAIL */

if (empty($_POST["email"])) {

$errorMSG .= "<li>Email is required</li>";

} else if(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {

$errorMSG .= "<li>Invalid email format</li>";

}else {

$email = $_POST["email"];

}


/* MSG SUBJECT */

if (empty($_POST["msg_subject"])) {

$errorMSG .= "<li>Subject is required</li>";

} else {

$msg_subject = $_POST["msg_subject"];

}


/* MESSAGE */

if (empty($_POST["message"])) {

$errorMSG .= "<li>Message is required</li>";

} else {

$message = $_POST["message"];

}


if(empty($errorMSG)){

$msg = "Name: ".$name.", Email: ".$email.", Subject: ".$msg_subject.", Message:".$message;

echo json_encode(['code'=>200, 'msg'=>$msg]);

exit;

}


echo json_encode(['code'=>404, 'msg'=>$errorMSG]);


?>

You can quick run our example by following command, so run bellow command for run PHP project.

php -S localhost:8000

Now, you can check from your url by bellow URL:

http://localhost:8000

I hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

JQuery Ajax CRUD Operations in PHP MySQL Example

Read Now →

PHP MySQL Create Dynamic Treeview Example

Read Now →

PHP MySQL Column Sorting Example Tutorial

Read Now →

PHP Bootstrap Autocomplete Tokenfield using Ajax Example

Read Now →

Multiple File Upload using Dropzone JS in PHP Example

Read Now →

PHP CRUD Operation Using Ajax and JQuery Example

Read Now →

PHP Crop Image Before Upload using Croppie JS Example

Read Now →

PHP JQuery Select2 Ajax Autocomplete Example

Read Now →

PHP Paypal Payment Gateway Integration Example

Read Now →

File Upload with Progress Bar using jQuery Ajax and PHP Example

Read Now →

Mediafire PHP File Uploading and Get Link Example

Read Now →

PHP AngularJS CRUD with Search and Pagination Tutorial

Read Now →

PHP - I Can't get the $_POST Values on Ajax Request

Read Now →