asprin
asprin

Reputation: 9823

Using $_POST variable inside Form tag of Paypal

I searched before asking this question. I didn't find an answer probably because I didn't use the correct keywords while searching.

Anyways, here is what I'm faced with:

I want to sell tickets from a website. Each tickets costs $10. I'm using Paypal for receiving the payment. The html form consists of this:

<tr>
    <td><label for="not">Number of Tickets</label></td>
    <td><input name="not" type="text" id="not" /></td>
</tr>

 <?php $total = $_POST['not']*10; ?>    

<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="amount" value="<?php echo $total; ?>">
<input name="item_name" type="hidden" id="item_name" value="Ticket Purchase" />

 <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Pay" name="submit"  />
</tr>   

The problem is the total amount is to be calculated based on number of tickets being purchased. So if someone enters 5 tickets, the total should be $50 which will be used as value inside the 'amount' variable. But paypal isn't taking the $total value. So I was wondering how should I go about posting a variable amount inside the ?

Thanks in advance, Nisar

Upvotes: 1

Views: 228

Answers (1)

Nicholas King
Nicholas King

Reputation: 938

I think you would be better off calculating the total with javascript/jquery and update it that way.

If you change

<input type="hidden" name="amount" value="$total">

to

<input type="hidden" name="amount" value="" id="total">

then inside script brackets using jquery something like this

$("#not").change(function()
{
  var total = $("#not").val() * 10;
  $("#total").val(total);
});

Although you still need to do data validation i.e. ensure that "not" is an integer.

Hope that helps.

Thanks

Nicholas

Upvotes: 2

Related Questions