Quick question on html forms / url redirection

deicistdeicist Manchester, UK
edited January 2006 in Internet & Media
Righto, I'm working on my very first web app using ruby on rails, and it's all going really well except for this one thing... I'd like a simple form consisting of a text box and a button. The user should enter a number in the box and when the button is clicked it should link to www.mydomain.com/folder/number where number is the number entered in the box. It doesn't need any error checking or anything, just read the number and tag it to the end of the url. I'm sure this should be simple to do but I can't figure it out. Anyone?

Comments

  • EMTEMT Seattle, WA Icrontian
    edited January 2006
    You'll need to use JavaScript to run when they click the button. It has to change the "action" parameter of the form. I'm not 100% that the action would get changed in time though... maybe it needs to run before that.

    I guess it would be safer to just not use a form (is that possible? not sure; at most a dummy form) but only a textbox & button, and have the button's script change your location to a string formed by the desired URL + the text box's contents.
  • deicistdeicist Manchester, UK
    edited January 2006
    does javascript have an 'onchange' event for text boxes? Does javascript even have events? As you can tell, I'm not completely fluent in javascript :D
  • EMTEMT Seattle, WA Icrontian
    edited January 2006
    Well, it does have events. You can write a script for objects when certain events happen to them, like onClick and onFocus I believe. Never heard of an onChange but I'm no expert either.
  • LincLinc Owner Detroit Icrontian
    edited January 2006
    Can we use PHP? If so, I'll give you the code.
  • deicistdeicist Manchester, UK
    edited January 2006
    Nope, no PHP allowed, must be pure HTML or at most javascript... I guess this isn't as simple as I thought :(
  • KyleKyle Lafayette, LA New
    edited January 2006
    This is really easy.

    [PHP]<html>
    <head>
    <script language="Javascript">
    function myFunction()
    {
    document.location.href = "http://www.mydomain.com/folder/&quot; + myForm.myTextBox.value;
    }
    </script>
    </head>
    <body>
    <form name="myForm">
    <input type="text" name="myTextBox" value="">
    <input type="button" name="myButton" value="Submit" onClick="myFunction();">
    </form>
    </body>
    </html>[/PHP]
Sign In or Register to comment.