SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: action variable

Hybrid View

  1. #1
    SitePoint Addict rwar's Avatar
    Join Date
    Sep 2001
    Location
    PF / RS / BR
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    action variable

    How do I set the ACTION variable of a form in javascript, i.e. I have some radios. If a user chooses radio one, the action is set to a script (script1.php), if he chooses radio two, action is set to another script (script2.php) and so on.

    Some help, please?
    php? mysql? apache? That's it.

  2. #2
    SitePoint Addict
    Join Date
    Mar 2001
    Location
    the windy city
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Untested, but I have an idea:
    instead of writing your <form> tag with HTMl write it with javascript:
    <script language="javascript"><!--
    document.write("<form name='frm' method='post' action='" + document.frm.radio_name.value + "'>")
    //-->
    </script>

    Please let me know if it had worked.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2001
    Location
    the windy city
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure the first example will work since you are writing a <form> tag before the frm is read and JS might get confused by the "radio_name.value" thing. So I have thought of another idea.
    write your form in html as usual, but don't specify the action. Then, instead of <input type="submit"> have <input type="botton" value="submit" onClick="subm()">

    then have
    function subm() {
    document.form_name.action = document.form_name.radio_name.value
    document.form_name.submit()
    }

    HTH

  4. #4
    SitePoint Addict
    Join Date
    Mar 2001
    Location
    the windy city
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure the first example will work since you are writing a <form> tag before the frm is read and JS might get confused by the "radio_name.value" thing. So I have thought of another idea.
    write your form in html as usual, but don't specify the action. Then, instead of <input type="submit"> have <input type="botton" value="submit" onClick="subm()">

    then have
    function subm() {
    document.form_name.action = document.form_name.radio_name.value
    document.form_name.submit()
    }

    HTH

  5. #5
    Typo Negative brokenvoice's Avatar
    Join Date
    Dec 2001
    Location
    Scotland
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agreed, you want to set the form.action property. I would do it differently though (and this is untested) by not assigning an action to the form in the form tag and using event handlers to set the action.
    Code:
    <input type="radio" name="blah" value="etc1" onClick="document.formName.action='first_action_URL'">
    <input type="radio" name="blah" value="etc2" onClick="document.formName.action='second_action_URL'">
    You would have to setup some kind of validation to check that one of the radio buttons in the group had been selected, but you'd have to do that anyway for the multiple choice action thing to work with any real usefulness.

  6. #6
    SitePoint Addict rwar's Avatar
    Join Date
    Sep 2001
    Location
    PF / RS / BR
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have used this function, but it didn't work. I'm a newbie to javascript, so I'm asking for help again.

    PHP Code:
    <script language="javascript">
    <!-- 
    function 
    subm()
    {
        
    document.my_form.METHOD "POST";
        
    document.my_form.action document.form_name.radios.value;
        
    document.my_form.submit();
    }
    // end hide -->
    </script>

    ...

    <form name="my_form">
        <input type="radio" name="radios" value="script1.php">script1<br><br>
        <input type="radio" name="radios" value="script2.php">script2<br><br>

        <input type="button" name="submit" value="submit" onClick="subm()"> 
    </form> 
    Thanks.
    php? mysql? apache? That's it.

  7. #7
    SitePoint Addict
    Join Date
    Mar 2001
    Location
    the windy city
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rwar,
    at the first glance I have spotted the following:
    document.my_form.action*=*document.form_name.radios.value
    should actually be
    document.my_form.action*=*document.my_form.radios.value

    since your form's name is "my_form"

  8. #8
    SitePoint Addict rwar's Avatar
    Join Date
    Sep 2001
    Location
    PF / RS / BR
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, valeria_vi. But I'm not understanding the point you want I change the script.

    Could you use the script I write and change it?
    php? mysql? apache? That's it.

  9. #9
    SitePoint Addict rwar's Avatar
    Join Date
    Sep 2001
    Location
    PF / RS / BR
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The bottom bar (Internet Explorer) shows

    JavaScript error: Type 'javascript' into location for details.
    php? mysql? apache? That's it.

  10. #10
    SitePoint Addict
    Join Date
    Mar 2001
    Location
    the windy city
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have an error in this line:
    document.my_form.submit();
    there should be no () after submit i.e. the line should look like this:
    document.my_form.submit;

    also, i wouls keep document.my_form.METHOD to be all lower-case.

    my final code looked like this:

    <HTML>
    <HEAD>
    <TITLE>Untitled</TITLE>
    <script language="javascript"><!--

    function subm() {
    document.my_form.method = "post"
    document.my_form.action = document.my_form.radios.value
    document.my_form.submit
    }
    //-->
    </script>
    </HEAD>

    <BODY>
    <form name="my_form">
    <input type="radio" name="radios" value="script1.php">script1<br><br>
    <input type="radio" name="radios" value="script2.php">script2<br><br>
    <input type="button" name="submit" value="submit" onClick="subm()">
    </form>
    </BODY>
    </HTML>

    and it gave me no errors


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •