Connect with Expertise | Find Experts, Get Answers, Share Insights

Textfield input to link on submit

 
Join Date: Jan 2010
Posts: 1
#1: Jan 23 '10
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 :)

Expand|Select|Wrap|Line Numbers
  1. <FORM METHOD="LINK" ACTION="page1.htm">
  2.   <input type="text" name="thelink" id="thelink" />
  3.   <INPUT TYPE="submit" VALUE="Clickable Button">
  4. </FORM>
So how do I make form action become "thelink"?

kovik's Avatar
E
C
 
Join Date: Jun 2007
Location: Baltimore
Posts: 844
#2: Jan 23 '10

re: Textfield input to link on submit


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.
dgreenhouse's Avatar
E
C
 
Join Date: May 2008
Location: San Francisco
Posts: 154
#3: Jan 23 '10

re: Textfield input to link on submit


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.
Expand|Select|Wrap|Line Numbers
  1. -form.php
  2. <form method="post" action="generic.php">
  3. <input type="text" name="the_url" />
  4. <input type="submit" name="submit" value="Go There!" />
  5. </form>
  6.  
  7. -generic.php
  8. <?php
  9.   if (isset($_POST['submit']) && isUrlValid($_POST['the_url'])) {
  10.     header ('Location: ' . $_POST['the_url'] );
  11.     exit;
  12.   }
  13.   // Something was wrong so do something else...
  14.   // ...
  15.   // ...
  16.  
  17.   function isUrlValid($this_url)
  18.   {
  19.     $isValid = false;
  20.  
  21.     // Determine if the url is valid
  22.     // if so, $isValid = true;
  23.  
  24.    return $isValid;
  25.   }
  26. ?>
  27.  
Atli's Avatar
E
M
C
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,618
#4: Jan 23 '10

re: Textfield input to link on submit


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.
kovik's Avatar
E
C
 
Join Date: Jun 2007
Location: Baltimore
Posts: 844
#5: Jan 23 '10

re: Textfield input to link on submit


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