1

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> 
2
  • You get the undefined error, because on page loadthe action is created. However the variables of the function aren't defined yet. You probably need to use JS to update the action Commented Jan 29, 2013 at 21:45
  • oh, so there's not other way than using JavaScript? Commented Jan 29, 2013 at 21:52

1 Answer 1

2

You might want to check for the existence of 'quantity' inside the $_POST array before using it. Like this:

$quantity = array_key_exists('quantity', $_POST) ? $_POST['quantity'] : 0; 

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.