Textfield input to link on submit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • terminator10
    New Member
    • Jan 2010
    • 1

    Textfield input to link on submit

    I just want a Textfield and a submit button. When a URL is entered into the Textfield and submit is pressed it goes to the URL. So far I have the following to work with :)

    [code=html]<FORM METHOD="LINK" ACTION="page1.h tm">
    <input type="text" name="thelink" id="thelink" />
    <INPUT TYPE="submit" VALUE="Clickabl e Button">
    </FORM>[/code]

    So how do I make form action become "thelink"?
    Last edited by Atli; Jan 23 '10, 02:02 PM. Reason: Added [code] tags.
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    PHP is server-side, so it takes server requests and creates responses. This means that PHP will not see the data in your form until after the submission. This does not allow time for the action attribute to be modified. You could technically accept the form input to a default action, and then recreate the same request and process it on another URL, but that's fairly complicated.

    I think you should be looking into a JavaScript solution. You're interested in the <form> element and the value of the <input> element. You'll want to set the "action" attribute of the <form> element to the value of the <input> element when the <form> element's "onSubmit" event occurs.

    Comment

    • dgreenhouse
      Recognized Expert Contributor
      • May 2008
      • 250

      #3
      You could do as kovik states... Have a generic receiving script that processes the form, but you'll have to be VERY careful as it's a potential security risk.

      i.e.
      Code:
      -form.php
      <form method="post" action="generic.php">
      <input type="text" name="the_url" />
      <input type="submit" name="submit" value="Go There!" />
      </form>
      
      -generic.php
      <?php
        if (isset($_POST['submit']) && isUrlValid($_POST['the_url'])) {
          header ('Location: ' . $_POST['the_url'] );
          exit;
        }
        // Something was wrong so do something else...
        // ...
        // ...
      
        function isUrlValid($this_url)
        {
          $isValid = false;
      
          // Determine if the url is valid
          // if so, $isValid = true;
      
         return $isValid;
        }
      ?>

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        A minor nit-pick, dgreenhouse...

        You shouldn't use the submit button to test if the form has been submitted. It isn't always passed with the form. Like - in some browsers - when the enter button is pressed instead of clicking the button: the form is submitted but the submit button is not send with the data.

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          Looking at dgreenhouse's post made me realize that I forgot to recommend against this. o.o

          The user could input ANY URL. This means that data from your website may be posted to another. Security risk? I'd say so. Especially if you have any XSS injection vulnerabilities lying around.

          Comment

          Working...