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.
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).
and in your php script
Additionally, you can add something like
if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] )
if you want to check if data was acctually posted.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.
The button names are not submit, so the php
$_POST['submit']
value is not set. As inisset($_POST['submit'])
evaluates to false.The initial post mentioned buttons. You can also replace the input tags with buttons.
The
name
andvalue
attributes are required to submit the value when the form is submitted (theid
attribute is not necessary in this case). The attributetype=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).
Use this instead:
Try this code :