Multiple "Buy Me" buttons

Hi there. I have a product page (subscribe.php) which displays 3 different items. Product-A, Product-B, snd Product-C.

The items are displayed in an HTML table, and each product has a “Buy Me” submit button associated with it.

My form uses the following code:

<td><input type="submit" name="selectPlanA" class="button3" value="Buy Me" /></td>
<td><input type="submit" name="selectPlanB" class="button3" value="Buy Me" /></td>
<td><input type="submit" name="selectPlanC" class="button3" value="Buy Me" /></td>

Here is what I want to happen…

For my subscribe.php script, when the user clicks one of the “Buy Me” buttons, my subscribe.php will inspect the $_POST array and see which button was selected.

If a user clicked on the “Buy Me” button for either Product-A or Product-B, then the user should be routed to my checkout.php script.

If a user clicked on the “Buy Me” button for Product-C, then the user should be routed to productcatalog.php where the user gets to choose a free book.

The problem is that I’m not sure how to have my subscribe.php script figure out which button was chosen?

You help would be appreciated!!

Which value exists in your $_POST array, ‘selectPlanA’,‘selectPlanB’, or ‘selectPlanC’?

I haven’t coded this yet, and haven’t done this in ages.

I thought what was passed to the $_POST array was what was in the value= attribute, no?

Correct.

My post was, in effect, “Try it as you have it written, and look closely at what’s in the $_POST array when you push different buttons.”

1 Like

I want the same value in each button. And if the $_POST array contains what is in value= then I have a conflict…

Try it, and look closely.

2 Likes

@m_hutley,

hI, i’m late for a doctor’s appt and don’t have access to my code right now, but let me see if I can connect the dots…

If I built my form the way mentioned, and the user clicked on Plan-B, then when I submit the form does my $_POST array look like this…

$_POST[selectPlanA]=
$_POST[selectPlanB]="Buy Me"
$_POST[selectPlanC]=

If so, I guess that would be enough to figure out where to branch to.

Am I warmer?

Also, if that assumption is right, then is that an okay way to approach my problem, or is that sloppy PHP coding??

Thanks.

You’re warm, but not quite there.

I would advise you to stop trying to just cerebr-ize your page without trying to code it, and just try. You’ll learn a lot more by trying and inspecting the result than you will by saying “well I think it will behave this way”. Webpage code is not a one-and-done thing, you get as many stabs at it as you need.

As far as ‘sloppy’ goes - I don’t generally like forms with multiple submit buttons. If you want to offer a choice, that’s a <select>. But that’s me, and multiple buttons is a valid method.

But lets take your case.

If I had a form that looked like this:

<form method='POST'>
<input type='text' name='name'>
<input type='text' name='email'>
<input type="submit" name="selectPlanA" class="button3" value="Buy Me" />
<input type="submit" name="selectPlanB" class="button3" value="Buy Me" />
<input type="submit" name="selectPlanC" class="button3" value="Buy Me" />
</form>
<?php
var_dump($_POST);
?>

(This is my s1.php. This is how I try your problem.)

When i fill out the form and push one of the buttons, the var_dump at the bottom puts out this:

array(3) { ["name"]=> string(8) "m_hutley" ["email"]=> string(27) "m_hutley@notmyrealemail.com" ["selectPlanB"]=> string(6) "Buy Me" }

Now you tell me. Which button did I push?

1 Like

Finally back!

I assume you pressed the middle choice, Plan-B?!

Correct.

So can I do something like this in my PHP script…

// pseudocode

IF ($_POST['selectPlanA']){
  // Redirect to checkout.php

}elseif ($_POST['selectPlanB']){
  // Redirect to checkout.php

}elseif ($_POST['selectPlanC']){
  // Redirect to catalog.php

}else{
  // Display an error message
}

close, take a look at isset

@m_hutley,

How does this look…

