Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one submit button

Tags:

php

button

submit

I'm having trouble with more then one submit Buttons in HTML & PHP, i tried to code a GUI for a web-based calculator. That's realy easy, but the function in php isn't so easy. 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 I can differentiate these many submit buttons? It's even possible to have more than one submit button? I tried it to seperate the buttons by the value... but it didn't work.

And sb has an idea how i can add a value to an existing value in the textfield? So i can push button 1 and it will write 1 in the textfield and when i will push button 2 it will add the number 2 so it will a "12"?

Thanks for all suggestions!

like image 686
Pascal Avatar asked Dec 02 '22 23:12

Pascal


1 Answers

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 a button its going to be send with your post data to the server. All the post data you find in the $_POST array.

If you want to look whats in the $_POST array you can run this small code:

<?php
   echo '<pre>' . print_r($_POST, true) . '</pre>';
?>
like image 98
S.Visser Avatar answered Dec 05 '22 13:12

S.Visser