I would like to create Validation using AJAX and update DB before form is sent to Paypal.
<form method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr" class="paypal-button" target="_top">
<div class="hide" id="errorBox"></div>
<input type="hidden" name="env" value="www.sandbox"><input type="hidden" name="notify_url" value="www.immortalinsurance.com">
<input type="hidden" name="tax" value="0">
<input type="hidden" name="shipping" value="0">
<input type="hidden" name="currency_code" value="AUD"><input type="hidden" name="amount" value="$15.00">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="item_name" value="Cover Yourself & a Friend"><input type="hidden" name="custom" value="">
<p class="paypal-group">
<label class="paypal-label" for="policy_name1">policy_name1
<input type="text" class="paypal-input" name="policy_name1">
</label></p>
<p class="paypal-group">
<label class="paypal-label" for="policy_name2">policy_name2
<input type="text" class="paypal-input" name="policy_name2">
</label></p>
<input type="hidden" name="button" value="buynow"><input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="xxx@gmail.com"><input type="hidden" name="bn" value="JavaScriptButton_buynow">
<button type="submit" class="paypal-button large">Buy Now</button>
</form>
1, Make sure fields are NOT empty, else error.
2, Verify that the price matches $row['price']; else error.
if above statements are true then update DB and allow form to continue to paypal.
All i have at the moment is :
$(document).on('submit', '.additem-form', function(event){
var self = this;
event.preventDefault();
$.ajax({
type: "POST",
url: "controller/initialstoring.php",
data: {
itemid: $('input[name=itemid]').val(),
amount: $('input[name=amount]').val()
},
success: function(data) {
$('input[name=custom]').val($lastid);
Continue with Usual Form Submit...
}
});
});
Here is my initialstoring.php:
<?php
include("../application/config/db_config.php");
$itemid = $_POST['itemid'];
$date = date('d-m-Y h:i:s');
$itemprice = $_POST['itemprice'];
$sql = "SELECT * FROM policies WHERE id = $itemid";
$stmt = $mysqli->query($sql);
$row = mysqli_fetch_assoc($stmt);
if ($itemprice == $row['price']) {
$insert_row = $mysqli->query("INSERT INTO purchases (name_on_policy, other_on_policy, dateofpurchase, status) VALUES ('$certname', '$othercertname', '$date', '$status')");
$lastid = $mysqli->insert_id;
} else {
echo "error";
}
?>
Is this the correct method in doing this? Also would there be any reason to check the price is = $row[price] before submit?
How do i after ajax is Success and just before actual form submit, add to the custom variable?
Thanks