Form Submission Using Ajax PHP and Javascript

Form Submission Using Ajax PHP and Javascript

In this tutorial i am going to share the most useful code use at today date Form Submission Using Ajax PHP and Javascript.
Using this code you can submit the PHP web forms or web form data without refreshing the whole web page of your site, The JavaScript get() and post() methods are used to request data from the server with an HTTP GET or POST request. let’s see it in detail.

In this tutorial i have created here a simple HTML form which contain some inputs from the user such as, User name, email id, contact no and message below the form is a submit button, this submit button use for call JavaScript function ( submitform() ) where i was written my JavaScript and Ajax code.

Now you will learn same functionality using ajax, PHP and JavaScript through this blog post. Just follow our post step by step for better result or download it to use. downloading link given below of the post.

First Create HTML Form

<!DOCTYPE html>
<title>Submit Form Using AJAX PHP and javascript</title>
<div class="container">
<h2>Submit Form Using AJAX, PHP and JavaScript</h2>
<!-- Required Div Starts Here -->
<form id="myform" name="myform">
        <label>Name :</label>
        <input id="name" type="text" class="textbox">
        <label>Email :</label>
        <input id="email" type="text" class="textbox">
        <label>Phone No :</label>
        <input id="phoneno" type="text" class="textbox">
        <label>Message :</label>
        <textarea name="message" id="message" class="textarea"></textarea>
        <p id="error" style="color:#FF0000; float:left;"></p>
        <input id="submit" onClick="submitform()" type="button" value="Submit" class="button">

<div style="clear:both;"></div>

CSS Code For better look HTML Form

    border:1px solid #666666;
h2{color:#000000; font-size:28px;}
.textbox{width:100%; padding:5px;}
.textarea{width:100%; padding:5PX; height:100px;}
    padding:10px 30px;
    border:#999999 1px solid;

JavaScript AJAX Code

var baseUrl = "http://www.phpkida/"; // You can set your baseurl here like that

function submitform()
    var name=document.getElementById("name").value;
    var email=document.getElementById("email").value;
    var phoneno=document.getElementById("phoneno").value;
    var message=document.getElementById("message").value;
        document.getElementById("name").style.borderColor = "red";
        document.getElementById("error").innerHTML="Enter your name!";
    else if(email=="")
        document.getElementById("email").style.borderColor = "red";
        document.getElementById("error").innerHTML="Enter your email id!";
    else if(phoneno=="")
        document.getElementById("phoneno").style.borderColor = "red";
        document.getElementById("error").innerHTML="Enter Your Phone Number!";
    else if(message=="")
        document.getElementById("message").style.borderColor = "red";
        document.getElementById("error").innerHTML="Enter Your Message!";
    else if(name!="" && email!="" && phoneno!="" && message!="")
        document.getElementById("error").style.color = "green";
        document.getElementById("error").innerHTML="Form Submiting";

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    ajaxUrl_code = baseUrl+"phpresponce.php?newrequest=1&name&"+name+"&email="+email+"&phoneno="+phoneno+"&message="+message;
    xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4)
        //msg = JSON.parse(xmlhttp.responseText);
            document.getElementById("error").innerHTML="Form has been submited!";
        else if(msg=="0")
            document.getElementById("error").innerHTML="Please try again! Something is wrong.";
    }"GET", ajaxUrl_code, true);

PHP Code

$host = "localhost";
$user = "root";
$password = "";
$database = "test";

$conn=mysqli_connect($host, $user, $password) or die(mysqli_error($conn));
mysqli_select_db($conn, $database) or die(mysqli_error($conn));

mysqli_query($conn, "
`name` varchar(50) NOT NULL,
`email` varchar(60) NOT NULL,
`phoneno` varchar(15) NOT NULL,
`message` text NOT NULL,
`created_date` varchar(50) NOT NULL,
)") or die(mysqli_error($conn));

if(isset($_REQUEST['newrequest']) && isset($_REQUEST['name']) && isset($_REQUEST['email']) && isset($_REQUEST['phoneno']) && isset($_REQUEST['message']))
    echo $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $phoneno = $_REQUEST['phoneno'];
    $message = $_REQUEST['message'];
    $query = mysqli_query($conn, "insert into mytest(`name`, `email`, `phoneno`, `message`, `created_date`) values ('$name', '$email', '$phoneno', '$message', '$created_date')") or die(mysqli_error($conn));
        echo "1";
        echo "0";

Click on Image and download Source Code of This Tutorial

Form Submission Using Ajax PHP and Javascript

Author: Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.
I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution of problems which i already solved.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts: You may like

How to login with LinkedIn oAuth2 in PHP

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

Convert Base64 string to an image file

Fullscreen Background Video Using HTML5 And CSS

How to get form filed names with values php

how to get current week start date and end date in php

PHPKIDA Offering Free Tools:


Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now


Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now


Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now


Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now


Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now


Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.