0
  • I have 2 buttons in 1 form
  • in 1st button it can pass values into another page using REQUEST
  • but in 2nd button i want again pass values to another page using REQUEST
  • the problem is when i click the second button the destination is on the destination of 1st button
  • i have read some answers, they say i need to change the action using javascript
  • I've tried their suggestion but it doesn't work for me.

    <form action="first_btn_destination.php"> $data = "data1"; <input type="submit" name="first_btn" id="first_btn"> <input type="submit" name="second_btn" id="second_btn"> </form>

  • how can i change the action using javascript and pass the value.

Paul
  • 59
  • 1
  • 7

2 Answers2

1

It is not possible to do such a thing without javascript. One way to do it is to change action attribute of the form using javascript when you click on a form and submit it manually.

Take a look at here to see how you can prevent your form from submitting on button clicking.

UPDATE:

The following code shows you how to change action using javascript before submitting.

<form id="myForm">
    <input type="submit" name="first_btn" id="first_btn" onclick="modifyAction('http://google.com')">
    <input type="submit" name="second_btn" id="second_btn" onclick="modifyAction('http://yahoo.com')">
</form>
<script>
    var modifyAction = function(action) {
        document.getElementById('myForm').action = action;
    };
</script>
Community
  • 1
  • 1
AliBZ
  • 4,039
  • 12
  • 45
  • 67
0

It is kind of possible without javascript, in that they could visit the first page but never now it... If you can edit the first page, (where they go with first_btn and form action url) and it is a php page, you can test if the value of second_btn is set. Then, if it is set, you could use php header to redirect.

<? 
if($_GET['second_btn'] || $_POST['second_btn']){
    header('Location: http://www.example.com/second_btn_page.php');
    exit;
}?>

This would bring the user to the second page in a way that is invisible to the end user. Note that php's header function needs to be placed high enough in your code such that nothing at all has been output to the browser, or it won't work.

Amos
  • 161
  • 1
  • 8
  • why would you wanna redirect when it is possible to do it without it? – AliBZ May 15 '15 at 17:15
  • I've used a set up like this for a couple of reasons... It's simple since it's one language (so if Paul was familiar / comfortable with php, but not JavaScript, it might provide an alternative that was comfortable to work with). It is quite flexible. If you need to update a data base, or php session variables, that is of course easy. Works without JavaScript, which while not a concern these days, you never know... On balance, probably JavaScript is the way to go in this example, but it's nice to have options and it is something I have used, figured it's worth mentioning? – Amos May 15 '15 at 19:48