dongwei5740 2015-05-13 03:29
浏览 25

AJAX在表单重定向到Paypal之前验证并将数据发送到DB

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 &amp; 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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)