tash517 tash517 - 1 year ago 94
jQuery Question

Insert data to cpanel database

So I'm working on this project where users can sign up for the newsletter and their names get added to the online database. I am using the online database (CPanel - PhPMyAdmin) I've been stuck on what type of code am I supposed to use to add this data from my form to the database.

This is what I have so far and I'm not quite sure how to go from here. Any help is appreciated.

ALSO: I'm using Bootstrap3 and jquery/ajax

<div id="newsletter" class="container-fluid text-center">

<form id="signup-form" action="" method="">
<div class="input-prepend" ><span class="add-on"><i class="glyphicon glyphicon-user"></i></span>
<label for="name">Your Full Name</label>
<input type="text" id="name" name="name" placeholder="your full name" size="40" style="color:black;" class="validate" required>

<div class="input-prepend"><span class="add-on"><i class="glyphicon glyphicon-envelope"></i></span>
<label for="email">Your Email</label>
<input type="email" id="email" name="email" placeholder="your@email.com" size="40" style="color:black;"class="validate" required>

<button id="sub" type="submit" class="btn btn-large"> Sign Up!</button>


Answer Source

You can use Ajax

Example -

$('#button').click(function() {//give id of submit button 
    var val1 = $('#text1').val();//get value you need to post
    var val2 = $('#text2').val();//get value you need to post
        type: 'POST',
        url: 'process.php',
        data: { text1: val1, text2: val2 },
        success: function(response) {
            $('#result').html(response);//responce you recevied 
    }); });

Get both values using Post method. Perform your action and send true false in the result.

---------------Explaning in deep with new example----------------

HTML File: ajaxsubmit.html

<!DOCTYPE html>
<title>Submit Form Using AJAX and jQuery</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="script.js"></script>
<div id="mainform">
<h2>Submit Form Using AJAX and jQuery</h2> <!-- Required div Starts Here -->
<div id="form">
<h3>Fill Your Information !</h3>
<label>Name :</label>
<input id="name" type="text">
<label>Email :</label>
<input id="email" type="text">
<label>Password :</label>
<input id="password" type="password">
<label>Contact No :</label>
<input id="contact" type="text">
<input id="submit" type="button" value="Submit">

PHP File: ajaxsubmit.php

$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server..
$db = mysql_select_db("mydba", $connection); // Selecting Database
//Fetching Values from URL
//Insert query
$query = mysql_query("insert into form_element(name, email, password, contact) values ('$name2', '$email2', '$password2','$contact2')");
echo "Form Submitted Succesfully";
mysql_close($connection); // Connection Closed

jQuery File: script.js

var name = $("#name").val();
var email = $("#email").val();
var password = $("#password").val();
var contact = $("#contact").val();
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'name1='+ name + '&email1='+ email + '&password1='+ password + '&contact1='+ contact;
alert("Please Fill All Fields");
// AJAX Code To Submit Form.
type: "POST",
url: "ajaxsubmit.php",
data: dataString,
cache: false,
success: function(result){
return false;

MY-SQL Code Segment:

CREATE TABLE form_element(
name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
password varchar(255) NOT NULL,
contact varchar(255) NOT NULL,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download