Aphex22
Aphex22

Reputation: 93

How to send an option from a drop-down menu to a separate submit button in HTML/PHP

I'm trying to send the option selected from a drop-down menu to a separate submit button. Here's my code so far:

 <tr>
         <td valign="top" style="width: 247px"><a href="action-amazon-sale.php?size=<?=$size?>&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&brand=<?=$product['brand_id']?>&title=<?=$product['title']?>&colour=<?=$product['colour']?>&shoe_id=<?=$product['shoe_id'];?>">
          <input name="button5" type="submit" class="submit-button-green" id="button5" value="AMAZON SOLD" style="width:100%" onclick="this.disabled=1;"/>
          </a></td>
         </tr>
          <tr>
         <td>
            <form action="" style= "width: 250px" style="height: 38px; width: 350px">
            <select name="shipping" class="button_select" style="width: 345px">
            <option value="">SELECT SHIPPING FOR AMAZON &amp EBAY SALES</option>
            <option value="0">FREE UK SHIPPING</option>
            <option value="5.50">UK SHIPPING &pound; 5.50</option>
            <option value="6.95">BOOTS SHIPPING &pound; 6.95</option>
            <option value="14.95">EURO &pound; 14.95</option>
            <option value="19.95">EURO 2 &pound; 19.95</option>
            <option value="29.95">WORLD &pound; 29.95</option>
            </select>
            </form>
            </td>
         </tr>
    </table></td>
    </tr>

I want the user to first select a shipping option from the drop-down menu and upon clicking the 'AMAZON SOLD' submit button, have that value POST to the subsequent PHP page, i.e. action-amazon-sale.php ...

Can anybody please tell me how I should do this?

Thanks.


UPDATE #2

   <tr>
        <td valign="top" style="width: 247px"><a href="action-amazon-sale.php?size=<?=$size?>&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&brand=<?=$product['brand_id']?>&title=<?=$product['title']?>&colour=<?=$product['colour']?>&shoe_id=<?=$product['shoe_id'];?>">
          <input name="button5" type="submit" class="submit-button-green" id="button5" value="AMAZON SOLD" style="width:100%"  form="shipping" formmethod="post" 
  formaction="action-amazon-sale.php?size=<?=$size?>&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&brand=<?=$product['brand_id']?>&title=<?=$product['title']?>&colour=<?=$product['colour']?>&shoe_id=<?=$product['shoe_id'];?>" onclick="this.disabled=1;"/>
          </a></td>  
          <td width="530" align="right"></td>
    </tr>
     <td width="252" valign="top" style="height: 13px"><a href="action-amazon-sale.php?size=<?=$size?>_con_b&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&brand=<?=$product['brand_id']?>&title=<?=$product['title']?>&colour=<?=$product['colour']?>&shoe_id=<?=$product['shoe_id'];?>">
            <input name="button5" type="submit" class="submit-button-green" id="button5" value="AMAZON CONCESSION SOLD" style="width:100%"  form="shipping" formmethod="post" formaction="action-amazon-sale.php?size=<?=$size?>_con_b&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&brand=<?=$product['brand_id']?>&title=<?=$product['title']?>&colour=<?=$product['colour']?>&shoe_id=<?=$product['shoe_id'];?>" onclick="this.disabled=1;"/>
            </a></td> 
          <tr>
                 <form id="shipping">
     <td valign="top">
              <select name="shipping" class="button_select" style="width: 276px">
              <option value="">SELECT SHIPPING FOR AMAZON SALES</option>
              <option value="0">FREE UK SHIPPING</option>
              <option value="16.95">EURO SHIPPING &pound; 16.95</option>
              <option value="14.50">BOOTS SHIPPING &euro; 17.50</option>
                  </select>       
             </form></td></tr>
      <tr>

On the target URL I have written a short test i.e.

$shipping=$_POST['shipping'];

echo $shipping;

However, the result is blank. Where am I going wrong? Thanks

Upvotes: 0

