如何在提交表单后保持相同 php 但仍将表单条目保存在文件中?
How to stay on same php after form submit but still save form entries in a file?
我有一个 html 表单,我想在其中使用 php.
将所有字段的所有条目保存到一个文件中
- 如果我能够成功保存条目,那么我想弹出消息说
{bytes} bytes written to file
。
- 如果我无法写入成功,那么我想弹出消息说
There was an error writing this file
。
- 如果用户没有写入权限,那么它应该给出弹出消息 -
Write access revoked
。
我从表单操作中调用 save.php
文件以将所有条目保存在一个文件中并添加一些验证。
下面是我的 index.php
文件,里面有表格 -
<?php
declare(strict_types = 1);
session_start();
require_once 'helpers.php';
if (! check_auth()) {
redirect('login.php');
return;
}
?>
<!doctype html>
<html lang="en">
<head>
<title>Home</title>
</head>
<body>
<div>
<h1>Website Title</h1>
<a href="logout.php">Logout</a>
</div>
<div>
<p>Welcome back, <?= $_SESSION['user_id'] ?>!</p>
</div>
<form action="save.php" method="POST">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="submit" name="submit" value="Save Data">
</form>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</body>
</html>
下面是我的 save.php
文件 -
<?php
declare(strict_types = 1);
session_start();
require_once 'helpers.php';
if (!check_auth())
{
redirect('login.php');
return;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if (!isWriteAccess())
{
echo json_encode(['success' => false, 'message' => 'Write access revoked', ]);
return;
}
// Your code here...
if (isset($_POST['field1']) && isset($_POST['field2']))
{
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
$ret = file_put_contents('mydata.txt', $data, LOCK_EX);
if ($ret === false)
{
die('There was an error writing this file');
}
else
{
echo "$ret bytes written to file";
}
}
else
{
die('no post data to process');
}
}
问题陈述
截至目前,每当我在 index.php
中单击表单上的保存按钮时,它只会在我的浏览器上打印所有内容作为响应,并重定向到 save.php
,但我不想那。我想在弹出窗口 window 上显示所有消息,但它应该保留在同一个 index.php
文件中。
- 如果我能够成功写入文件,那么我应该会看到
some bytes written to the file
作为弹出窗口,但它应该只保留在 index.php
文件中。
- 如果存在写入访问问题,那么它应该显示
Write access revoked
作为弹出窗口,但它应该只保留在 index.php
文件上。
我如何确保无论何时单击 index.php 中表单上的保存按钮,它都应该停留在同一页面上,但仍会进行各种验证并将条目保存在文件中?
您已经连接了 jquery,所以使用 $.ajax() 来提交像 this example or this 这样的表单。在 'success:' 处理程序中,data
变量将包含来自您的 save.php
:
的响应
success: function(data) {
$('#result').html(data);
}
因此您可以使用此结果弹出 window。在你的 index.php:
中有类似的东西
<script>
$(function() {
//This submits a form
$("#idForm").submit(function(e) {
e.preventDefault(); // avoid to execute the actual submit of the form.
var form = $(this);
var url = form.attr('action');
var method = form.attr('method');
$.ajax({
type: method, // "POST"
url: url,
data: form.serialize(), // serializes the form's elements.
success: function(data) {
alert(data); // show response from the php script.
}
});
});
</script>
PS:您可以通过yourself轻松获得您的问题的答案,此类问题在这里得到了很多解答。所有技巧都是在 Google.
中将 site:whosebug.com
添加到您的请求中
我有一个 html 表单,我想在其中使用 php.
将所有字段的所有条目保存到一个文件中- 如果我能够成功保存条目,那么我想弹出消息说
{bytes} bytes written to file
。 - 如果我无法写入成功,那么我想弹出消息说
There was an error writing this file
。 - 如果用户没有写入权限,那么它应该给出弹出消息 -
Write access revoked
。
我从表单操作中调用 save.php
文件以将所有条目保存在一个文件中并添加一些验证。
下面是我的 index.php
文件,里面有表格 -
<?php
declare(strict_types = 1);
session_start();
require_once 'helpers.php';
if (! check_auth()) {
redirect('login.php');
return;
}
?>
<!doctype html>
<html lang="en">
<head>
<title>Home</title>
</head>
<body>
<div>
<h1>Website Title</h1>
<a href="logout.php">Logout</a>
</div>
<div>
<p>Welcome back, <?= $_SESSION['user_id'] ?>!</p>
</div>
<form action="save.php" method="POST">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="submit" name="submit" value="Save Data">
</form>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</body>
</html>
下面是我的 save.php
文件 -
<?php
declare(strict_types = 1);
session_start();
require_once 'helpers.php';
if (!check_auth())
{
redirect('login.php');
return;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if (!isWriteAccess())
{
echo json_encode(['success' => false, 'message' => 'Write access revoked', ]);
return;
}
// Your code here...
if (isset($_POST['field1']) && isset($_POST['field2']))
{
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
$ret = file_put_contents('mydata.txt', $data, LOCK_EX);
if ($ret === false)
{
die('There was an error writing this file');
}
else
{
echo "$ret bytes written to file";
}
}
else
{
die('no post data to process');
}
}
问题陈述
截至目前,每当我在 index.php
中单击表单上的保存按钮时,它只会在我的浏览器上打印所有内容作为响应,并重定向到 save.php
,但我不想那。我想在弹出窗口 window 上显示所有消息,但它应该保留在同一个 index.php
文件中。
- 如果我能够成功写入文件,那么我应该会看到
some bytes written to the file
作为弹出窗口,但它应该只保留在index.php
文件中。 - 如果存在写入访问问题,那么它应该显示
Write access revoked
作为弹出窗口,但它应该只保留在index.php
文件上。
我如何确保无论何时单击 index.php 中表单上的保存按钮,它都应该停留在同一页面上,但仍会进行各种验证并将条目保存在文件中?
您已经连接了 jquery,所以使用 $.ajax() 来提交像 this example or this 这样的表单。在 'success:' 处理程序中,data
变量将包含来自您的 save.php
:
success: function(data) {
$('#result').html(data);
}
因此您可以使用此结果弹出 window。在你的 index.php:
中有类似的东西<script>
$(function() {
//This submits a form
$("#idForm").submit(function(e) {
e.preventDefault(); // avoid to execute the actual submit of the form.
var form = $(this);
var url = form.attr('action');
var method = form.attr('method');
$.ajax({
type: method, // "POST"
url: url,
data: form.serialize(), // serializes the form's elements.
success: function(data) {
alert(data); // show response from the php script.
}
});
});
</script>
PS:您可以通过yourself轻松获得您的问题的答案,此类问题在这里得到了很多解答。所有技巧都是在 Google.
中将site:whosebug.com
添加到您的请求中