Depending on which button the user clicks, the form will be sent with the value of the name attribute. How to write the value of the name attribute in a variable, if the value in it may be different?

<input name="Сухари" type="submit" value="Заказать сухари" /> <input name="Чипсы" type="submit" value="Заказать чипсы" /> 

How do I get the word "Crackers" when pressed to enter in the $ zakaz variable and if I click on the word "Chips", enter in the variable $ zakaz.

    3 answers 3

    When submitting a form, the name of the submit that was clicked is sent. That is, in your case check is necessary

     if(isset($_POST['Сухари'])) { //используем $_POST['Сухари_value'] } elseif(isset($_POST['Чипсы'])) { //используем $_POST['Чипсы_value'] } 

    But in general, I do not advise you to call variables Cyrillic.

    • if utf-8, IMHO, no matter how you call it) - kemerov4anin

    Remember that IE does not allow you to manipulate the name attribute in the form, to change the attribute you need to hide the old interface and create a new one with new attributes. Maybe you should do the checkbox "Order crackers / chips" and depending on which one is selected to show the corresponding input with the necessary name attribute?

      To send data to the server in your html-code must be a form or it must process the JavaScript, giving the data asynchronously!

       <form name="zakaz_form" method="post" action="path\to_script.php"> <input name="suhari" ...> <input name="chips" ...> </form> 

      And already in the script, the variable named $ _POST ['suhari'] will have the desired value if you click on the button.