1,105,375 Community Members

Change Form action with Submit button?

Member Avatar
veledrom
Practically a Posting Shark
822 posts since Apr 2008
Reputation Points: 32 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
mychalberry
Newbie Poster
3 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
maluisalea
Newbie Poster
2 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
Shanti C
Posting Virtuoso
1,675 posts since Jul 2008
Reputation Points: 106 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 4 [?]
 
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");
}
Member Avatar
nav33n
Purple hazed!
3,894 posts since Nov 2007
Reputation Points: 472 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 20 [?]
Team Colleague
Featured
 
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>
Member Avatar
Shanti C
Posting Virtuoso
1,675 posts since Jul 2008
Reputation Points: 106 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 4 [?]
 
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..

Member Avatar
Shanti C
Posting Virtuoso
1,675 posts since Jul 2008
Reputation Points: 106 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 4 [?]
 
0
 

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

Member Avatar
nav33n
Purple hazed!
3,894 posts since Nov 2007
Reputation Points: 472 [?]
Q&As Helped to Solve: 381 [?]
Skill Endorsements: 20 [?]
Team Colleague
Featured
 
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.

Member Avatar
Shanti C
Posting Virtuoso
1,675 posts since Jul 2008
Reputation Points: 106 [?]
Q&As Helped to Solve: 165 [?]
Skill Endorsements: 4 [?]
 
0
 

k..

Question Answered as of 5 Years Ago by Shanti C, nav33n, mychalberry and 1 other
Member Avatar
veledrom
Practically a Posting Shark
822 posts since Apr 2008
Reputation Points: 32 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Thanks again

Member Avatar
wulawula
Light Poster
25 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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