954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Change Form action with Submit button?

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

veledrom
Master Poster
758 posts since Apr 2008
Reputation Points: 42
Solved Threads: 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.

mychalberry
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 2
 

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.

maluisalea
Newbie Poster
2 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
 

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");
}
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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>
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
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..

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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['name'] will be available in test1.php, test2.php and test3.php on respective button clicks.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

k..

Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

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

Thanks again

veledrom
Master Poster
758 posts since Apr 2008
Reputation Points: 42
Solved Threads: 0
 

I love nav33n's javascript solution.

wulawula
Light Poster
25 posts since Jul 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You