Search code examples
phpjavascriptjqueryhtmlforms

How to make form with two buttons submit a different value for each button?


    <form action="here.php" method="POST">
    <input type="text" name="text">
    
    <div id="one">
       <input type="hidden" name="aaa" value="one">
       <input type="submit" value="Send">
    </div>
    
    <div id="two">
       <input type="hidden" name="aaa" value="two">
       <input type="submit" value="Send">
    </div>
    
    </form>

Clicking on Send of div one or div two, the POST data has $_POST['aaa'] = 'two'

Is it possible to make one form with two submit buttons that POST different values?

If I click on div one submit, I would like to receive $_POST['aaa'] = 'one' and if i click on div two submit I would like to receive $_POST['aaa'] = 'two'.

I'm using PHP and jQuery.

I don't want two forms. I don't want multiple <input type="text" name="text">


Solution

  • Instead of using hidden inputs, e.g.,

    <input type="hidden" name="aaa" value="one">
    

    use a value attribute for each of the buttons, and assign a different value to each of them, e.g.,

    <form action="demo_form.asp" method="get">
      Choose your favorite subject:
      <button name="subject" type="submit" value="fav_HTML">HTML</button>
      <button name="subject" type="submit" value="fav_CSS">CSS</button>
    </form>
    

    [reference]