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

</form>
<div style="clear:both;"></div>
</div>
</body>
</html>

CSS Code For better look HTML Form

<style>
.container{
    width:600px;
    margin:auto;
    background:#CCFFFF;
    padding:20px;
    border:1px solid #666666;
    border-radius:5px;
}
h2{color:#000000; font-size:28px;}
.textbox{width:100%; padding:5px;}
.textarea{width:100%; padding:5PX; height:100px;}
.button{
    float:right;
    padding:10px 30px;
    background-color:#CCCCCC;
    border:#999999 1px solid;
    margin-top:10px;
    cursor:pointer;
}
</style>

JavaScript AJAX Code

<script>
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;
    err="";
    if(name=="")
    {
        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();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    ajaxUrl_code = baseUrl+"phpresponce.php?newrequest=1&name&"+name+"&email="+email+"&phoneno="+phoneno+"&message="+message;
    alert(ajaxUrl_code);
    xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4)
    {
        msg=xmlhttp.responseText;
        //msg = JSON.parse(xmlhttp.responseText);
        if(msg=="1")
        {
            document.getElementById("error").style.color="green";
            document.getElementById("error").innerHTML="Form has been submited!";
        }
        else if(msg=="0")
        {
            document.getElementById("error").style.color="red";
            document.getElementById("error").innerHTML="Please try again! Something is wrong.";
        }
    }
    }
    xmlhttp.open("GET", ajaxUrl_code, true);
    xmlhttp.send();
}
</script>

PHP Code

<?php
$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, "
CREATE TABLE IF NOT EXISTS `mytest` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`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,
PRIMARY KEY (`id`)
)") 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'];
    $created_date=date("d-m-Y");
    $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));
    
    if(!empty($query))
    {
        echo "1";
    }
    else
    {
        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.