ajax insert
index.php
<!DOCTYPE html>
<html>
<head>
<title>Ajax insert</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<form >
<div class="form-group">
<div class="form-label">Names</div>
<input type="text" name="name" id="name" class="form-control">
</div>
<div class="form-group">
<div class="form-label">Surname</div>
<input type="text" name="surname" id="surname" class="form-control">
</div>
<div class="form-group">
<input type="button" name="submit" value="Submit" id="submit">
</div>
</form>
</div>
<div class="col-md-3" >
<textarea id="view"></textarea>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
var name=$("#name").val();
var surname=$("#surname").val();
$.post("insert.php",{fname:name,sname:surname},function(data){
$("#view").html(data);
/*var dataString='fname='+ name +'sname='surname;
$.ajax({
method:"post",
url:"insert.php",
data:dataString,
success:function(data){
$("#view").html(data);
}
*/
});
});
});
</script>
</body>
</html>
insert.php
<?php
$con=mysqli_connect('localhost','root','root','insert');
if (isset($_POST['fname']) && isset($_POST['sname'])) {
$name=$_POST['fname'];
$surname=$_POST['sname'];
$query=mysqli_query($con,"insert into db_insert(name,surname) values('$name','$surname')") or die(mysqli_error($con));
if($query)
{
echo "record inserted successfully";
}
else {
echo "failed";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Ajax insert</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<form >
<div class="form-group">
<div class="form-label">Names</div>
<input type="text" name="name" id="name" class="form-control">
</div>
<div class="form-group">
<div class="form-label">Surname</div>
<input type="text" name="surname" id="surname" class="form-control">
</div>
<div class="form-group">
<input type="button" name="submit" value="Submit" id="submit">
</div>
</form>
</div>
<div class="col-md-3" >
<textarea id="view"></textarea>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
var name=$("#name").val();
var surname=$("#surname").val();
$.post("insert.php",{fname:name,sname:surname},function(data){
$("#view").html(data);
/*var dataString='fname='+ name +'sname='surname;
$.ajax({
method:"post",
url:"insert.php",
data:dataString,
success:function(data){
$("#view").html(data);
}
*/
});
});
});
</script>
</body>
</html>
insert.php
<?php
$con=mysqli_connect('localhost','root','root','insert');
if (isset($_POST['fname']) && isset($_POST['sname'])) {
$name=$_POST['fname'];
$surname=$_POST['sname'];
$query=mysqli_query($con,"insert into db_insert(name,surname) values('$name','$surname')") or die(mysqli_error($con));
if($query)
{
echo "record inserted successfully";
}
else {
echo "failed";
}
}
?>
Comments
Post a Comment