45

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 SQL database magic happens
?>

Everything except sending the submit button value works flawlessly.

6
  • 1
    Use checkbox instead!
    – Prix
    Mar 22, 2014 at 15:32
  • 5
    the button names are not submit, so the php $_POST['submit'] value is probably not set. as in isset($_POST['submit']) evaluates to false.
    – robbmj
    Mar 22, 2014 at 15:34
  • 3
    id="submit" id has a unique value, but in your case you are replicating id="submit" twice Mar 22, 2014 at 15:37
  • 3
    robbmj's comment did solve it though,, it's now working. I'd vote it up so that makes it even ;)
    – M.G.Poirot
    Mar 22, 2014 at 15:40
  • Also I think name = 'tea' is incorrect syntax. Try name='tea' without the spaces.
    – nclsvh
    Feb 7, 2018 at 10:54

7 Answers 7

52

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 />';
}
?>
3
  • 1
    I have tried this with button names btn_create and btn_update, but I cannot get their values in the $_POST. Might there be something else missing?
    – nclsvh
    Feb 7, 2018 at 11:00
  • @nclsvh is your form method post? If not, it won't show up in the $_POST collection. Jul 31, 2018 at 15:33
  • Yes, IIRC that was the problem. Thanks for the answer
    – nclsvh
    Aug 1, 2018 at 6:50
22

Use this instead:

<input id='tea-submit' type='submit' name = 'submit'    value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
3
  • Aah, that solved it. The small mistakes are the worst to solve x)
    – M.G.Poirot
    Mar 22, 2014 at 15:42
  • Was trying to get submit picked up by Perl CGI. The Name parameter made it start showing up. Thanks for the push in the right direction!
    – Westrock
    Sep 21, 2015 at 21:31
  • Probably shouldn't give them the exact same id - ID is supposed to be unique. They can have a different ID, and still have the same name.
    – Cullub
    Dec 8, 2015 at 14:34
18

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>
8

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.

7

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.

2
  • 2
    If OP ran his page through W3C Validator, it would tell him that duplicate IDs are invalid.
    – yitwail
    Mar 22, 2014 at 15:55
  • 3
    One more tip for the OP: If you are trying to debug form post / get data in php, you can do the following on the page that is being called by the form: echo '<pre>'; var_dump($_POST); echo '</pre>'; It will clearly show all data passed. Mar 22, 2014 at 15:57
0

You can maintain your html as it is but use this php code

<?php
    $name = $_POST['name'];
    $purchase1 = $_POST['Tea'];
    $purchase2 =$_POST['Coffee'];
?>
0

You could use something like this to give your button a value:

<?php
if (isset($_POST['submit'])) {
  $aSubmitVal = array_keys($_POST['submit'])[0];
  echo 'The button value is: ' . $aSubmitVal;
}
?>
<form action="/" method="post">
  <input id="someId" type="submit" name="submit[SomeValue]" value="Button name">
</form>

This will give you the string "SomeValue" as a result

https://i.stack.imgur.com/SW8q7.gif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.