我可以在同一文件中仅通过 php 提交表单并将记录添加到数据库而不使用 Get/Post 方法吗?

发布于 2024-10-15 00:48:19 字数 241 浏览 3 评论 0原文

假设我已经创建了一个注册表。现在,要将记录添加到数据库中,我们通过 POST 方法将数据发送到另一个 php 文件,在其中进行一些验证并添加记录。是否可以在同一个文件中执行此操作,而无需通过 POST/GET 发送和获取数据?如果不是,那为什么?

编辑:即使发送到同一个 php 文件也会发送并丢失资源。我问这个问题是因为我想避免通过 GET/POST 发送和通过相同的 Get/POST 获取所浪费的时间。如果不可能,我想了解为什么 PHP 不允许。

Say I have create a registration form. Now to add records into a DB, we send the data to another php file by POST method, where we do some validations and add a record. Is it possible to do it in the same file without sending and getting the data by POST/GET? If no, then why?

EDIT: Even sending to the same php file is SENDING and losing resource. I ask this question because I want to avoid the lost of time on sending by GET/POST and getting by the same Get/POST. And if it is not posible, I want to understand why PHP does not allow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

满天都是小星星 2024-10-22 00:48:19

不可以。您始终必须将数据从客户端发送到服务器,这是没有办法解决的。

如果您不想重新加载用户所在的整个页面,您可以通过 AJAX 将数据提交到负责处理数据并添加数据的 php 文件。这样用户就永远不会离开页面。

No. You always have to send data from the client to the server, there is no way around that.

If you dont want to reload the entire page the user is on, you could submit the data via AJAX to the php file responsible for processing it and adding the data. That way the user never leaves the page.

影子是时光的心 2024-10-22 00:48:19

是的当然。

只需在您的表单“action”中放入

$_SERVER['PHP_SELF']

PHP 页面的开头,检查 $_POST 是否已设置,

if(isset($_POST))
{
   // actions to be taken after form submission
}

当然您可以添加一个隐藏的输入标记来细化 $_POST 的检查。例如在你的表格中

<input type="hidden" name="formSubmit" value="yes" />

那么你的支票应该是这样的

if(isset($_POST['formSubmit']))
{
       // actions to be taken after form submission
}

yes ofcourse.

just in your form "action" put

$_SERVER['PHP_SELF']

then in the beginning of your PHP page check if the $_POST is set or not

if(isset($_POST))
{
   // actions to be taken after form submission
}

ofcourse you can add a hidden input tag for refining checks for the $_POST. eg in your form

<input type="hidden" name="formSubmit" value="yes" />

then your check should be like

if(isset($_POST['formSubmit']))
{
       // actions to be taken after form submission
}
转角预定愛 2024-10-22 00:48:19

这是有可能的。例如:

<?php
if(true === isset($_POST['submit']) // check if submit-button was clicked
{
    // do some validation here...

    // If validation successes add record into db here...
}
else // no post data sent so output the form
{
    // output the form here...
}

It's possible. For example:

<?php
if(true === isset($_POST['submit']) // check if submit-button was clicked
{
    // do some validation here...

    // If validation successes add record into db here...
}
else // no post data sent so output the form
{
    // output the form here...
}
岁月静好 2024-10-22 00:48:19

是的,可以设置

action="同一页面"

表单标签中的 。
您可以在同一页面上访问所有表单属性。

Yes it is possible set

action="same page"

in form tag.
you can access your all form attributes on same page.

柠栀 2024-10-22 00:48:19

是的,这很容易。该表单可以回传到其自身。这是最容易完成的,甚至不需要在表单标记中指定操作的值。

<form method='POST'>

然后在页面顶部添加任何内容之前,添加一个 if 语句来检查表单是否已提交。

if (isset ($_POST['post'])) { // 'post' is the name of the submit button
    $error = false;
    // Do validation

从那里进行验证并根据结果采取行动。
如果您需要进行大量验证,也许可以将其放入另一个文件中并包含它。

    include "formValidation.php";

如果一切顺利并且所有测试都通过,则使用

    if ($error === false) {
        Header ("Location: confirmation.php"); 
        exit;
    }
}

如果测试失败,则停留在保留所有发布数据的页面上,并显示错误。

if (isset ($error) && !empty ($error)) {
    echo "<div class='error'>$error</div>";
}

Yes it is easy. The form can post back to its self. This is most easily done by not even specifying the value of action in the form tag.

<form method='POST'>

Then at the top of the page before any content is put on the page, include an if statement to check if the form was submitted.

if (isset ($_POST['post'])) { // 'post' is the name of the submit button
    $error = false;
    // Do validation

From there do validation and act according to the result.
If you have lots of validation to do, perhaps put that in another file and include it.

    include "formValidation.php";

If all is well and all tests are passed use

    if ($error === false) {
        Header ("Location: confirmation.php"); 
        exit;
    }
}

If tests fail, stay on the page keeping all the post data, and display an error.

if (isset ($error) && !empty ($error)) {
    echo "<div class='error'>$error</div>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文