2 Replies - 423 Views - Last Post: 10 March 2011 - 07:36 AM

#1 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Buttons Separate From Forms

Posted 10 March 2011 - 04:42 AM

Ok well I have a site here: http://www.school.un...roundcoding.com
I have to buttons but I had to make a form for each of them. Is there a way I could put two buttons on one form BUT have them post have different PHP files as actions when one was clicked. Like the destroy cookies button would go to dest.php and the sumbit button would go to login.php.

I have an awesome IDE called PHPEdit and it let me look through the attributes but I couldn't find out how to make a separate action for a button. Does anybody know how without having multiple forms?

Is This A Good Question/Topic? 0
  • +

Replies To: Buttons Separate From Forms

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Buttons Separate From Forms

Posted 10 March 2011 - 05:22 AM

Hey.

Each form only has one action attribute, so it can only go to a single file.

However, you can use Javascript to alter the attribute before sending. For example, with a form like this:
<form id="MyForm" name="MyForm" action="login.php">
    <!-- Some INPUT fields here -->

    <input id="LoginSubmit" type="submit" value="Login"> 
    <input id="DestroySubmit" type="submit" value="Destroy Cookies">
</form>


You could use something like this:
window.onload = function() {
    // Get a reference to the destroy button
    var destBtn = document.getElementById('DestroySubmit');

    // Attach a function to the "click" event of the button, so it will be
    // executed whenever the button is clicked.
    destBtn.onclick = function() {
         // Change the "action" attribute of the form named MyForm.
         document.forms['MyForm'].setAttribute('action', 'destr.php');
    }

    // Do the same thing to the login, button. Even though the "login.php" action
    // is the default action, it is a good idea to have the login button set
    // the action to. If, for example, the user presses back after clicking
    // the "Destroy Cookies" button, the action will remain "destr.php" like
    // it was just before the redirect. This resets that.
    var loginBtn = document.getElementById('LoginSubmit');
    loginBtn.onclick = function() {
         document.forms['MyForm'].setAttribute('action', 'login.php');
    }
}



Note that this assumes the client browser has Javascript enabled. If you don't want to depend on that, you may well be better of designing the PHP code you are sending the form to in such a way that you can send both actions to the same page and have PHP work it out.
Was This Post Helpful? 1
  • +
  • -

#3 enjoibp3  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 48
  • Joined: 02-March 11

Re: Buttons Separate From Forms

Posted 10 March 2011 - 07:36 AM

Alti's way works but you could also name your submit buttons different things then listen for them in php. So your two submit buttons would have different names on them say one is
<input type="text" name="submit" value="Submit" />


and the other is
<input type="text" name="destroy" value="Destroy Cookies" />



set your form action to
<?php $_SERVER['PHP_SELF']; ?>



Then simply listen to see if either submit button is pressed :)

if(isset($_POST['destroy'])){
    echo "Destroy Cookies!";
}
if(isset($_POST['submit'])){
    echo "Submit Form!";
}



Have Fun :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1