How To Submit Form Without Page Refreshing with jQuery In PHP ?


1 Create a form for submit data like below refreshform.html
<!DOCTYPE html> <html> <head> <title>Submit Form Without Refreshing Page</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <link href="css/refreshform.css" rel="stylesheet"> <script src="js/refreshform.js"></script> </head> <body> <div id="mainform"> <h2>Submit Form Without Refreshing Page</h2> <!-- Required Div Starts Here --> <form id="form" name="form"> <h3>Fill Your Information!</h3> <label>Name:</label> <input id="name" placeholder="Your Name" type="text"> <label>Email:</label> <input id="email" placeholder="Your Email" type="text"> <label>Contact No.</label> <input id="contact" placeholder="Your Mobile No." type="text"> <label>Gender:</label> <input name="gender" type="radio" value="male">Male <input name="gender" type="radio" value="female">Female <label>Message:</label> <textarea id="msg" placeholder="Your message.."> </textarea> <input id="submit" type="button" value="Submit"> </form> </div> </body> </html>

....................................

jQuery File – refreshform.js




$(document).ready(function() { $("#submit").click(function() { var name = $("#name").val(); var email = $("#email").val(); var contact = $("#contact").val(); var gender = $("input[type=radio]:checked").val(); var msg = $("#msg").val(); if (name == '' || email == '' || contact == '' || gender == '' || msg == '') { alert("Insertion Failed Some Fields are Blank....!!"); } else { // Returns successful data submission message when the entered information is stored in database. $.post("refreshform.php", { name1: name, email1: email, contact1: contact, gender1: gender, msg1: msg }, function(data) { alert(data); $('#form')[0].reset(); // To reset form fields }); } }); });


................................................


My-SQL Code

CREATE DATABASE mydba;
CREATE TABLE form_element ( id int(25) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, email varchar(255) NOT NULL, contact int(25) NOT NULL, gender varchar(255) NOT NULL, message varchar(255) NOT NULL, PRIMARY KEY (id) )


........................................


PHP File – refreshform.php





<?php // Establishing connection with server by passing "server_name", "user_id", "password". $connection = mysql_connect("localhost", "root", ""); // Selecting Database by passing "database_name" and above connection variable. $db = mysql_select_db("mydba", $connection); $name2=$_POST['name1']; // Fetching Values from URL $email2=$_POST['email1']; $contact2=$_POST['contact1']; $gender2=$_POST['gender1']; $msg2=$_POST['msg1']; $query = mysql_query("insert into form_element(name, email, contact, gender, message) values ('$name2','$email2','$contact2','$gender2','$msg2')"); //Insert query if($query){ echo "Data Submitted succesfully"; } mysql_close($connection); // Connection Closed. ?>




Css File – refreshform.css

@import "http://fonts.googleapis.com/css?family=Fauna+One|Muli";
#form{ background-color:#556b2f; color:#D5FFFA; border-radius:5px; border:3px solid #d3cd3d; padding:4px 30px; font-weight:700; width:350px; font-size:12px; float:left; height:auto; margin-left:35px } label{ font-size:15px } h3{ text-align:center; font-size:21px } div#mainform{ width:960px; margin:50px auto; font-family:'Fauna One',serif } input[type=text]{ width:100%; height:40px; margin-top:10px; border:none; border-radius:3px; padding:5px } textarea{ width:100%; height:60px; margin-top:10px; border:none; border-radius:3px; padding:5px; resize:none } input[type=radio]{ margin:10px 5px 5px } input[type=button]{ width:100%; height:40px; margin:35px 0 30px; background-color:#f4a460; border:1px solid #fff; border-radius:3px; font-family:'Fauna One',serif; font-weight:700; font-size:18px }


Comments

Popular posts from this blog

How to filter data from database according to client name & date wise in PHP

Captcha Code In PHP