【发布时间】:2016-01-03 16:31:59
【问题描述】:
编辑:我终于成功了!下面是我的例子。我希望这可以帮助有同样需要的其他人。
我需要一个发货单选按钮。一种用于普通运输,一种用于快递运输。
在此表单上通过谷歌搜索和 cale_b 获得一些帮助后,我的问题如下:
我有一个 foreach 循环,它从数据库中获取值并将它们显示在页面上。我需要一个单选按钮选择器(或任何以相同方式工作的东西)来选择用户想要的运输类型。我需要每行上的购买按钮来获得从另一个 php 页面计算的总数。
我可以从我的运输数据库中提取所有这些信息并进行计算并使用 ajax 将其发送回页面,但我需要能够根据每个动态获取发送和带回的价格、尺寸、类型和 shippingType排。然后使用新的总成本并将其分配给一个名为 totalCost 的输入字段以进行提交。
这是我现在使用的 html/php 代码。
<div class="details">
<table class="default">
<th>Type</th>
<th>Size</th>
<th>Price</th>
<th>Shipping</th>
<th>Total</th>
<th>Buy</th>
<?php
$getArtDetails = get_tableContentsWhere($con,'prices','artID',$id);
foreach($getArtDetails as $detail)
{
?>
<tr>
<td class="type"><?= $detail['type'] ?></td>
<td class="size"><?= $detail['size'] ?></td>
<td class="price">$<?= $detail['price'] ?>.00</td>
<td>
<input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="Xpress">Express Shipping
<input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="Regular">Regular Shipping
<input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="Free">Free Shipping
</td>
<td class="shipping"></td>
<td>
<form action="" method="post">
<input type="hidden" name="totalCost" value="set by jquery?">
<input type="submit" name="submit" value="Buy">
</form>
</td>
</tr>
<?php } ?>
</table>
</div>
这是返回我的总成本的 getShipping.php 文件。
shipping.php:
<?php
include 'admin/config/functions.php';
$size = $_GET['size'];
$type = $_GET['type'];
$price = $_GET['price'];
$shippingType = $_GET['shippingType'];
$shipping = get_shippingRate($con,$size,$type,$shippingType);
$totalCost = $shipping + $price;
echo $totalCost;
以下脚本完成了我需要它做的事情,但有 2 个问题 1.当我选择一个单选按钮时,它们的值都会改变,而不是每一个。 cale_b 我在这里尝试了您的解决方案以查找下一个 TR,但它不返回结果。反过来也不设置提交的输入 2. 另一个问题是我需要它将尺寸、类型和价格动态发送到我的 getShipping.php 页面。我也不知道该怎么做。
<script>
$('input[name^="shipping"]').each(function(index) {
$(this).on('click', function(){
var shippingType = $(this).val();
var size = $(this).closest('tr').find('td.size').text();
var type = $(this).closest('tr').find('td.type').text();
var price = $(this).closest('tr').find('td.price').text();
// I had to set $(this) to equal $this so I could access it within the ajax call as $(this) was referring to itself.
var $this = $(this);
$.ajax({
type: "GET",
url: "getShipping.php",
data: 'size=' + size + '&type=' + type + '&shippingType=' + shippingType + '&price=' + price,
success:function(data){
//called when successful
$this.closest('tr').find('td.shipping').html(data);
},
error: function(e) {
//called when there is an error
console.log(e.message);
}
});
});
});
</script>
因此,对于上面的所有代码,看来我已经接近我正在寻找的内容,但需要一些额外的指导。
对此的任何帮助将不胜感激。
谢谢,
jAC
【问题讨论】:
-
你的ajax在哪里?我们需要更多信息来帮助解决这个问题。或者你不是真的指ajax,而只是jQuery? (因为值在您的输入元素中)?
-
@cale_b 我现在没有任何工作。这就是我需要帮助的。我不知道它应该是ajax还是jquery。两者对我来说都很新。
-
@cale_b 添加了我尝试过的脚本(一个 jquery 和一个 ajax)
-
好的。建议编辑问题并删除 AJAX 标记,并对其进行更改,使其不显示“...将其添加到 PHP 变量...”。另外,请在下面查看我的答案 - 如果它不正确/不正确,请告诉我(如何)以便我可以修复它。
-
@cale_b 将它添加到 php 变量正是我需要的,这就是为什么我认为我可能需要 ajax