<?php
	// HANDLE FORM.
	if ($_SERVER['REQUEST_METHOD']=='POST'){
		// Form was Submitted (Post).
 		if (isset($_POST['selectPlanA'])){
      // Plan-A chosen.
      echo "<p>You selected Plan-A!</p>";
      
    }elseif(isset($_POST['selectPlanB'])){
      // Plan-B chosen.
      echo "<p>You selected Plan-B!</p>";
      
    }elseif(isset($_POST['selectPlanC'])){
      // Plan-C chosen.
      echo "<p>You selected Plan-C!</p>";
      
    }else{
      // Invalid choice.
      echo "<p>Invalid choice!</p>"; 
    }
	}//End of HANDLE FORM
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <style>
    .button3{
      display: inline-block;
      width: auto;
      overflow: visible;
      margin: -10px 0px 0em 5px;
      padding: 0em 0.8em 0 0.8em;
      border: 1px solid #326985;
      font-family: inherit;
      font-size: 12px;
      text-align: center;
      color: #FFF;
      background: #3181bc;
      line-height: 25px;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      border-radius: 5px;
      text-decoration: none;
    }

    form{
      margin: 3em;
    }
    
    #membershipPlans{
      border-collapse: collapse;
    }

    #membershipPlans th, 
    #membershipPlans td{
      border: 1px solid #AAA;
      text-align: center;
    }

    #membershipPlans td{
      height: 40px;
    }
    
    #membershipPlans th:first-child,
    #membershipPlans td:first-child{
      padding: 0 0.5em 0 0;
      text-align: right;
    }

    #membershipPlans col{
      width: 100px;
    }

    #membershipPlans col#feature{
      text-align: left;
    }

    #membershipPlans col#premiumPlus{
      background-color: #FF9900;
    }
  </style>
</head>

<body>
  <form id="productGrid" action="" method="post">  
    <table id="membershipPlans">
      <!-- Column Groups -->
      <colgroup>
        <col id="feature">
        <col id="guest">
        <col id="basic">
        <col id="premium">
        <col id="premiumPlus">
      </colgroup>

      <!-- Column Headings -->
      <tr>
        <th>Feature</th>
        <th>Guest</th>
        <th>Basic<br/>Access</th>
        <th>Premium<br/>Access</th>
        <th>Premium<br/>+ eBook</th>
      </tr>

      <!-- Column Headings -->
      <tr>
        <td></td>
        <td></td>
        <td><input type="submit" name="selectPlanA" class="button3" value="Select"/></td>
        <td><input type="submit" name="selectPlanB" class="button3" value="Select"/></td>
        <td><input type="submit" name="selectPlanC" class="button3" value="Select"/></td>
      </tr>

      <!-- Rows -->
      <tr>
        <td>News:</td>
        <td>X</td>
        <td>X</td>
        <td>X</td>
        <td>X</td>
      </tr>
      <tr>
        <td>Articles:</td>
        <td>X</td>
        <td>X</td>
        <td>X</td>
        <td>X</td>
      </tr>
      <tr>
        <td>Opinion:</td>
        <td>-</td>
        <td>X</td>
        <td>X</td>
        <td>X</td>
      </tr>
      <tr>
        <td>Interviews:</td>
        <td>-</td>
        <td>-</td>
        <td>X</td>
        <td>X</td>
      </tr>
    </table>
  </form>

</body>
</html>

At a cursory glance, looks fine.

So now that I have a basic mockup, I need to make this more usable/manageable.

I’m trying to come up with a practical solution so that I can change prices and have my “checkout.php” script update things accordingly.

Originally I was just going to use what I have above, and in my “checkout.php” script do something like this…

