<?php
$conn = mysql_connect("localhost","root","");
if($conn){
mysql_select_db('ajax',$conn);
}else{
echo "error connection_aborted";
}
?>
=====================================================================
<?php
include "config.php";
if(isset($_POST['update'])){
$sql ="UPDATE student1 SET
student_name = '".$_POST['sn']."',
gender = '".$_POST['gender']."',
phone = '".$_POST['phone']."'
WHERE id = '".$_POST['id']."'
";
mysql_query($sql);
echo 0;
exit();
}
if(isset($_POST['edit'])){
$sql = " SELECT * FROM student1 where id ='{$_POST['idgg']}'";
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
header("Content-type: text/x-json");
echo json_encode($row);
exit();
}
if(isset($_POST['delete'])){
$sql = "DELETE FROM student1 WHERE id = '".$_POST['idajax']."'";
mysql_query($sql);
exit();
}
if(isset($_POST['show'])){
/*cara 1
?>
<tr>
<td>1</td>
<td>nama</td>
<td>gender</td>
<td>phone</td>
</tr>
<?php
*/
/*cara 1
echo "
<tr>
<td>1</td>
<td>nama</td>
<td>gender</td>
<td>phone</td>
</tr>
";
*/
$no=1;
$sql ="SELECT * FROM student1";
$result = mysql_query($sql);
while($row = mysql_fetch_object($result)){
$gender ='';
$r = $row->gender;
if($r==0){
$gender = "male";
}else{
$gender = "female";
}
?>
<tr>
<td><?=$no?></td>
<td><?=$row->student_name?></td>
<td><?=$gender?></td>
<td><?=$row->phone?></td>
<td><a href="#" idd="<?=$row->id?>" class="edit">Edit</a>|<a href="#" data-id="<?= $row->id?>" class="delete">delete</a></td>
</tr>
<?php
$no++;
}
exit();
}
if(isset($_POST['save1'])){
$sql="INSERT INTO student1 (student_name,gender,phone) values('".$_POST['studentnamebb']."','".$_POST['gender']."','".$_POST['phone']."')";
mysql_query($sql);
echo 0;
exit();
}
?>
<html>
<head><title></title></head>
<body>
<div id="tablesss">
<h3>Ajax jquery Json Crud</h3>
<table id="data">
<tr>
<td>StudentNama</td>
<td>:</td>
<td><input type="text" name="studentname" id="studentname"></td>
</tr>
<tr>
<td>Gender</td>
<td>:</td>
<td><select name="gender" id="gender">
<option value="0">Male</option>
<option value="1">female</option>
</select>
</td>
</tr>
<tr>
<td>Phone</td>
<td>:</td>
<td><input type="text" name="phone" id="phone"></td>
</tr>
<input type="text" id="id">
<input type="button" value="save" id="save">
<input type="button" value="update" id="update">
</table>
</div>
<div id="msg"></div>
<div>
<table>
<thead >
<th>No</th>
<th>StudentName</th>
<th>Gender</th>
<th>Phone</th>
<th>Action</th>
</thead>
<tbody id="showsdata">
</tbody>
</table>
</div>
<script src="../../js/jquery-2.1.4.min.js"></script>
</body>
</html>
<script>
$(document).ready(function(){
showdata();
//update
$('#update').click(function(){
//alert('update');
var id = $('#id').val();
var sn = $('#studentname').val();
var gender = $('#gender').val();
var phone = $('#phone').val();
$.ajax({
url : 'index.php',
type : 'POST',
async: false,
data : {
'update':1,
'id':id,
'sn':sn,
'gender':gender,
'phone':phone
},
success: function(up){
if(up==0){
alert('update success');
$('#studentname').val('');
$('#phone').val('');
$('#id').val('');
showdata();
}
}
});
});
//end update
//edit
$('body').delegate('.edit','click',function(){
var idvv = $(this).attr('idd');
$.ajax({
url : 'index.php',
type : 'POST',
async : false,
data : {
'edit' :1,
'idgg' : idvv
},
success : function(edit){
//alert(edit);
$('#id').val(edit.id);
$('#studentname'). val(edit.student_name);
$('#gender'). val(edit.gender);
$('#phone'). val(edit.phone)
}
});
});
//end edit
//delete
$('body').delegate('.delete','click',function(){
var id = $(this).data('id');
$.ajax({
url : 'index.php',
type : 'POST',
async : false,
data : {
'delete' : 1,
'idajax' : id
},
success : function(rr){
alert('delete success');
showdata();
}
});
});
//end delete
//css
var cssobj = {
'color' : 'yellow',
'background-color' : 'black',
'height' : '30px',
'width' : '280px'
}
//$('h3').css('color','red');
$('h3').css(cssobj);
//end css
//save data
$('#save').click(function(){
var studentname1 = $('#studentname').val();
var gender1 = $('#gender').val();
var phone1 = $('#phone').val();
$.ajax({
url : "index.php",
type : "POST",
async : false,
data : {
'save1' : 1,
'studentnamebb' : studentname1,
'gender' : gender1,
'phone' : phone1
},
success : function(r){
if(r==0)
{
alert('insert success');
$('#studentname').val('');
$('#phone').val('');
showdata();
}
}
});
});//end save data
});
function showdata(){
$.ajax({
url : 'index.php',
type : 'POST',
async : false,
data : {
'show' : 1
},
success : function(re){
$('#showsdata').html(re);
}
});
}
</script>
output
0 Response to "crud ajax jquery json "
Posting Komentar