PHP 调用 Metodo 并重定向查看

PHP call Metodo and redirect to view

我有一个带有输入类型文本和提交按钮的表单,如下所示:

<form method="post">            
    <input type="submit"value="Emitir Voucher" class="btn btn-success" style="float: right" />
        <div style="overflow: hidden; padding-right: .5em;">
             <input type="text" name="friendName" style="width: 100%;" />
        </div>​
</form>

我需要在单击按钮时将 name=friendName 和更多 3 个参数发送到 metodo,然后在完成 metodo 中的操作后重定向到成功 page.php。 我该怎么做。我已经尝试输入 action="success.php" 但没有用。我可以调用 metodo 并将参数从我的表单发送到 mymetodo 吗?

在表单中使用 action 的方法是正确的。那里给出的文件应该包含您想要 运行 的方法,如下所示:

//success.php:
<?php
method(); //just make method call the first and only statement in your file

method() {
    //Do your stuff here, e. g. access $_POST['name'], etc. But see below because of security!

    //Now, redirect to page.php
    //Make sure there is no output above this line!
    header('Location: page.php');
}
?>

请注意,您应该始终检查用户传递的每个变量,至少使用 FILTER_INPUT。您还可以四处寻找一个框架(如 Symfony、CakePHP 等),它可以让您更轻松地完成所有工作。

编辑(根据评论):

您可以同时发送多个参数。只需在表单中使用多个 <input> 标签。如果您希望用户填写它们,请使用 sth。喜欢 <input type="text" name="userInput">。如果用户不想给这个值,可以使用<input type="hidden" name="hiddenInput" value="hiddenValue">

然后您可以使用 $_POST['userInput']$_POST['hiddenInput']success.php 中访问这些值。 namevalue属性的内容可以随意选择。

此外,您还可以在 URL 中发送 "hidden" 值,从而避免 <input type="hidden" ... 元素。 URL 看起来像这样:success.php?hiddenInput=hiddenValue&otherHiddenInput=otherHiddenValue...。第一个输入必须以 ? 开头,随后的所有输入必须以 & 开头。这将是您在表单中设置为 action 的 URL。

如果您将表单方法设置为 getinput 标签提交的所有值都将以这种方式处理,包括 <input type="hidden" ...。之后,您可以使用 $_GET 而不是 $_POST.

来访问它们

请注意通过 URL (get) 提交的变量。不应以这种方式处理与安全相关的数据。出于安全原因,还请记住 FILTER_INPUT 函数。