Here's html code that contains a textfield to get a value for update record to MySQL database
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<div id="reportListPage" data-role="page" data-add-back-btn="true">
<div data-role="header">
<h1>Laporan Terakhir</h1>
</div>
<div data-role="content">
<div id="">
<h3 id="no"></h3>
<p id="nama"></p>
<p id="alamat"></p>
<p id="tanggal"></p>
<form action="" method="post" name="submit">
<input id="nama" type="hidden" name="nama">
<input name="kwh" type="text" placeholder="Angka KwH">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
<ul id="reportList" data-role="listview" data-inset="true"></ul>
</div>
</div>
</body>
</html>
this is my js script, it might has function to display record from database tabel
$('#reportListPage').live('pageshow', function(event) {
var id = getUrlVars()["id"];
var date = new Date();
console.log(date.toLocaleDateString());
$.getJSON(serviceURL + 'getreports.php?id='+id, displayEmployee);
});
function displayEmployee(data) {
var employee = data.item;
$('#no').text(employee.no );
$('#nama').text(employee.nama);
$('#alamat').text(employee.alamat);
$('#tanggal').text(Date);
if (employee.reportCount>0) {
$('#actionList').append('<li>
<a href="reportlist.html?id=' + employee.id + '"><h1>Pemakaian Terkahir</h1>' +
'<p>' + employee.tanggal + '</p>' +
'<p5>' + employee.KWH + '</p5>' + '<p5> KwH</p5>'+
'<p>' + employee.reportCount + '</p></a></li>');
}
$('#actionList').listview('refresh');
}
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?')
+ 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
and this is my PHP code to connect and fetch data
<?php
include 'config.php';
$sql = "select e.id, e.no, e.nama, e.alamat, e.tanggal, count(r.id) reportCount " .
"from employee e left join employee r on r.id = e.id " .
"where e.id=:id group by e.nama order by e.tanggal, e.KWH";
try {
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare($sql);
$stmt->bindParam("id", $_GET[id]);
$stmt->execute();
$employee = $stmt->fetchObject();
$dbh = null;
echo '{"item":'. json_encode($employee) .'}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
?>
and this is my localhost data
INSERT INTO employee
(id,
no,
nama,
alamat,
tanggal,
kwh)
VALUES (1,
'12345',
'mukhtar hasan',
00 - 00 - 0000,
'0')
How can i make a proper way to post the value from textbox to update MySQL record ?