I am using a Paypal shopping cart on one of my sites right now and was wondering if there was a way to move the <form>...</form> tags to an external file for the 'add to cart' button.

Also I am looking into a different shopping cart system, but I do not know any server side scripting. Does anyone have a recommendation and also how do I make my server secure for a shopping cart system? Thanks!

Dani AI

Generated

— you can keep the PayPal form in a separate file for reuse and easier maintenance. It is not the same mechanism as an external CSS file (CSS is fetched by the browser), but you can have the server assemble the page so the form lives in one file and is pulled into many product pages. was on the right track mentioning an include; here are practical options, a simple PHP pattern, and the security caveats to watch.

A common PHP approach: set product-specific variables on the page, then include a partial that outputs the form using those variables.

<?php
$item_name = "Blue Widget";
$price = "19.95";
include 'paypal_button.php';
?>

Contents of paypal_button.php might look like:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  <input type="hidden" name="business" value="seller@example.com">
  <input type="hidden" name="cmd" value="_cart">
  <input type="hidden" name="add" value="1">
  <input type="hidden" name="item_name" value="<?php echo htmlspecialchars($item_name); ?>">
  <input type="hidden" name="amount" value="<?php echo htmlspecialchars($price); ?>">
  <input type="submit" value="Add to cart">
</form>

Notes and cautions: any HTML form moved into a file and sent to the browser is still visible and editable by users. To prevent price tampering, use PayPal-hosted/encrypted buttons or create server-side logic that signs/verifies transactions and checks PayPal IPN/webhook notifications. If you cannot or do not want to handle server-side security, use a hosted cart or PayPal-hosted solution so card data and price logic live on the provider side.

Quick troubleshooting: make sure the include file is parsed by the server (use .php or configure the server), use correct relative paths, confirm variable scope (set vars before the include), and check file permissions. If unsure about PCI or TLS setup, use a hosted checkout to avoid handling card numbers directly.

Recommended Answers

All 3 Replies

Can you explain what you mean by "move to an external file"?

To make your site secure you have to have SSL certificate installed. You would probably be better off staying with PayPal to keep your costs down.

I mean like with css, you can link to an external css file, can this be done wtih the Paypal code, I think it's PHP but I don't know anything about server side yet...

Well... you could create your shopping page(s) with your items and paypal info on it and use ASP or PHP to do an "include" into specific pages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.