Submit form with variable for action?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tencip

    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.co m" 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

  • Toby Inkster

    #2
    Re: Submit form with variable for action?

    tencip wrote:
    [color=blue]
    > <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.[/color]

    Is method="post" set in stone?

    If not, the easy solution is this:

    <form action="somefil e.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.p hp", but then from
    somefile.php we redirect to the correct location:

    <?php
    $url = sprintf("http://%s/admin/index.php?usrna me=%s&pass=%s",
    $_REQUEST['domain'], $_REQUEST['usrname'], $_REQUEST['pass']);
    header("Locatio n: $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

    Comment

    • tencip

      #3
      Re: Submit form with variable for action?

      > 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?

      Comment

      • rossz

        #4
        Re: Submit form with variable for action?

        tencip wrote:[color=blue]
        > 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.[/color]

        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($_POS T['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="javas cript">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>

        Comment

        • Chuck Anderson

          #5
          Re: Submit form with variable for action?

          tencip wrote:
          [color=blue]
          >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.co m" 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.
          >
          >[/color]
          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="Submit Form(this.form) ;">

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

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

          --
          *************** **************
          Chuck Anderson • Boulder, CO

          Integrity is obvious.
          The lack of it is common.
          *************** **************

          Comment

          • Joseph S.

            #6
            Re: Submit form with variable for action?

            This works on my computer, should work on yours as well:
            just type yahoo.com in the third field:
            <html>
            <head>
            <title>Variab le action field</title>
            </head>
            <body>
            <form action="varacti on.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("Locatio n: http://www.{$_POST['domain']}");
            }
            ?>
            </body>
            </html>

            Comment

            • John Dunlop

              #7
              Re: Submit form with variable for action?

              Joseph S. wrote:
              [color=blue]
              > header("Locatio n: http://www.{$_POST['domain']}");[/color]

              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



              --
              Jock

              Comment

              Working...