Submit form with variable for action?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi everyone,

So, i've got a form that is very simple. It hold three elements:
username, password, and domain. The goal here is to have the form
submit the login to an administrative section depending on which domain
someone has chosen.

For instance, let's say we have three administrative sites, that all
have different URLs, but we want this one form to handle logging into
any of them. So, the form itself needs to have a dynamic action

Here's an example:


<form action=" " method="post"
name="login" id="login" >
    <input name="usrname" type="text">
    <input name="pass" type="password">
        <input name="domain" type="text">
    <input name="submit" type="submit" value="Login">

Now, the issue is, instead of the action I have now, I need the action
to change based on what is typed by the user in the domain field. So in
the case above, if the user types "" in the domain field,
the action needs to point to " ".

I have looked near and far for a solution for this. Using PHP, so have
some flexibility, and javascript is okay if we have to use it.


Re: Submit form with variable for action?

tencip wrote:

Quoted text here. Click to load it

Is method="post" set in stone?

If not, the easy solution is this:

<form action="somefile.php" method="get">
    <input name="usrname">
    <input name="pass" type="password">
        <input name="domain">
    <input type="submit" value="Login">

The form will *always* submit to "somefile.php", but then from
somefile.php we redirect to the correct location:

  $url = sprintf("http://%s/admin/index.php?usrname=%s&pass=%s",
             $_REQUEST['domain'], $_REQUEST['usrname'], $_REQUEST['pass']);
  header("Location: $url");
<p><a href="<?=$url?>"><?=$url?></a></p>

Of course, if you use GET rather than POST you have the problem of the
password being part of the URL, in plain sight.

Toby A Inkster BSc (Hons) ARCS
Contact Me  ~

Re: Submit form with variable for action?

Quoted text here. Click to load it

Unfortunately, yes, it's set in stone.  So while your example would
have worked great if this were a GET, it's a POST. =(

Any other ideas?

Re: Submit form with variable for action?

tencip wrote:
Quoted text here. Click to load it

Ok, this is an ugly hack that uses javascript.  I would never use it
because there is no guarantee javascript is enabled.

<? if (!is_null($_POST['domain'])) { ?>
 <form name='second' method='POST' action='<? $_POST['domain'];
  <input type='hidden' name='name' value='<? print $_POST['name']; ?>'>
  <input type='hidden' name='password' value='<? print
$_POST['password']; ?>'>
  <input type='submit'>
 <script language="javascript">document.forms[0].submit()</script>
<? } else { ?>
 <form method='POST'>
  domain: <input type='text' name='domain'><br>
  Name: <input type='text' name='name'><br>
  Password: <input type='text' name='password'><br>
  <input type='submit'>
<? } ?>

Re: Submit form with variable for action?

tencip wrote:

Quoted text here. Click to load it
It's quite simple in JavaScript.

First, remove the action from the form definition
<form method="post" name="login" id="login" >

Then, instead of using type=submit, use type=button.
<input type=button name=submit value="Login"  

function SubmitForm(form)
if (form.domain.value == "xxx")
form.action = "xxx"
form.action = "zzz";

form.submit(); // Submit the page
return true;

 Chuck Anderson • Boulder, CO
 Integrity is obvious.
 The lack of it is common.

Re: Submit form with variable for action?

This works on my computer, should work on yours as well:
just type in the third field:
<title>Variable action field</title>
<form action="varaction.php" method="post" name="login" id="login">
        Username:<input name="usrname" type="text"/>
        Password:<input name="pass" type="password"/>
        Domain: http://www.<input name="domain" type="text"/>
        <input name="submit" type="submit" value="Login"/>
    if (isset($_POST['submit'])){
        header("Location: http://www.");

Re: Submit form with variable for action?

Joseph S. wrote:

Quoted text here. Click to load it

In the absence of a different status code, that will send a 302 (temporary
redirect).  But the HTTP1.1 spec warns that some browsers treat 302
responses as if they were 303s, meaning that they issue a GET request no
matter what the first method was, be it HEAD, POST, whatever.  Any
information POSTed the first time around wouldn't be sent again.

Your solution suggests that not all browsers treat 302s like that, that
some stick with POST.

To disambiguate this, in theory at least, HTTP1.1 provides the 303 and 307
status codes.  303 is for when user agents should perform a GET on the
Location URI, 307 for when they should continue using the same method as
before (possibly involving user interaction).  Some browsers don't grok
these though.  More details at


Site Timeline