469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Submit form with variable for action?

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
element.

Here's an example:

Code:

<form action="http://www.mysite.com/admin/index.php" 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">
</form>

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 "bobsite.com" in the domain field,
the action needs to point to "http://www.bobsite.com/admin/index.php".

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.

Best,
Ryan

Sep 25 '05 #1
6 13009
tencip wrote:
<form action="http://www.mysite.com/admin/index.php" 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">
</form>

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.


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">
</form>

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

<?php
$url = sprintf("http://%s/admin/index.php?usrname=%s&pass=%s",
$_REQUEST['domain'], $_REQUEST['usrname'], $_REQUEST['pass']);
header("Location: $url");
?>
<title>Redirect</title>
<h1>Redirect</h1>
<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 ~ http://tobyinkster.co.uk/contact

Sep 25 '05 #2
> Is method="post" set in stone?

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?

Sep 25 '05 #3
tencip wrote:
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.


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

<html>
<body>
<? if (!is_null($_POST['domain'])) { ?>
<form name='second' method='POST' action='<? $_POST['domain'];
?>/test/test2.php'>
<input type='hidden' name='name' value='<? print $_POST['name']; ?>'>
<input type='hidden' name='password' value='<? print
$_POST['password']; ?>'>
<input type='submit'>
</form>
<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'>
</form>
<? } ?>
</body>
</html>
Sep 26 '05 #4
tencip wrote:
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
element.

Here's an example:

Code:

<form action="http://www.mysite.com/admin/index.php" 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">
</form>

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 "bobsite.com" in the domain field,
the action needs to point to "http://www.bobsite.com/admin/index.php".

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.

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"
Onclick="SubmitForm(this.form);">

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

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

--
*****************************
Chuck Anderson Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Sep 26 '05 #5
This works on my computer, should work on yours as well:
just type yahoo.com in the third field:
<html>
<head>
<title>Variable action field</title>
</head>
<body>
<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"/>
</form>
<?php
if (isset($_POST['submit'])){
header("Location: http://www.{$_POST['domain']}");
}
?>
</body>
</html>

Sep 27 '05 #6
Joseph S. wrote:
header("Location: http://www.{$_POST['domain']}");


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

http://www.ietf.org/rfc/rfc2616

--
Jock
Sep 30 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by rjames.clarke | last post: by
5 posts views Thread by Navillus | last post: by
10 posts views Thread by ljlolel | last post: by
13 posts views Thread by deko | last post: by
11 posts views Thread by V S Rawat | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.