IF ($_SESSION['chosenPlan'] = selectPlanA){
  $orderTotal = $20;

ELSEIF ($_SESSION['chosenPlan'] = selectPlanB){
  $orderTotal = $30;

ELSEIF ($_SESSION['chosenPlan'] = selectPlanC){
  $orderTotal = $40;

ELSE{
  echo "An error has occurred!";
  exit();
ENDIF

When I was thinking about this last night I came up with these ideas…

Option #1:
Find some way so when the user clicks on one of the “Select” buttons on my “offers.php” (i.e. code above) script, I pass a corresponding hard-coded value to my “checkout.php” script.

Of course, when the price changes then I would have to go in and update the hard-coded price in "offers.php’

Option #2:
Use the code above, and have “checkout.php” determine which button was chosen, and then use a hard-coded value in “checkout.php” to display the Order Total.

Option #3:
Determine which option was chosen in “offers.php” and then in “checkout.php” look up the value in my database.

Does this make sense?

Ideas?

Option #3 would be the normal way to do it. Your database contains the pricing and product details, and is used to display that price on the first page, and then to use it during the checkout phase. Hard-coding is rarely the best way to do anything that has the slightest chance of changing.

2 Likes

@droopsnoot,

Even if I store pricing in a table, the design I used in post #13 is still hard-coded.

That is to say it relies on me using hard-coded values associated with my “Select” buttons in the Form as in…

<input type="submit" name="selectPlanA" class="button3" value="Select"/>
<input type="submit" name="selectPlanB" class="button3" value="Select"/>
<input type="submit" name="selectPlanC" class="button3" value="Select"/>

and then in my PHP script…

	// HANDLE FORM.
	if ($_SERVER['REQUEST_METHOD']=='POST'){
		// Form was Submitted (Post).
 		if (isset($_POST['selectPlanA'])){
      // Plan-A chosen.
      echo "<p>You selected Plan-A!</p>";
      
    }elseif(isset($_POST['selectPlanB'])){
      // Plan-B chosen.
      echo "<p>You selected Plan-B!</p>";
      
    }elseif(isset($_POST['selectPlanC'])){
      // Plan-C chosen.
      echo "<p>You selected Plan-C!</p>";
      
    }else{
      // Invalid choice.
      echo "<p>Invalid choice!</p>"; 
    }
	}//End of HANDLE FORM

If next week I come up with pricing plans PlanD, PlanE, PlanF then I still have to do in and change my code and my PHP code.

See?

Here is what I would like to do…

Assume that my subscriptions page always has 3 choices, although those choices (and pricing) may change regularly.

For each of my three “Select” buttons, if I could somehow have a corresponding code (e.g. plan-001, plan-002, plan-003,… plan-999) that gets submitted when a given button is clicked, then the receiving page (e.g. “checkout.php”) could then grab that code, and either reference a hard-coded table OR look things up in a pricing table in my database.

But the key point is that I should only have to change the “pricing codes” associated with each “Select” button on my subscriptions page. Follow me?

I should not have to change anything in my code on “subscriptions.php” (i.e. checing with Select button was chosen) and I shouldn’t have to change anything to process the codes in “checkout.php”.

I would just have to go in and change the “pricing codes” in the subscriptions page. (And down the road if I wanted to get fancy, that could be linked up to a database as well, where I just populate those variables with the latest codes, but that is down the road.)

Does my proposed solution make sense?

And how do I make it happen?? :slightly_frowning_face:

There’s no reason that the button names couldn’t be the ids for your prices in the database, surely?

Your database could contain a flag to say whether the subscription is “live” or not, and when the form is generated, it only draws buttons for the live subs, which you say will only ever be three. Once you want to drop subs A, B and C, you simply mark those as not live (or delete them, if you can deal with the audit trail), create your new ones and mark them as live, and the subs page will update itself.

Am I going to have a button clled “#TG543M”? :wink:

No.

Yes, I was thinking of that, but that still doesn’t address the hang up…

Whether the “pricing strategy” comes from a database or is hard-coded, how do I associate pricing strategy #TG543M with the 2nd “Select” button in such a way that i don’t have to change the code in the <input>?

It isn’t practical to make value= the procong strategy because that wouldn’t mean Jack to a user, and I want to make the button labels verbs telling people what to do (e.g. Select, Buy Now, etc).

As in my first attempt, I could certain set name= the pricing strategy, but then every time the pricing strategy changes, I have to update my form code AND the code handling it in “checkout.php”.

The only thing I can think of to address these issues is maybe this…

In my html table, I could wrap each “Select” button in its own html form, AND I could add a hidden field to each separate form that contains either a hard-coded "pricing stratgey’ or a variable that gets populated maybe from my database.

That way if you clcik on the 2nd “Select” button, my 'checkout.php" script will see the following…

<input type="submit" name="selectPlanB" class="button3" value="Select"/>
<input type="hidden" name=selectPlanB_pricing" value="30" />

$_POST{
  ['selectPlanB'] = "Select"
  ['selectPlanB_pricing'] = "30"
}

What do you think about that?

Well I certainly wouldn’t go hard coding a bunch of IF conditions. You can have any number of plans stored in the DB and simply query ONE time to gather all display and processing data and place this in an array using the plan ID as the primary array key. No other form fields are needed beside the button if you use this KEY as the button key. I give example how you would build this array from query result and also a hard coded sample array for the demo. Note all query and processing are above output to browser. Sample:

<?php
/*
Assuming plan names are called "Plan A", "Plan B" etc.
Any number of fields can be grabbed from DB to use in both display and or processing.
Example	I will use `id`, `plan_name`, `price`, `description`
The data array can come from DB query and this single query used for both display and processing.
Ideally I would use the ID as the primary array key. 
*/
 
/*
//DB Version
$available_plans = array();

//Query goes here
while($row = $result->fetch_assoc()){
	$available_plans[$row['id']] = $row;
}
*/

//Sample hard coded version
$available_plans = array(
	'1' => array('id' => '1', 'plan_name' => 'Plan A', 'price' => '250', 'description' => 'Plan A is a great plan'),
	'2' => array('id' => '2', 'plan_name' => 'Plan B', 'price' => '220', 'description' => 'Plan B is a great plan'),
	'3' => array('id' => '3', 'plan_name' => 'Plan C', 'price' => '200', 'description' => 'Plan C is a great plan'),
	'4' => array('id' => '4', 'plan_name' => 'Plan D', 'price' => '185', 'description' => 'Plan D is a great plan'),
	'5' => array('id' => '5', 'plan_name' => 'Plan E', 'price' => '150', 'description' => 'Plan E is a great plan'),
	'6' => array('id' => '6', 'plan_name' => 'Plan F', 'price' => '135', 'description' => 'Plan F is a great plan'),
	'7' => array('id' => '7', 'plan_name' => 'Plan G', 'price' => '120', 'description' => 'Plan G is a great plan'),
	'8' => array('id' => '8', 'plan_name' => 'Plan H', 'price' => '100', 'description' => 'Plan H is a great plan')
); 

//Sample Processing
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['select_plan'])):
	//As only ONE button will be pressed you can use array_keys to get plan id 
	$plan_keys = array_keys($_POST['select_plan']);	
	$selected_plan = $plan_keys[0];
	//All values from the available_plans array are available 
	echo '<pre>';
	print_r($available_plans[$selected_plan]);
	echo '</pre>';
endif;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Cool Site</title>
</head>
<body>
<form action="samplebuttonform.php" method="post">
<?php
	if(!empty($available_plans)):
		foreach($available_plans as $plan_id => $arr):
			echo '<div style="width:20%; margin:1%; padding:1%; border:1px solid #ddd; float:left;">'."\r";
				echo '<div style="display: block; text-align:center;"><b>'.$arr['plan_name'].'</b></div>'."\r";
				echo '<div style="display: block; text-align:left;">'.$arr['description'].'</div>'."\r";  
				echo '<div style="display: block; text-align:center;"><b>PRICE $'.$arr['price'].'</b></div>'."\r"; 
				echo '<div style="display: block; text-align:center;"><input type="submit" name="select_plan['.$plan_id.']" value="Select '.$arr['plan_name'].'" /></div>'."\r";
			echo '</div>'."\r";
		endforeach;
	endif;
?>
</form>
</body>
</html>

You’ll notice that only one button is pressed at any time so you can use array_keys to get selected plan during processing. In sample I am printing the details from selected plan.

1 Like