我四处寻找解决方案,偶然发现了 AJAX java script/jquery 组合的解决方案。 在我-6ren">
gpt4 book ai didi

php - 优惠券代码方法 Ajax jquery

转载 作者:行者123 更新时间:2023-11-28 23:58:33 25 4
gpt4 key购买 nike

我正在尝试将优惠券代码系统方法添加到我现有的表单中,唯一的问题是该表单已经有一个表单操作。

<form id="apply" name="apply" method="post" action="<?php echo $editFormAction; ?>">

我四处寻找解决方案,偶然发现了 AJAX java script/jquery 组合的解决方案。

在我的代码中,我回显了数据库中将用于数学计算的折扣值。我还回应了优惠券代码本身和要减少的总数。

<tr>
<td class="detailnoborder"><label for="Dicount_code">Enter Code for Discount:</label>&nbsp;&nbsp;</td>
<td class="detailnoborder1">
<input type="text" tabindex="33" id="coupon" name="coupon_id" size="10"/>
<input type="text" tabindex="10" id="T_cost" name="T_cost" size="5" value="<?php echo $row_rsMembershipTypes['Cost']?>"/>
<input type="text" tabindex="10" id="Discount" name="Discount" size="5" value="<?php echo $row_rsdiscount['discount']?>"/>
<input type="text" tabindex="10" id="D_amount" name="D_amount" size="5" value="<?php echo $row_rsdiscount['D_Cost']?>"/>
<input type="button" id="Check" value="Check" onclick="chk_discount()" type="submit"/>
</tr>

我最初有一个名为 chk_discount 的函数,我完全不确定我在做什么。

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$("#Check").click(function() {
$.ajax({
type: "POST",
url: 'ajax.php',
data:{coupon: $('#coupon').val(), },
success:function(data) {
if (data != ''){
//display error message however you would like
}
}
});
}
</script>

进一步研究后,我决定创建一个连接到我的 ajax.php 文件的函数,php 函数将位于该文件中。老实说,我比较困惑。

 <?php
if(isset($_POST['coupon'] == HXAR1){
$D_Cost - $Cost;
}

return ($Cost);
?>

理想情况下,当我发布到 ajax 时,我想做一个数学运算,用一个数据库值减去另一个数据库值,得到总成本,然后在提交时发送到 Paypal,但支票折扣将在之前完成..

我不要求任何人为我做这件事,但如果能提供帮助,我们将不胜感激。

最佳答案

我将尝试解释我的理解:

  1. You have a Coupon code, that checks from Javascript (client side) to PHP (server side) that the coupon has a registered code (HXAR1). This is a security method to ensure a user does not change it in client code, and then, changes your product value, or uses a coupon that is not registered. Then your server (PHP) is the master to give that info.

  2. After calling ajax.php (a file that will run in server) from your AJAX client (Asynchronous JavaScript And XML methods ) your PHP server function will return the REAL COST after applying the coupon discount, then that value is the one you need to send to PAYPAL.

  3. AJAX will receive PHP return value in success method, then, you will need to retrieve this value in javascript (at client) and send it to PAYPAL.

我将尝试向您解释您必须在哪里进行编码:

看看我准备的片段。它将很容易地向您解释如何将数据发送到 php 文件。

注意 你有一些 PHP 代码让我认为你有 N 次出现这种结构,那么 $("#Check") 或 $("#coupon") 选择器将不起作用如果出现不止一次。我在那里删除了 php 代码,然后你可以使用你自己的值

  $("#Check").click(function() {
$.ajax({
type: "POST",
url: 'ajax.php',
data: {
Dis_code: $('#coupon').val(),
T_cost: $('#T_cost').val(),
Discount: $('#Discount').val()
},
success: function(data) {
if (data != '') {
alert("you succesfully send and received "+data+" from ajax.php");

//HERE IS THE SUBMIT YOU NEED ALL. IT WILL
//SUBMIT THE FORM TO THE ORIGINAL ACTION
$("#apply").submit();
}
},
error: function(data){
alert("You tried to send data to ajax.php but it didn´t give a OK response");
}

});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<tr>
<td class="detailnoborder">
<label for="Dicount_code">Enter Code for Discount:</label>&nbsp;&nbsp;</td>
<td class="detailnoborder1">
<form id="apply" name="apply" method="post" action="<?php echo $editFormAction; ?>">

<br>Coupon<input type="text" tabindex="33" id="coupon" name="coupon_id" size="10" />
<br>
Cost<input type="text" tabindex="10" id="T_cost" name="T_cost" size="5" value="" />
<br>Discount<input type="text" tabindex="10" id="Discount" name="Discount" size="5" value=""/>
<br>DiscountCost<input type="text" tabindex="10" id="D_amount" name="D_amount" size="5" value="" />
<br><input type="button" id="Check" value="Check" />
</tr>

HOW IT WORKS

  • The jquery click() function binds your ajax function to an element with id Check (in this case, the button). If there is more than one button, you should make it in a different way. Check the button has not type="submit", then it will be a dummy button at first (before this click binding is executed).

  • $.ajax picks the values from coupon, T_cost, and Discount inputs, and puts them in the data payload of POST request, and sends IT after user click.

  • Ajax.php will receive the data payload, and will able to use its parameters to make all the operations you need. After that, will send back a RESPONSE that ajax will receive.

  • $.ajax will receive the response send by Ajax.php in success method, and then you will be able to do what to do with it. This method will be executed right after php in server side has ben executed and the response has been send.

  • After all, in success(), $.submit() will send the form called "apply" to it´s original action.

希望这些解释和片段中的代码能让您明白。抱歉,但我必须删除 php 代码才能使其运行。

无论如何,这里有一些关于 javascript 和 php 通信的信息

jQuery Ajax POST example with PHP

Pass variable from jQuery to PHP

关于php - 优惠券代码方法 Ajax jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30752674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com