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

Leave a Reply

Related Posts: You may like

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


Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.