简体   繁体   中英

is it possible to read the action value of a form with php?

I'm wondering if there's a way to read, or extract, the value from the action parameter of a form tag in php? <form name="..." method="post" action="*this*">

This code return each value of each element inside the form, but not from the form -tag itself:

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

Any ideas?

Now I'm just using a hidden field to execute the correct script:

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

And at the very top, before hta page is starting to load and any html is sent to the client, I'm checking to se if any form has been submittet:

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

And inside 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>';
            }
        }
    }
//

My idea was to eliminate the need for the hidden load-script element, and just use the form action value to determen which script to load. But that might not be possible?

The action defines the url of the script to be called. Therefore You can see all action's details in some $_SERVER variables - maybe $_SERVER['PHP_SELF'] and $_SERVER['QUERY_STRING'] will be useful for You.

No, by its very definition submitting a form only serializes all the values of the form in question and sends those to the server by the defined method . In PHP you can check exactly what is in a postback with:

var_dump($_POST);     // assuming POST method

No other data from your HTML is returned whatsoever.

Relevant section in the HTML5 standards to be found here , specifically the form data set construction logic .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM