PHP-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.

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>


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";
    }
    
}
?>

Post a Comment

Previous Post Next Post