【问题标题】:HTML POST form submit is mangling URLHTML POST 表单提交正在破坏 URL
【发布时间】:2011-11-06 02:46:03
【问题描述】:

我正在尝试让 HTML/PHP 表单正确提交。一些细节:

基本网址 = http://localhost/directory

页面 = page/add

完整地址 = http://localhost/directory/page/add

使用htaccess重写url,所以http://localhost/directory/page/add其实就是http://localhost/directory/index.php?q=page/add

我的 HTML POST 操作是“页面/添加”,以便前端控制器知道要触发哪个函数来清理和提交数据(它充当“表单 ID”)。

页面在http://localhost/directory/page/add 处加载正常,但是当我单击提交按钮时,URL 被修改为页面/页面/添加。每次我按“提交”时,都会在网址中添加另一个“页面”。所以点击 5 次会得到“page/page/page/page/page/page/add”

我似乎无法找到为什么我会得到那个“额外的”“页面”。

实际的 PHP 错误(page/page/add 在 $routes 中不存在,因为它不是有效的路由):

注意:未定义的索引:第 92 行 C:\xampp\htdocs\script\includes\common.inc 中的页面/页面/添加

这是第 92 行的函数:

function route_path($path = NULL) {

    $routes = get_routes(); //Returns array: approved "urls => function callbacks"

    if($path === NULL) {
        $path = get_path(); //Returns $_GET['q'] with trim and strip_tags
    }

    $function = $routes[$path]; <<<<<----This is LINE 92 

    if(isset($function)) {
    $form_name = str_replace('/', '_', $path); // page/add = function page_add()
    }

    if(function_exists($function)) {
        call_user_func($function, $form_name);
    }
    else {
        //TODO: Redirect to Login screen.
    }
}

基本的 HTML 是:

<form action="page/add" method="post" />

//Form elements

<input type="submit" value="Submit" />
</form>

感谢您的帮助。

更新:我所做的是将&lt;base&gt; 标记添加到我的HTML 模板中。这使我可以将操作保留为页面/添加(因为它也是我的简单路由器/调度程序中的路由)。

【问题讨论】:

  • 表格的网址是什么?你知道page/add 是相对于当前的吗?如果您的表单的 URL 以 / 结尾或不以 / 结尾,这将有所不同。

标签: php html forms


【解决方案1】:

通过使用相对路径,您是在告诉表单以现有路径以及您的操作提交。因此,如果您位于 http://example.com/page/add,则表单使用 http://example.com/page/ 作为基础,并将操作 page/add 添加到 http://example.com/page/page/add 的 POST 中。

您仍然可以使用相对路径,只需相应地更改操作:

<form action="add" method="post" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2011-08-02
    • 2012-11-05
    • 2011-10-14
    • 1970-01-01
    • 2011-03-15
    • 2019-08-31
    相关资源
    最近更新 更多