一切都发生在一个 php 文件中
HTML 代码
<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>
然后是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>
和 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>';
}
}
那么会发生什么。无需单击按钮的 Javascript(ajax)接受用户输入,发送到 php 代码。php代码更新mysql。这意味着以某种方式在不单击提交按钮的情况下创建要更新的 php 变量($amount1,$date_day1)?
但是为什么后面这些变量不存在呢?我想不刷新页面(不点击提交按钮)使用变量。例如在输入表单中作为 value=""
怎么做?据我了解需要使用json?但是从我发现的信息无法理解....有人可以逐步编写如何使用json将用户输入(或来自mysql的值)传递给输入值=“”
或者可能是一些书/教程如何运作(书/教程让傻瓜理解)?