I have a function that creates a URL depending on what product and what quantity the customer selected from the form page.
The function looks something like this:
function getPurchaseLink($token, $csvfile, $force_platform = "", $quantity = 1){
// Code to create url
}
Each product has its own form with a select dropdown list of quantity.
The problem is when I hard code it the quantity like: $quantity = 1;
and in the form action I put:
<form method="post" action="<?php echo getPurchaseLink($TOKEN, $csvfile, $force_platform = "", $quantity); ?>">
It works when I submit the form.
But it’s not the way I want it. I want to get the quantity value of the select box options.
Like $quantity = $_POST[‘quantity’];
But I got an Undefined Error, When the page is load.
So, I can’t submit the form if I can’t get the quantity value from the select box.
Here’s my whole form:
<?php
require 'script_purchase.php';
$csvfile = 'purchaselinks.csv';
//echo getPurchaseLink($token = '1Opp2p11M', $csvfile, $force_platform = "", $quantity = 99) . '<br/>';
$quantity = $_POST['quantity'];
?>
<html>
<body>
<form method="post" action="<?php echo getPurchaseLink('1Opp2p11M', $csvfile, $force_platform = "", $quantity); ?>">
<div class="comp">
<label class="font_12" for="quantity"># of PCs</label><br/>
<select class="font_12" id="quantity" name="quantity">
<option value="10">10 PCs</option>
<option value="25">25 PCs</option>
<option value="50">50 PCs</option>
<option value="99">99 PCs</option>
</select>
</div>
<div class="div_small"><span class="font_12"><br/>for</span></div>
<input name="submit" id="button_addtocart" type="submit" value="Submit" class="checkout" />
</form>
</body>
</html>
You might want to check for the existence of
'quantity'inside the$_POSTarray before using it. Like this:Or some other value as a default. The problem is, the first time the page gets rendered there’s nothing inside the POST data.
You should also apply some kind of validation (just in case someone wants to abuse your script). Have a look at http://www.php.net/manual/en/function.filter-input.php