我正在制作一个网页,其中包含 html 表单和一些处理表单数据的 php 代码。我想要做的是使其在每次按下按钮并将数据保存到文件后不会刷新页面?我该如何去做呢?
这是代码:
<html>
<head>
</head>
<body>
<form method="post" action="out.php">
<input type="submit" name="foo" value="A" />
<input type="submit" name="foo" value="B" />
<input type="submit" name="foo" value="C" />
<input type="submit" name="foo" value="D" />
<input type="submit" name="foo" value="E" />
<input type="submit" name="foo" value="F" />
<input type="submit" name="foo" value="G" />
</form>
<?php
$name = $_POST['foo'];
$fp = fopen("formdata.txt", "w");
fwrite($fp, "");
$savestring = $name;
fwrite($fp, $savestring);
fclose($fp);
?>
</body>
</html>
最佳答案
如果您完全避免使用 JavaScript,则可以定位隐藏的 iframe。
CSS:
iframe.hidden{ display:none }
HTML:
<iframe name="iframe" width="0" height="0" tabindex="-1" class="hidden"></iframe>
<form method="post" action="out.php" target="iframe">...
但是,您最好使用 JavaScript 来确保数据已正确提交。如果用户的 session 已过期怎么办?如果他们的互联网连接不稳定怎么办?他们如何知道数据已验证正常?
关于php - 提交 html 表单,无需刷新或 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30545106/