change form action based on select option

Discussion in 'Javascript' started by Arthur, Jun 15, 2007.

  1. Arthur

    Arthur Guest

    Hello.

    How might it be possible to change where a form action is directed based on
    a selected option.

    For example I have this:

    <FORM METHOD = "post" ACTION = "">

    And a drop down such as

    <Select name="formaction">
    <option value="method1">method1</option>
    <option value="method2">method2</option>
    </select>

    If option 1 is selected I need this to happen

    <FORM METHOD = "post" ACTION = "http://www.thisdomain.com">


    If option 2 is selected I need this to happen

    <FORM METHOD = "post" ACTION = "http://www.thatdomain.com">

    Thanks.
     
    Arthur, Jun 15, 2007
    #1
    1. Advertising

  2. Arthur

    presci Guest

    write a event onchange and check for the selected index and
    document.forms[0].action='blah';
    or
    document.forms[0].action=document.forms[0].selectbox.options[selectbox.selectedIndex].value;


    On Jun 15, 9:02 am, Arthur <> wrote:
    > Hello.
    >
    > How might it be possible to change where a form action is directed based on
    > a selected option.
    >
    > For example I have this:
    >
    > <FORM METHOD = "post" ACTION = "">
    >
    > And a drop down such as
    >
    > <Select name="formaction">
    > <option value="method1">method1</option>
    > <option value="method2">method2</option>
    > </select>
    >
    > If option 1 is selected I need this to happen
    >
    > <FORM METHOD = "post" ACTION = "http://www.thisdomain.com">
    >
    > If option 2 is selected I need this to happen
    >
    > <FORM METHOD = "post" ACTION = "http://www.thatdomain.com">
    >
    > Thanks.
     
    presci, Jun 15, 2007
    #2
    1. Advertising

  3. presci :
    > write a event onchange and check for the selected index and
    > document.forms[0].action='blah';
    > or
    > document.forms[0].action=document.forms[0].selectbox.options[selectbox.selectedIndex].value;
    >
    >
    > On Jun 15, 9:02 am, Arthur <> wrote:
    > > Hello.
    > >
    > > How might it be possible to change where a form action is directed based on
    > > a selected option.
    > >
    > > For example I have this:
    > >
    > > <FORM METHOD = "post" ACTION = "">
    > >
    > > And a drop down such as
    > >
    > > <Select name="formaction">
    > > <option value="method1">method1</option>
    > > <option value="method2">method2</option>
    > > </select>
    > >
    > > If option 1 is selected I need this to happen
    > >
    > > <FORM METHOD = "post" ACTION = "http://www.thisdomain.com">
    > >
    > > If option 2 is selected I need this to happen
    > >
    > > <FORM METHOD = "post" ACTION = "http://www.thatdomain.com">
    > >
    > > Thanks.

    ............................................................................................................................
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <title>
    </title>
    <script type="text/javascript">
    /**
    * I thing that this
    * way is much elegant
    * than document.forms[0].action
    * =document.forms[0].
    * selectbox.options[selectbox.selectedIndex].value;
    * @author Georgi Naumov
    */
    function changeAction(aForm,aValue)
    {
    if(aValue=="")
    return;
    aForm.setAttribute("action",aValue);
    }
    </script>
    </head>
    <body>
    <div>
    <form action="somewhere.php" action="blanc.php">
    <select name="myselect"
    onchange="changeAction(this.form,this.value);">
    <option value="">plese select action</option>
    <option value="one.php">one</option>
    <option value="three.php">three</option>
    </select>
    </form>
    </div>
    </body>
    </html>
     
    Georgi Naumov, Jun 16, 2007
    #3
  4. Arthur

    Randy Webb Guest

    Georgi Naumov said the following on 6/16/2007 5:04 PM:
    > presci :
    >> write a event onchange and check for the selected index and
    >> document.forms[0].action='blah';
    >> or
    >> document.forms[0].action=document.forms[0].selectbox.options[selectbox.selectedIndex].value;
    >>
    >>
    >> On Jun 15, 9:02 am, Arthur <> wrote:
    >>> Hello.
    >>>
    >>> How might it be possible to change where a form action is directed based on
    >>> a selected option.
    >>>
    >>> For example I have this:
    >>>
    >>> <FORM METHOD = "post" ACTION = "">
    >>>
    >>> And a drop down such as
    >>>
    >>> <Select name="formaction">
    >>> <option value="method1">method1</option>
    >>> <option value="method2">method2</option>
    >>> </select>
    >>>
    >>> If option 1 is selected I need this to happen
    >>>
    >>> <FORM METHOD = "post" ACTION = "http://www.thisdomain.com">
    >>>
    >>> If option 2 is selected I need this to happen
    >>>
    >>> <FORM METHOD = "post" ACTION = "http://www.thatdomain.com">
    >>>
    >>> Thanks.


    <snipped lots of XHTML that it takes a person of a particular mental
    capacity to serve on the web>

    > /**
    > * I thing that this
    > * way is much elegant
    > * than document.forms[0].action
    > * =document.forms[0].
    > * selectbox.options[selectbox.selectedIndex].value;
    > * @author Georgi Naumov
    >


    You think wrong.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jun 16, 2007
    #4
  5. Randy Webb :
    > Georgi Naumov said the following on 6/16/2007 5:04 PM:
    > > presci :
    > >> write a event onchange and check for the selected index and
    > >> document.forms[0].action='blah';
    > >> or
    > >> document.forms[0].action=document.forms[0].selectbox.options[selectbox.selectedIndex].value;
    > >>
    > >>
    > >> On Jun 15, 9:02 am, Arthur <> wrote:
    > >>> Hello.
    > >>>
    > >>> How might it be possible to change where a form action is directed based on
    > >>> a selected option.
    > >>>
    > >>> For example I have this:
    > >>>
    > >>> <FORM METHOD = "post" ACTION = "">
    > >>>
    > >>> And a drop down such as
    > >>>
    > >>> <Select name="formaction">
    > >>> <option value="method1">method1</option>
    > >>> <option value="method2">method2</option>
    > >>> </select>
    > >>>
    > >>> If option 1 is selected I need this to happen
    > >>>
    > >>> <FORM METHOD = "post" ACTION = "http://www.thisdomain.com">
    > >>>
    > >>> If option 2 is selected I need this to happen
    > >>>
    > >>> <FORM METHOD = "post" ACTION = "http://www.thatdomain.com">
    > >>>
    > >>> Thanks.

    >
    > <snipped lots of XHTML that it takes a person of a particular mental
    > capacity to serve on the web>
    >
    > > /**
    > > * I thing that this
    > > * way is much elegant
    > > * than document.forms[0].action
    > > * =document.forms[0].
    > > * selectbox.options[selectbox.selectedIndex].value;
    > > * @author Georgi Naumov
    > >

    >
    > You think wrong.

    .....................................................................................
    Why ? This:
    aForm.setAttribute("action",aValue);
    is shorter than:
    document.forms[0].selectbox.options[selectbox.selectedIndex].value;
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    > Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Georgi Naumov, Jun 17, 2007
    #5
  6. Arthur

    -Lost Guest

    Georgi Naumov wrote:
    > Randy Webb :
    >> You think wrong.


    > Why ? This:
    > aForm.setAttribute("action",aValue);
    > is shorter than:
    > document.forms[0].selectbox.options[selectbox.selectedIndex].value;


    You are kidding me right? I'll ask you a question... which one of those
    methods is cross-browser?

    Or how about, which one of those methods will fail in a certain
    mainstream browser?

    --
    -Lost
    Remove the extra words to reply by e-mail. Don't e-mail me. I am
    kidding. No I am not.
     
    -Lost, Jun 17, 2007
    #6
  7. Arthur

    Randy Webb Guest

    Georgi Naumov said the following on 6/17/2007 3:07 AM:
    > Randy Webb :
    >> Georgi Naumov said the following on 6/16/2007 5:04 PM:
    >>> presci :
    >>>> write a event onchange and check for the selected index and
    >>>> document.forms[0].action='blah';
    >>>> or
    >>>> document.forms[0].action=document.forms[0].selectbox.options[selectbox.selectedIndex].value;
    >>>>
    >>>>
    >>>> On Jun 15, 9:02 am, Arthur <> wrote:
    >>>>> Hello.
    >>>>>
    >>>>> How might it be possible to change where a form action is directed based on
    >>>>> a selected option.
    >>>>>
    >>>>> For example I have this:
    >>>>>
    >>>>> <FORM METHOD = "post" ACTION = "">
    >>>>>
    >>>>> And a drop down such as
    >>>>>
    >>>>> <Select name="formaction">
    >>>>> <option value="method1">method1</option>
    >>>>> <option value="method2">method2</option>
    >>>>> </select>
    >>>>>
    >>>>> If option 1 is selected I need this to happen
    >>>>>
    >>>>> <FORM METHOD = "post" ACTION = "http://www.thisdomain.com">
    >>>>>
    >>>>> If option 2 is selected I need this to happen
    >>>>>
    >>>>> <FORM METHOD = "post" ACTION = "http://www.thatdomain.com">
    >>>>>
    >>>>> Thanks.

    >> <snipped lots of XHTML that it takes a person of a particular mental
    >> capacity to serve on the web>
    >>
    >>> /**
    >>> * I thing that this
    >>> * way is much elegant
    >>> * than document.forms[0].action
    >>> * =document.forms[0].
    >>> * selectbox.options[selectbox.selectedIndex].value;
    >>> * @author Georgi Naumov
    >>>

    >> You think wrong.

    > ....................................................................................
    > Why ?


    setAttribute is known to be as buggy as a termite infested house in a
    particular browser produced by a company in Redmond Washington, USA.

    > This:
    > aForm.setAttribute("action",aValue);
    > is shorter than:
    > document.forms[0].selectbox.options[selectbox.selectedIndex].value;


    Shorter code isn't always better code.

    onchange="this.form.action=this.value"

    How much shorter, simpler, of a solution do you want for this question?
    And I won't even get into the aspects of trying to write XHTML for the web.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jun 17, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Bloggs
    Replies:
    1
    Views:
    840
    Sudsy
    Aug 3, 2003
  2. J. Hall
    Replies:
    2
    Views:
    1,075
    Jeff Thies
    Apr 21, 2004
  3. John
    Replies:
    0
    Views:
    918
  4. WebRaster
    Replies:
    1
    Views:
    272
    Francisco Spaeth
    Oct 26, 2005
  5. Replies:
    7
    Views:
    261
Loading...

Share This Page