1.11M Members

Change Form action with Submit button?

 
0
 

Hi,

I have a form with 3 submit buttons.
This is what i want;
Click on button1, form action becomes 1.php
Click on button2, form action becomes 2.php
Click on button3, form action becomes 3.php

I dont want to enter action manual. How can we do this?

Thanks

 
0
 

You can either use three different forms with a button in each one, use a javascript function when a button is clicked, or use the html dom in conjunction with javascript.

 
0
 

why not try using hidden field where you will set which button you clicked and then call whichever form action you need by testing the hidden field value.

 
0
 

you just give different names to your buttons like:

<input type="submit" name="b1">
<input type="submit" name="b2">
<input type="submit" name="b3">

then at the top of your page:
do like this:

<?
ob_start();
if($_POST['b1'])
{
header("location:one.php");
}
if($_POST['b2'])
{
header("location:two.php");
}
if($_POST['b3'])
{
header("location:three.php");
}
 
0
 

you just give different names to your buttons like:

<input type="submit" name="b1">
<input type="submit" name="b2">
<input type="submit" name="b3">

then at the top of your page:
do like this:

<?
ob_start();
if($_POST['b1'])
{
header("location:one.php");
}
if($_POST['b2'])
{
header("location:two.php");
}
if($_POST['b3'])
{
header("location:three.php");
}

That will just redirect the page once the button is clicked. To post the form data to different scripts depending upon the button clicked, you need to make use of javascript.

<html>
<head>
</head>
<body>
<form method="post" name="form">
<input type="text" name="name" />
<input type="submit" name="submit1" value="submit1" onclick="javascript: form.action='test1.php';" />
<input type="submit" name="submit2" value="submit2" onclick="javascript: form.action='test2.php';"/>
<input type="submit" name="submit3" value="submit3" onclick="javascript: form.action='test3.php';" />
</form>
</body>
</html>
 
0
 

That will just redirect the page once the button is clicked. To post the form data to different scripts depending upon the button clicked, you need to make use of javascript.

ok naveen...
i have understand the thread in that way..

 
0
 

naveen, please tel me the difference between your post and my post....

 
0
 

In your post, it just redirects the user to respective page on button click. It doesn't post the form. In my post, I have specified the action for the form, so it posts the form data to respective script. That is, $_POST will be available in test1.php, test2.php and test3.php on respective button clicks.

 
0
 

k..

Question Answered as of 5 Years Ago by Shanti C, nav33n, maluisalea and 1 other
 
0
 

Thanks guys. I have solved my problem with nav33n's solution. It is exactly what i wanted.

Thanks again

 
0
 

I love nav33n's javascript solution.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article