Kris
Kris

Reputation: 67

change form action based on button clicked

I am trying to change a form action based on which button someone clicks.

Form

<form action="addline.php" method="post" id="rundataform">
.....
<input value="Select1" type="submit" class="button1" > // button one
<input value="Select2" type="submit" class="button2" > // button two
</form>

I basically want users to click button 1 to run addline.php and button 2 to run addline2.php. I know I need to use JS here and i've tried researching and other scripts but nothing is working.

Can some please give me the code that will work.

Kris

Upvotes: 0

Views: 1170

Answers (2)

Raghavendra
Raghavendra

Reputation: 3580

<form action="addline.php" method="post" id="rundataform">
<script>
function changeAction(val) {
    document.forms[0].action = val;
}
</script>
<input value="Select1" onmouseover="changeAction('addline.php')" type="submit"  class="button1" > // button one
<input value="Select2" onmouseover="changeAction('addline1.php')" type="submit" class="button2" > // button two
</form>

you can do like this. trick for simple solution

<form action="addline.php" method="post" id="rundataform">
<script>
function changeAction(val) {
document.forms[0].action = val;
}
</script>
<input value="Select1" onmouseover="changeAction('addline.php')" type="submit"  class="button1" > // button one
<input value="Select2" onmouseover="changeAction('addline1.php')" type="submit" class="button2" > // button two
</form>

Upvotes: 2

Abhishek
Abhishek

Reputation: 462

You can use ajax for that.

<script type="text/javascript">
        $("#button").click(function(){
            $.ajax({
                type: 'POST',
                url: 'required.php',
                success: function(data) {

                }
            });
        });
</script>

In this you can pass button id and in url tag desired URL of php file where you want to send you contorl.

Upvotes: 0

Related Questions