Views: 2142

Answers (2)

iamsleepy
iamsleepy

Reputation: 540

You id your form like this:

<form id="myform">
  <input type="text" name="example" value="" />
</form>

And the submit button:

<input type="submit" value="submit" form="myform" />

OR if you want to define the method, if you define the formmethod in your submit button it will override the method defined in the <form> tag.

<input type="submit" value="submit" form="myform" formmethod="post" />

This indicate that the submit belongs to myform. I think this is HTML5 added attributes. Some browsers won't submit if the submit input is placed inside the form tags.

This attribute can be used for most if not all html input. This means you can have your input field placed outside the form but still belong to the form.

You can use your updated codes using separate form for each one. What you put in a href should be in the formaction.

<input type="submit" form="myform" formaction="action-amazon-sale.php?size=<?=$size?>&amp;id=<?=$product['product_id'];?>&barcode=<?=$barcode?>&shop_price=<?=$product['price']?>&brand=<?=$product['brand_id']?>&title=<?=$product['title']?>&colour=<?=$product['colour']?>&shoe_id=<?=$product['shoe_id'];?>"

Or you can just refer to one form. Like this:

        <form id="shipping" style= "width: 250px" style="height: 38px; width: 350px">
        <select name="shipping" class="button_select" style="width: 345px">
        <option value="">SELECT SHIPPING FOR AMAZON &amp EBAY SALES</option>
        <option value="0">FREE UK SHIPPING</option>
        <option value="5.50">UK SHIPPING &pound; 5.50</option>
        <option value="6.95">BOOTS SHIPPING &pound; 6.95</option>
        <option value="14.95">EURO &pound; 14.95</option>
        <option value="19.95">EURO 2 &pound; 19.95</option>
        <option value="29.95">WORLD &pound; 29.95</option>
        </select>
        </form>

with your buttons, like this:

  <input name="button5" type="submit" class="submit-button-green" id="button5" value="AMAZON SOLD" style="width:100%"  form="shipping" formmethod="post" 
  formaction="action-amazon-sale.php?size=<?=$size?>...." onclick="this.disabled=1;"/>

   <input name="button5" type="submit" class="submit-button-green" id="button5" value="AMAZON SOLD" style="width:100%"  form="shipping" formmethod="post" 
  formaction="action-amazon-sale.php?size=<?=$size?>_con_b&amp;id=" onclick="this.disabled=1;"/>

Upvotes: 0

Om Prakash
Om Prakash

Reputation: 54

You need to put the input button inside the form and method should be POST

<form method="POST" action="action-amazon-sale.php?size=<?= $size ?>&amp;id=<?= $product['product_id']; ?>&barcode=<?= $barcode ?>&shop_price=<?= $product['price'] ?>&brand=<?= $product['brand_id'] ?>&title=<?= $product['title'] ?>&colour=<?= $product['colour'] ?>&shoe_id=<?= $product['shoe_id']; ?>" style= "width: 250px" style="height: 38px; width: 350px"> 
      <table>
        <tr>
          <td valign="top" style="width: 247px">
            <input name="button5" type="submit" class="submit-button-green" id="button5" value="AMAZON SOLD" style="width:100%" onclick="this.disabled = 1;"/>
          </td>
        </tr>
        <tr>
          <td>
            <select name="shipping" class="button_select" style="width: 345px">
              <option value="">SELECT SHIPPING FOR AMAZON &amp EBAY SALES</option>
              <option value="0">FREE UK SHIPPING</option>
              <option value="5.50">UK SHIPPING &pound; 5.50</option>
              <option value="6.95">BOOTS SHIPPING &pound; 6.95</option>
              <option value="14.95">EURO &pound; 14.95</option>
              <option value="19.95">EURO 2 &pound; 19.95</option>
              <option value="29.95">WORLD &pound; 29.95</option>
            </select>
          </td>
        </tr>
      </table>
    </form>

Upvotes: 1

Related Questions