All happens in one php file
HTML code
<td>
<input type="text" name="date_day1" id="date_day1"
value="<?php echo $_POST['date_day1']?>" size="1">
</td>
<td>
<input type="text" name="amount1" id="amount1"
value="<?php echo $_POST['amount1']?>" size="5"></td>
Then javascript
<script type="text/javascript">
//cross-browser xmlHTTP getter
function getHTTPObject() {
var xmlhttp; // The variable that makes Ajax possible!
//Global XMLHTTP Request object
//Creating object of XMLHTTP in Internet Explorer
try {
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(oc) {
XmlHttp = null;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
// Set var the new request Opera 8.0+, Firefox, Safari
xmlhttp = new XMLHttpRequest();
}//try {
catch (e) {//if it fails move onto the next
xmlhttp = false;
}//catch (e) {
}//if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
return xmlhttp;
}//function getHTTPObject() {
function init(){
window.setInterval(autoSave,3000); // 15000=15 seconds
}
function autoSave(){
var date_day1 = document.getElementById("date_day1").value;
var amount1 = document.getElementById("amount1").value;
var params = "date_day1="+date_day1+"&amount1="+amount1;
var http = getHTTPObject();
http.open("POST", window.location.href, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);
}
</script>
And php
$date_day1 = $_POST['date_day1'];
$amount1 = $_POST['amount1'];
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($_SERVER["REQUEST_METHOD"]=="POST"){
if ($stmt = mysqli_prepare($mysqli,
"UPDATE 2_1_journal SET Amount = ? WHERE RecordDay = ? ") ) {
$stmt->bind_param( 'ds', $amount1 , $date_day1 );
$stmt->execute();
echo $date_day1 .' date_day1 from update<br>';
echo $amount1 .' amount1<br>';
}
}
So what happens. Javascript (ajax) without clicking button takes user input, send to php code. Php code updates mysql. That means that somehow without clicking submit button are created php variables to update ($amount1 , $date_day1)?
But why these variables do not exist latter? I want without page refresh (without clicking submit button) use variables. For example in input form as value=""
How to do that? As I understand need to use json? but from information I have found can not understand.... Can someon write step by step how with json pass user input (or value from mysql) to input value=""
Or may be some book/tutorial how all works (book/tutorial for dummies to understand)?