开发者

multiple buttons on a form

I have a clear button that I want to tie into some php codi开发者_如何学JAVAng. how do I detect if the clear button is pressed. When the user press clear, i'm going to have it update a sql table to clear out entries. but first I need to know when the button is pressed.

<input name="Reset1" type="reset" value="clear" />


You check the post or get data from your form, using the name of the button:

<form action='' method='post'>
  <button type='submit' name='reset'>Clear</button>
  <button type='submit' name='submit'>Submit</button>
</form>

PHP (after submission):

if(isset($_POST['reset'])) { /* ...clear and reset stuff... */ }
else if(isset($_POST['submit']) { /* ...submit stuff... */ }

Alternatively, you have two buttons with the same name, which both submit your form, and if/else their values:

<form action='' method='post'>
  <button name='submit' value='0'>Clear</button>
  <button name='submit' value='1'>Submit</button>
  <button name='submit' value='2'>Something Else</button>
</form>

PHP (after submission):

if($_POST['submit']==0)      { /* ...clear and reset stuff... */ }
else if($_POST['submit']==1) { /* ...submit stuff... */ }
else if($_POST['submit']==2) { /* ...do something else... */ }


Solution from: http://websistent.com/multiple-submit-buttons-in-php/

html:

<input type="submit" name="btn_submit" value="Button 1" />
<input type="submit" name="btn_submit" value="Button 2" />
<input type="submit" name="btn_submit" value="Button 3" />

php:

<?php
if($_REQUEST['btn_submit']=="Button 1")
{
print "You pressed Button 1";
}
else if($_REQUEST['btn_submit']=="Button 2")
{
print "You pressed Button 2";
}
else if($_REQUEST['btn_submit']=="Button 3")
{
print "You pressed Button 3";
}
?>


Either wire up the item as a button you attach to a Javascript snippet that fires off an AJAX request, or use a submit input. (Determine whether the form was submitted or reset based upon the submit value)


<form action="" method="post" >

    <input type="submit" name="btn_submit" value="Button 1" />
    <input type="submit" name="btn_submit" value="Button 2" />
    <input type="submit" name="btn_submit" value="Button 3" />
<form>

The variables in $_REQUEST are provided to the script via the GET, POST, and COOKIE ,you can use $_POST instead as the form's method is POST

<?php

    if (isset($_REQUEST['btn_submit'])) {

        switch ($_REQUEST['btn_submit']) {
           case "Button 1":
                 echo "You pressed Button 1";
                 break;
           case "Button 2":
                 print "You pressed Button 2";
                 break;
           case "Button 3":
                 print "You pressed Button 3";
                 break;
        }
    }
?>


You can bind an ajax event to it being clicked. If you're using jQuery, you can bind the ajax() function to its click event. The ajax function allows for easy passing of variables, error checking, etc.

You'll ajax call a php file, let's call it reset_ajax.php, which will make the query.

The other option is to keep track of the reset button being clicked with a hidden input that's updated every time its clicked, and then you can make the query on the backend upon form submission.

Up to you which method you want to use. The first one has the advantage of working even if they don't submit the form.


surprisingly, the formaction attribute was not covered. you can use it like so

<form method="post" action="/action/project/edit">
    ...
    <button type="submit">edit</button>
    <button type="submit" formaction="/action/project/delete">delete</button>
</form>

of course, this does not use javascript and has good browser support. this route (no pun intended) has the added benefit of not requiring extra if / then statements on the server since the same route won't be used for two different things

reference https://css-tricks.com/separate-form-submit-buttons-go-different-urls/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