是否有可能用PHP读取表单的动作值?

时间:2014-09-03 22:33:44

标签: php forms

我想知道是否有办法从php中的表单标记的action参数中读取或提取值? <form name="..." method="post" action="*this*">

此代码返回表单内每个元素的每个值,但不返回form - 标记本身:

foreach($_POST as $_FORM_value){
    $_FORM_values[] = $_FORM_value;
}

有什么想法吗?

现在我只是使用隐藏字段来执行正确的脚本:

<input type="hidden" name="load-script" value="ScriptName" readonly>

在最顶层,在hta页面开始加载并且任何html被发送到客户端之前,我正在检查是否有任何表格已经提交:

if(isset($_POST['load-script'])){include dir_setup.'post.exec.php';}

post.exec.php

#   get name from submit-button
    foreach($_POST as $_FORM_ELEMENT_name=>$_FORM_ELEMENT_value){
        if (!strncmp($_FORM_ELEMENT_name,'submit_',7)){
            $_FORM_ELEMENT_submit_name=explode('_',$_FORM_ELEMENT_name);
            $_FORM_name=$_FORM_ELEMENT_submit_name[1];
        }
    }
//
#   fetch script
    if(empty($_POST['load-script'])){
        $_SITE_notices[] = '<code><b>load-script</b></code> in <code><b>'.$_FORM_name.'</b></code> is empty.';
    }else{
        $_FORM_SCRIPT=$_POST['load-script'].'.script.php';  //  script to search for.
        if(file_exists(dir_scripts.$_FORM_SCRIPT)){  //  check locally first.
            include dir_scripts.$_FORM_SCRIPT;  //  include script from local folder - if found.
        }else{  //  script was not found locally.
            if($_SITE_FRAMEWORK){
                if(file_exists(ROOT_FRAMEWORK.dir_scripts.$_FORM_SCRIPT)){  //  check framework.
                    include ROOT_FRAMEWORK.dir_scripts.$_FORM_SCRIPT;  //  include script from framework - if found.
                }else{  //  script was not found in either directory; quit searching and throw error.
                    $_SITE_notices[] = 'Could not locate <code><b>'.$_FORM_SCRIPT.'</b></code> in either <code><small>&lt;ROOT&gt;</small><b>/'.dir_scripts.'</b></code> or <code><small>&lt;FRAMEWORK&gt;</small><b>/'.dir_scripts.'</b></code>';
                }
            }else{  //  framework is not activated; quit searching and throw error.
                $_SITE_notices[] = 'Could not locate <code><b>'.$_FORM_SCRIPT.'</b></code> in <code><small>&lt;ROOT&gt;</small><b>/'.dir_scripts.'</b></code>';
            }
        }
    }
//

我的想法是消除对隐藏的load-script元素的需要,只需使用表单操作值来阻止加载哪个脚本。但那可能不可能?

2 个答案:

答案 0 :(得分:1)

action定义要调用的脚本的url。因此,您可以在某些$_SERVER个变量中查看所有操作的详细信息 - 可能$_SERVER['PHP_SELF']$_SERVER['QUERY_STRING']对您有用。

答案 1 :(得分:0)

不,根据其定义,仅提交表单会序列化所讨论的form的所有值,并通过定义的method将这些值发送到服务器。在PHP中,您可以使用以下命令确切地检查回发中的内容:

var_dump($_POST);     // assuming POST method

不会返回HTML中的其他任何数据。

Relevant section in the HTML5 standards to be found here,特别是form data set construction logic