Hi,

I tried to change the form action based on selection option and hoping that my javascript validation function working properly too... I found out one solution and hoping this javascript can do that kind of thing. I tried so many ways and I think there's something wrong with my function since my validation function not working and also the form action. I hope you guys can give some advise on my code. So here's my code for your references :

[JAVASCRIPT] 

    function validate(myForm)
    {   
        with(myForm)
        { 
            ....................
        };

    return true;
    };  

    function onSelectedOption(sel) {
        if ((sel.selectedIndex) == 3) {
            document.getElementById("myForm").action =
            "a.php";
            document.getElementById("myForm").submit();
        }
        else
        {
            document.getElementById("myForm").action =
            "b.php";
            document.getElementById("myForm").submit();
        }
    }

[HTML]
<form name="myForm" action="" method="POST">

<select name="selectA" id="selectA" onchange="onSelectedOption(this);">
<option value="0">- Choose ur selection -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

<input name="cmdSubmit" type="submit" value="Next" onClick="return validate(this)">

Really need your magic for my problem..

It's a silly thing... you're reaching for the form with document.getElementById('myForm'), but your form doesn't have id="myForm".

Just add the id to your form or use document.forms['myForm'] to get it by it's name.

Example:

    function onSelectedOption(sel) {
    var frm = document.forms["myForm"];
        if ((sel.selectedIndex) == 3) {
            frm.action = "a.php";
        }
        else
        {
            frm.action = "b.php"; 
        }
        // I don't think you want to submit the form eah time the combobox is changed, or do you?
        // If you do, just remove the comments.
        //frm.submit();
    }
commented: thanks for your answer. its solved now.. +2

Most obvious problem is you are using getElementById but your form doesn't have an id attribute.
The code for setting the action looks right except it isn't affecting anything because the form can't be found.

commented: thanks for your answer. its solved now.. +2

i just put id="myForm" and seem like its not working too. My validate func also not working , i want it validate first after i click submit button before redirect to another page based on my selection..

It works fine for me with the id added. The validate will never fire as you have it though as you are submitting the form in the onchange event of the select. Meaning you don't get to select 2 or 3 and then click next.
You'll want to call it from inside the onchange if you want to post the form when the user selects an option. Or move the submit to the validate function.

commented: yeah it work fine by the way. there a mistake on my code that make this func not working properly..thanks a lots +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.