More than one submit button

I am having problems with more than one sending buttons in HTML and PHP, I tried to code the GUI for the web calculator. It is very simple, but the php function is not so simple. So, I have this simple GUI with 6 submit buttons:

<?php $output= isset($_POST['output']) ? $_POST['output'] : ""; function calc(){ //calculate... } ?> <!DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8" /> <title>Calc</title> <style type="text/css"> .bwidth { width: 100%; } </style> </head> <body> <form action="calc.php"> <h1> <u>MY Own Calculator</u> </h1> <table border="3px" type="box" cellspacing="5px" width="250px"> <tr style="height: 24px;"> <td colspan="3"><input type="text" name="<?php $ausgabe ?>" value="0" style="width: 98%;" /></td> </tr> <tr> <td><input class="bwidth" type="submit" name="1" value="1" /></td> <td><input class="bwidth" type="submit" name="2" value="2" /></td> <td><input class="bwidth" type="submit" name="3" value="3" /></td> </tr> <tr> <td><input class="bwidth" type="submit" name="minus" value="-" /></td> <td><input class="bwidth" type="submit" name="plus" value="+" /></td> <td><input class="bwidth" type="submit" name="enter" value="=" /></td> </tr> </table> </form> </body> 

Now, how can I distinguish these submit buttons? Is it even possible to have more than one submit button? I tried to separate the buttons from the value ... but that didn't work.

And sb has an idea, how can I add a value to an existing value in a text box? So I can press button 1 and it will write 1 in the text box, and when I press button 2, it will add number 2 so that it is "12"?

Thanks for all the suggestions!

+4
source share
7 answers

You can check which button is set using isset($_GET['input_name']) .

Or you could name different inputs:

 <form action="calc.php"> <h1> <u>MY Own Calculator</u> </h1> <table border="3px" type="box" cellspacing="5px" width="250px"> <tr style="height: 24px;"> <td colspan="3"><input type="text" name="<?php $ausgabe ?>" value="0" style="width: 98%;" /></td> </tr> <tr> <td><input class="bwidth" type="submit" name="digit" value="1" /></td> <td><input class="bwidth" type="submit" name="digit" value="2" /></td> <td><input class="bwidth" type="submit" name="digit" value="3" /></td> </tr> <tr> <td><input class="bwidth" type="submit" name="operation" value="-" /></td> <td><input class="bwidth" type="submit" name="operation" value="+" /></td> <td><input class="bwidth" type="submit" name="operation" value="=" /></td> </tr> </table> </form> 

And in your function:

 function calc(){ $op = isset($_GET['operation']) ? $_GET['operation'] : null; switch ($op) { case "+": .... ; break; case "-": .... ; break; case "=": .... ; break; default: "not supported"; } } 
+2
source

In your php

 if(isset($_POST['minus'])) { // selected minus } if(isset($_POST['plus'])) { // selected plus } if(isset($_POST['enter'])) { // selected enter } 

When you click on the button, it will be sent with your data to the server. All message data that you find in the $ _POST array.

If you want to see what is in the $ _POST array, you can run this little code:

 <?php echo '<pre>' . print_r($_POST, true) . '</pre>'; ?> 
+4
source

You can do this using JavaScript, first you will need to change your <form> to something like:

 <form id="form1" name="form1" method="post" action="calc.php"> 

Then the JavaScript and buttons should look like this:

 <script> function submitForm(action) { document.getElementById('form1').action = action; document.getElementById('form1').submit(); } </script> ... <input type="button" onclick="submitForm('page1.php')" value="submit 1" /> <input type="button" onclick="submitForm('page2.php')" value="submit 2" /> 
+3
source

Perhaps you can use the same name for all submit buttons.

 <input type="submit" name="operator" value="+" /> <input type="submit" name="operator" value="-" /> <input type="submit" name="operator" value="=" /> 

and you can get the values ​​in php

 <?php $opr = $_GET['operator']; if($opr == "+") { //do something } else if($opr == "-") { //do something } else if($opr == "=") { //do something } ?> 
+3
source

when submitting a form to calc.php

Inside calc.php check which form element is received.

 if(isset($_REQUEST['minus'])) { // minus is clicked } else if(isset($_REQUEST['plus'])) { // plus is clicked } else if(isset($_REQUEST['enter'])) { // enter is clicked } 

A clicked button will be sent with the form, not with the others ...

Hope this helps ...

+2
source

Yes, you can have n number of submit buttons on the form.

Indicate the action page on the same line as the submit button.

 <form action="actionpage.php"> <input type="submit" value="1"> <input type="submit" value="2" formaction="postPage.php"> </form> 

UPDATED - IF QUESTIONS IN IE

 <form> <input type="submit" value="1" onclick="document.formName.action='abc.php';"> <input type="submit" value="2" onclick="document.formName.action='xyz.php';"> </form> 
+2
source

You can use JavaScript to submit your form. After clicking the button, you call the JS function, which sets up the hidden element with the pressed operation, and then sends the form to the server.

0
source

All Articles