html - How can I get multiple forms to use the same inputs in php

373

I have 15 identical PayPal "Buy Now" buttons each driven by its own form on a single php page. Each button has about 20 input variables but only 2 are unique to each item (item_name & item_number). Is there a way to clean up my code and have all the forms use the same input array? Not just the data, the whole string. Thanks, Wayne

Example:

<form target='paypal' action='https://www.paypal.com/cgi-bin/webscr' method='post'>
<input type='hidden' name='cmd' value='_cart'>
<input type='hidden' name='business' value='[email protected]'>
<input type='hidden' name='lc' value='US'>
<input type='hidden' name='item_name' value='Modern Art Print'>
<input type='hidden' name='item_number' value='MA024'>*
<input type='hidden' name='button_subtype' value='products'>
<input type='hidden' name='no_note' value='0'>
<input type='hidden' name='currency_code' value='USD'>
<input type='hidden' name='add' value='1'>
<input type='hidden' name='bn' value='PP-ShopCartBF:btn_cart_SM.gif:NonHostedGuest'>
<input type='hidden' name='on0' value='Select Size'>Buy Print 
<select name='os0'>
<option value='11 x 14'>11 x 14 $30.00</option>
<option value='8 x 10'>8 x 10 $20.00</option>
</select>
<input type='hidden' name='currency_code' value='USD'>
<input type='hidden' name='option_select0' value='11 x 14'>
<input type='hidden' name='option_amount0' value='30.00'>
<input type='hidden' name='option_select1' value='8 x 10'>
<input type='hidden' name='option_amount1' value='20.00'>
<input type='hidden' name='option_index' value='0'>
<input type='image' src='https://www.paypalobjects.com/en_US/i/btn/btn_cart_SM.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>
</form>
726

Answer

Solution:

Create a function that builds the form. Example:

function paypalForm( $item_name, $item_number ) {
    ?>
    <form target='paypal' action='https://www.paypal.com/cgi-bin/webscr' method='post'>
    <input type='hidden' name='cmd' value='_cart'>
    <input type='hidden' name='business' value='[email protected]'>
    <input type='hidden' name='lc' value='US'>
    <input type='hidden' name='item_name' value='<?=$item_name?>'>
    <input type='hidden' name='item_number' value='<?=$item_number?>'>*
    <input type='hidden' name='button_subtype' value='products'>
    <input type='hidden' name='no_note' value='0'>
    <input type='hidden' name='currency_code' value='USD'>
    <input type='hidden' name='add' value='1'>
    <input type='hidden' name='bn' value='PP-ShopCartBF:btn_cart_SM.gif:NonHostedGuest'>
    <input type='hidden' name='on0' value='Select Size'>Buy Print 
    <select name='os0'>
    <option value='11 x 14'>11 x 14 $30.00</option>
    <option value='8 x 10'>8 x 10 $20.00</option>
    </select>
    <input type='hidden' name='currency_code' value='USD'>
    <input type='hidden' name='option_select0' value='11 x 14'>
    <input type='hidden' name='option_amount0' value='30.00'>
    <input type='hidden' name='option_select1' value='8 x 10'>
    <input type='hidden' name='option_amount1' value='20.00'>
    <input type='hidden' name='option_index' value='0'>
    <input type='image' src='https://www.paypalobjects.com/en_US/i/btn/btn_cart_SM.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>
    </form>
    <?
}
489

Answer

Solution:

Assuming you are talking about cleaning the HTML markup, rather than generating the forms at the server side...

You could do it with Javascript, but it is not a good idea to rely on Javascript for your core site functionality, because it can be disabled by the user, and may break in a way you have not predicted in some browser or other.

I think that, since you are submitting the data directly to Paypal and therefore have no control over the server side, the answer to this is probably NO.

If you are talking about cleaning up the PHP code, Rijk van Wel's answer may help.

750

Answer

Solution:

Please refer to this article: http://www.chami.com/tips/internet/042599i.html

You can create a single form with multiple submit buttons and assign both item_name & item_number as button's value. This way the inputs aren't repeated in the code and you can easily distinguish between submitted forms.

Note: the server-side code in the article is written in ASP, but in PHP it's the same. Just refer to$_POST['buttonNameHere']

People are also looking for solutions to the problem: php - Get non-repeated characters in string and count them

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.