Send value of submit button when form gets posted

2019-01-04 02:59发布

I have a list of names and some buttons with product names. When one of the buttons is clicked the information of the list is sent to a PHP script, but I can't hit the submit button to send its value. How is it done? I boiled my code down to the following:

The sending page:

<html>
<form action="buy.php" method="post">
    <select name="name">
        <option>John</option>
        <option>Henry</option>
    <select>
    <input id='submit' type='submit' name = 'Tea'    value = 'Tea'>
    <input id='submit' type='submit' name = 'Coffee' value = 'Coffee'>
</form>
</html>

The receiving page: buy.php

<?php
    $name = $_POST['name'];
    $purchase = $_POST['submit'];
    //here some database magic happens
?>

Everything except sending the submit button value works flawlessly.

7条回答
淡お忘
2楼-- · 2019-01-04 03:43

Like the others said, you probably missunderstood the idea of a unique id. All I have to add is, that I do not like the idea of using "value" as the identifying property here, as it may change over time (i.e. if you want to provide multiple languages).

<input id='submit_tea'    type='submit' name = 'submit_tea'    value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />

and in your php script

if( array_key_exists( 'submit_tea', $_POST ) )
{
  // handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
  // handle coffee
}

Additionally, you can add something like if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] ) if you want to check if data was acctually posted.

查看更多
劫难
3楼-- · 2019-01-04 03:49

To start, using the same ID twice is not a good idea. ID's should be unique, if you need to style elements you should use a class to apply CSS instead.

At last, you defined the name of your submit button as Tea and Coffee, but in your PHP you are using submit as index. your index should have been $_POST['Tea'] for example. that would require you to check for it being set as it only sends one , you can do that with isset().

Buy anyway , user4035 just beat me to it , his code will "fix" this for you.

查看更多
Viruses.
4楼-- · 2019-01-04 03:50

The button names are not submit, so the php $_POST['submit'] value is not set. As in isset($_POST['submit']) evaluates to false.

<html>
<form action="" method="post">
    <input type="hidden" name="action" value="submit" />
    <select name="name">
        <option>John</option>
        <option>Henry</option>
    <select>
<!-- 
make sure all html elements that have an ID are unique and name the buttons submit 
-->
    <input id="tea-submit" type="submit" name="submit" value="Tea">
    <input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>

<?php
if (isset($_POST['action'])) {
    echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
查看更多
来,给爷笑一个
5楼-- · 2019-01-04 03:55

The initial post mentioned buttons. You can also replace the input tags with buttons.

<button type="submit" name="product" value="Tea">Tea</button>
<button type="submit" name="product" value="Coffee">Coffee</button>

The name and value attributes are required to submit the value when the form is submitted (the id attribute is not necessary in this case). The attribute type=submit specifies that clicking on this button causes the form to be submitted.

When the server is handling the submitted form, $_POST['product'] will contain the value "Tea" or "Coffee" depending on which button was clicked.

If you want you can also require the user to confirm before submitting the form (useful when you are implementing a delete button for example).

<button type="submit" name="product" value="Tea" onclick="return confirm('Are you sure you want tea?');">Tea</button>
<button type="submit" name="product" value="Coffee" onclick="return confirm('Are you sure you want coffee?');">Coffee</button>
查看更多
看我几分像从前
6楼-- · 2019-01-04 03:57

Use this instead:

<input id='tea-submit' type='submit' name = 'submit'    value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
查看更多
The star\"
7楼-- · 2019-01-04 04:03

Try this code :

    <html>
    <form action="" method="post">
        <input type="hidden" name="action" value="submit" />
        <select name="name">
            <option>John</option>
            <option>Henry</option>
        <select>
        <input id='submit' type='submit' name = 'tea'    value = 'Tea'>
        <input id='submit' type='submit' name = 'coffee'    value = 'Coffee'>
    </form>
    </html>
<?php
if (isset($_POST['name']) && isset($_POST['tea'])){
$tea = $_POST['tea'];
$name = $_POST['name'];
$message .= "Button  : ".$tea."\n";
$message .= "Name    : ".$name."\n";
$send = "your_email@service.tld";
$subject = "Your Subject here";
$headers = "From:$name<email@local.com>";
mail($send,$subject,$message,$headers);
}
else if (isset($_POST['name']) && isset($_POST['coffee'])){
$coffee = $_POST['coffee'];
$name = $_POST['name'];
$message .= "Button  : ".$coffee."\n";
$message .= "Name    : ".$name."\n";
$send = "your_email@service.tld";
$subject = "Your Subject here";
$headers = "From:$name<email@local.com>";
mail($send,$subject,$message,$headers);
}
?>
查看更多
登录 后发表回答