以下代码是HTML和PHP代码。我想按下提交按钮比执行practice.php文件(make test.txt文件)。但如果我按下按钮php不会生成文件。任何人都知道这个??
<HTML>
<HEAD>
</HEAD>
<BODY onLoad="tid=setInterval('refresh()',1000);">
<div id="outputDiv"></div>
<form method=post action="practice.php">
<script language="JavaScript">
function refresh()
{
var HTML = "";
HTML += "<input type=submit value=makefile>";
document.getElementById("outputDiv").innerHTML = HTML;
}
</script>
</form>
</BODY>
</HTML>
<html>
<head><meta http-equiv="Content-Type" content="text/html" charset="utf-8">
</head>
<body>
<?
$fp = fopen("test.txt", "w") or die("cannot open file");
$data = "!!!!!!!!!!!!!!!!!!!";
fwrite($fp, $data);
fclose($fp);
echo "!!!!!!!!!!!!!!!";
?>
</body>
</html>
答案 0 :(得分:-1)
使用“a”标志,如果没有文件,则会自动创建。
$fp = fopen("test.txt", "a")
使用JQuery,这很简单。
<input type="submit" value="Load file" id="button"/>
<script>
//If document is ready.
$(document).ready(function(){
//If button is clicked.
$("#button").click(function(){
//Load a file in "outputdiv" div.
$("#outputDiv").load("test.txt");
//Assuming you are trying to refresh div after 1000 ms sec, Therefor:
setInterval(function(){
$("#outputDiv").load("test.txt");
},1000);
});
});
</script>
答案 1 :(得分:-1)
您必须将提交按钮放在表单元素中。该脚本将提交按钮放在表单元素之外的<div id="outputDiv"></div>
元素内。当您按提交时,浏览器不知道要提交哪个表单。
例如,这会更好:
<HTML>
<HEAD>
</HEAD>
<BODY onLoad="tid=setInterval('refresh()',1000);">
<form method=post action="practice.php" id="form">
<script language="JavaScript">
function refresh()
{
var HTML = "";
HTML += "<input type=submit value=makefile>";
document.getElementById("form").innerHTML += HTML;
}
</script>
</form>
</BODY>
</HTML>