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.

Probably you need some assistance with Javascript code? Try JavaScript assignment help from AssignmentCore whose experts will do your homework of any complexity and within the deadline you need.

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

About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

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.