Asked  7 Months ago    Answers:  5   Viewed   35 times

I have a website started where I want to have 2 separate submit buttons, one of which will take data entered and do some calculations to it to display on the same screen. I've got this successfully working with:

<form id="form1" name="form1" method="post" onsubmit="" onreset="" action="programname.php">
<input type="submit" name="calc" value="Find Angle">

and then I use:

if (!isset($_POST['submit'])){
Do actions, display calculations}

Now I want a second submit button that still grabs the data they entered but then goes to a different address. Is there an elegant way to do this?

 Answers

56

You could add an onclick method to the new submit button that will change the action of the form and then submit it.

<script type="text/javascript">
  function submitForm(action) {
    var form = document.getElementById('form1');
    form.action = action;
    form.submit();
  }
</script>

...

<form id="form1">
  <!-- ... -->
  <input type="button" onclick="submitForm('page1.php')" value="submit 1" />
  <input type="button" onclick="submitForm('page2.php')" value="submit 2" />
</form>
Wednesday, March 31, 2021
 
innovation
answered 7 Months ago
39

Updated: Use call_user_func($function_name, (optional) $parametrs) + magic __call method. It is exactly the same as in Yii.

public function __call($name,$args) {
   if (method_exists($this,$name)) {
      $this->beforeAction();
      $ret =  call_user_func_array(array($this, $name), $args);
      $this->afterAction();
      return $ret;
   }
}

Now you can simple call $object->a3(). You can have function named a3 in the class, but add private modifier to it.

Wednesday, March 31, 2021
 
Packy
answered 7 Months ago
48

You forget signle quote and ) in the_name input.

<input type="text" name="the_input" value="<?php if (isset($_POST['the_input'])) { echo $_POST['the_input'] ; } ?>">

and for getting form button pressed value you need to append it value manually to serialize.

$form.serialize() + "&submit="+ $('button').attr("value")

Example

<script type="text/javascript">
    $(function()
    {
        $('button[type="submit"]').on('click',function(e)
        {
            e.preventDefault();
            var submit_value = $(this).val();
            jQuery.post
            (
                $(this).attr('action'),
                $(this).serialize()+ "&submit="+ submit_value,
                function(data)
                {
                    $('#result').empty().append(data).slideDown();
                }
            );
        });
    });
</script>

Complete Tested Code

<?php
    if(isset($_POST['the_input']))
    {
        $input = $_POST['the_input'];
        $eating = $_POST['eat_something'];
        exit;
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>StackOverFlow</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
        $(function()
        {
            $('button[type="submit"]').on('click',function(e)
            {
                e.preventDefault();
                var submit_value = $(this).val();
                jQuery.post
                (
                    $('#the-form').attr('action'),
                    $('#the-form').serialize()+ "&eat_something="+ submit_value,
                    function(data)
                    {
                        $('#result').empty().append(data).slideDown();
                    }
                );
            });
        });
    </script>
</head>
<body>
    <form method="post" action="random.php" id="the-form">
        <input type="text" name="the_input" value="<?php if (isset($_POST['the_input'])) { echo $_POST['the_input'] ; } ?>">
        <button type="submit" name="eat_something" value="TRUE">Eating</button>
        <button type="submit" name="eat_something" value="FALSE">Don't Eat</button>
    </form>
    <textarea id="result"></textarea>
</body>
</html>
Saturday, May 29, 2021
 
kmunky
answered 5 Months ago
57

I would suggest you just name the input elements as an array. something like:

<input type="text" name="fruits[company1][apple]">
<input type="text" name="fruits[company1][berries]">
<input type="text" name="fruits[company1][orange]">
<input type="text" name="fruits[company1][bannana]">

<input type="text" name="fruits[company2][apple]">
<input type="text" name="fruits[company2][berries]">
<input type="text" name="fruits[company2][orange]">
<input type="text" name="fruits[company2][bannana]">

or the same thing with the fruit being the first level and company name being second. It is really the same thing and generally just as easy to use either one. Just depends on how you want to loop over the data once you post the form. You might be better off also using ids for the company name and/or the fruit. Just makes it so, for example, company names with a space are still valid.

Using the above form, you can process the data with something like this:

<?php
foreach($_POST['fruits'] as $company=>$row){
    foreach($row as $fruit=>$quantity){
        if(!is_numeric($quantity) || $quantity < 0){
            $quantity = 0;
        }
        echo "You selected {$quantity} {$fruit} from {$company}";
    }
}
Saturday, May 29, 2021
 
Anand
answered 5 Months ago
96

Refer this :

Multiple submit buttons php different actions

Put this script in your page :

<script>
    function submitForm(action)
    {
        document.getElementById('columnarForm').action = action;
        document.getElementById('columnarForm').submit();
    }
</script>

Modify your input code :

<input type="image" name="camper" onclick="submitForm('formindb_hoh_1.php')" value="camper" src="../images/apps/camperBtn.png" class="submit_button" />
<input type="image" name="medical" onclick="submitForm('formindb_hoh_2.php')" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" />
Wednesday, June 30, 2021
 
njai
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :