I have created get_data(id)
and update_data()
function in item.js
file and corresponding get_data.php
and update_data.php
files. In my code get_data(id)
function is working perfectly but update_data()
is not updating the value in database. It seems like function is not calling update_data.php
. I've tried implementing ajax and php for the first time, Please help me to figure out the exact problem in my code. Here is my code
function loadItem(id) {
console.log(id);
$.ajax({
url: "get_data.php",
type: "POST",
data: {'id': id},
success: function (result) {
console.log(result);
var d = JSON.parse(result);
document.getElementById("ib_name").value = d.category[0].ic_name;
}
});
}
function updateItem() {
var id = document.getElementById("ib_id").value;
var name = document.getElementById("ib_name").value;
$.ajax({
url: "update_data.php",
type: "POST",
data: {'id': id, 'name': name},
success: function (result) {
console.log(result);
}
});
}
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<h1>Test Page</h1>
<button onclick="loadItem(2);">Load Data</button>
<br/>
<br/>
<br/>
<br/>
<form method="post">
<input type="text" name="ib_name" id="ib_name" value="" class="form-control"
placeholder="i.e HP, Dell, Sony etc." required/>
<br/> <button onclick="updateItem();">Update Data </button>
</form>
<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/item.js"></script>
</body>
</html>
get_data.php
<?php
include 'connection.php';
$id=$_POST["id"];
$category = array();
$temp = array();
$query = mysqli_query($link, "SELECT * FROM item_category WHERE ic_id = ".$id);
//$query = mysqli_query($link, "SELECT * FROM item_category");
while ($row = mysqli_fetch_array($query)) {
$temp['ic_id'] = $row['ic_id'];
$temp['ic_name'] = $row['ic_name'];
array_push($category, $temp);
}
$response['category'] = $category;
echo json_encode($response);
?>
update_data.php
<?php
include 'connection.php';
$id=$_POST['id'];
echo "id is".$id;
$name=$_POST['name'];
$query = mysqli_query($link, "UPDATE item_category SET ic_name = '$name' WHERE ic_id = ".$id);
echo "updated";
?>
Aucun commentaire:
Enregistrer un commentaire