Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send value of submit button when form gets posted

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.

like image 249
M.G.Poirot Avatar asked Mar 22 '14 15:03

M.G.Poirot


People also ask

How send data using submit button?

The formmethod attribute specifies which HTTP method to use when sending the form-data. This attribute overrides the form's method attribute. The formmethod attribute is only used for buttons with type="submit" . The form-data can be sent as URL variables (with method="get" ) or as HTTP post (with method="post" ).

How do I get form values on submit?

To get form values on submit, we can pass in an event handler function into the onSubmit prop to get the inputted form values. We use the useState hook so that we can use the phone state as the value of the value prop.

How do I link a submit button to a form?

To link a submit button to another webpage using HTML Form Tags: Using the HTML Form's Action Attribute, we can link the submit button to a separate page in the HTML Form element. Here, declare/write the “Submit button” within HTML Form Tags and give the File Path inside the HTML form's action property.


1 Answers

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 />'; } ?> 
like image 130
robbmj Avatar answered Sep 19 '22 23:09

robbmj