Ajax Insert Data MySQL


client-demo.php
<!DOCTYPE html>
<html>
<head>
<title>JavaScript [Demo]</title>
<script type="text/javascript" src="js/jquery-2.2.1.js"></script>
<style type="text/css">
*{
font-size: 14px;
font-family: arial;
}
.red{
background-color: red;
padding: 5px;
}
.blue{
background-color: blue;
padding: 5px;
}
</style>
</head>
<body>
<p>Username: <input type="text" id="txtusername" name="txtusername"/></p>
<p>Password: <input type="text" id="txtpassword" name="txtpassword"/></p>
<button>Send Page.</button>
<hr/>
<div class="student_info">

</div>
<script type="text/javascript">
$(function(){
$('button').on('click',function(){
//todo.
var formData = {
'username':$('#txtusername').val(),
'password':$('#txtpassword').val()
};
$.post( "mi_test.php", formData).done(function( data ) {
debugger;
});
})
});
</script>
</body>
</html>

--------------------------------------------------------
mi_test.php

<?php 
$db = mysql_connect('localhost','root','');
mysql_select_db('db_utility',$db);
$getUser = $_POST['username'];
$getPass = $_POST['password'];
$sql = mysql_query("insert into user_json (username,password) values ('$getUser','$getPass')",$db);
$arrMsg = array();
$arrMsg['result'] = 'some parameter is missing.';
if($sql){
$arrMsg['result'] = 'Data has been inserted.';
}
echo json_encode(array('result'=>$arrMsg));

?>

-----------------------------------------
user_json.sql

CREATE TABLE IF NOT EXISTS `user_json` (
  `id` int(11) NOT NULL,
  `username` varchar(25) NOT NULL,
  `password` varchar(25) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment