Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery更改select price,使用php函数响应price_Php_Jquery_Variables_Select - Fatal编程技术网

使用jquery更改select price,使用php函数响应price

使用jquery更改select price,使用php函数响应price,php,jquery,variables,select,Php,Jquery,Variables,Select,我正在创建一个购物车,我很难让jquery和php正常工作。我有一个名为displayAmount($price)的函数,目前只需将数字格式化为2个小数点,并在其中添加英镑符号。我希望在将来看到允许用户更改其本地货币时,保留此功能,它将使用此功能计算并显示正确的价格和货币 我希望能够在查看产品页面上显示价格,并在用户更改选择框中的选项时更改价格 所以我有这个: jQuery: $("select").live('change',function () { var price = 0;

我正在创建一个购物车,我很难让jquery和php正常工作。我有一个名为
displayAmount($price)
的函数,目前只需将数字格式化为2个小数点,并在其中添加英镑符号。我希望在将来看到允许用户更改其本地货币时,保留此功能,它将使用此功能计算并显示正确的价格和货币

我希望能够在查看产品页面上显示价格,并在用户更改选择框中的选项时更改价格

所以我有这个:

jQuery:

$("select").live('change',function () {
      var price = 0;
      $("select option:selected").each(function () {
        price += parseFloat($(this).data('price'));
      });
      $('#productPriceLabel').html(price);
    });
HTML:

第二次编辑:(完整代码)

//从数据库获取价格
$priceSQL=“选择*
来自shopProductsPrices
其中pd_id=“$productId”;
$priceResult=dbQuery($priceSQL);
$priceOptions=“”;
$productLowestPrice=10500;
而($priceRow=dbFetch($priceResult)){
如果($productLowestPrice>$priceRow['spp_price']){$productLowestPrice=$priceRow['spp_price'];}
$priceOptions.=''.$priceRow['spp_name'.'';
}
如果($productLowestPrice==10500){$productLowestPrice=0;}
包裹:
数量:
x1
x2
x3
$(“.productSelectOptions”).live('change',function(){
var价格=0;
$(“.productSelectOptions:selected”).each(函数(){
price+=parseFloat($(this.data('price'));
$.ajax({
类型:“POST”,
数据类型:“html”,
url:'library/common.php',
数据:'&price='+price,
成功:函数(返回数据){
$('#productPriceLabel').html(返回数据);
},
错误:函数(){
$('#productPriceLabel').html('Error Formatting');
}
});//结束Ajax
});
});

以下是我无法在评论中添加的内容:

我将“更改时选择”功能更改为仅在“productSelectOptions”更改时执行。否则,它将在更改任何选择选项时执行。那是你想要的吗

下面是修改后的jQuery代码,它将把新价格发送到PHP页面,并使用PHP函数进行格式化

$(".productSelectOptions").live('change',function () {
  var price = 0;
  $(".productSelectOptions option:selected").each(function () {
    price += parseFloat($(this).data('price'));
    $.ajax({
      type: "POST",
      dataType: "html",
      url: 'formatdisplayAmount.php',
      data: '&price='+price,
      success: function(returnedData){
        $('#productPriceLabel').html(returnedData);
      },
      error: function(){
        $('#productPriceLabel').html('Error Formatting');
      }
    }); //END Ajax
  });

});
您的“formatdisplayAmount.php”将包含格式化函数,如下所示:

function displayAmount($amount)
{
  $xamount = str_replace('.', '', $amount);
  $type = gettype($xamount);

  if($type = integer) {
    global $Config;
    return $Config['currency'] . number_format($amount, 2);
  } else {
    return $amount;
  }
}

echo displayAmount($_POST['price']);

在此处发布php函数。添加到原始post Sorry当您使用jQuery时,与用户的交互发生在客户端(在用户的浏览器中)。所有PHP脚本都是在服务器端处理的(换句话说,PHP是在页面传递到客户端之前处理的)。如果您希望让用户在页面上更改货币下拉列表,并使用PHP脚本重新格式化定价,则需要使用Ajax。看:如果你愿意,我可以试着详细说明,但我不能作为评论。谢谢。我试过了,但没用。价格没有通过邮政传递?这是我正在处理的页面的URL。也许这会有帮助。编辑:我还编辑了我的原始文章,将代码作为一个整体包含进来,但不包括当前与您文章中相同的php函数。因此library/common.php没有收到通过$.ajax发送的$u post变量“price”?你能加上:
print\r($\u POST)console.log(returnedData)到Ajax成功函数,让我知道调试说明了什么?
// Get prices from database
                    $priceSQL = "SELECT *
                                FROM shopProductsPrices
                                WHERE pd_id = '$productId'";    
                    $priceResult = dbQuery($priceSQL);
                    $priceOptions = "";
                    $productLowestPrice = 10500;
                    while($priceRow = dbFetch($priceResult)) {
                        if($productLowestPrice > $priceRow['spp_price']) { $productLowestPrice = $priceRow['spp_price']; }
                        $priceOptions .= "<option data-price='+".$priceRow['spp_price']."'>".$priceRow['spp_name']."</option>";
                    }
                    if($productLowestPrice == 10500) { $productLowestPrice = 0; }







<table style="border: none; width: 300px;">
                                                <tr>
                                                    <td>
                                                        <span id="productContentItem">Package:</span>
                                                    </td>
                                                    <td>
                                                        <select class='productSelectOptions'>
                                                            <?= $priceOptions ?>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <span id="productContentItem">Quantity:</span>
                                                    </td>
                                                    <td>
                                                        <select class='productSelectOptionsQty'>
                                                            <option value="1">x1</option>
                                                            <option value="2">x2</option>
                                                            <option value="3">x3</option>
                                                        </select>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                    </td>
                                                    <td>
                                                        <?= $productAddToBasket ?>
                                                    </td>
                                                </tr>
                                            </table>

                                                        <span id='productPriceLabel'><?= displayAmount($productLowestPrice) ?></span>
<script>
    $(".productSelectOptions").live('change',function () {
      var price = 0;
      $(".productSelectOptions option:selected").each(function () {
        price += parseFloat($(this).data('price'));
        $.ajax({
          type: "POST",
          dataType: "html",
          url: '<?= SERV_ROOT ?>library/common.php',
          data: '&price='+price,
          success: function(returnedData){
            $('#productPriceLabel').html(returnedData);
          },
          error: function(){
            $('#productPriceLabel').html('Error Formatting');
          }
        }); //END Ajax
      });

    });
</script>
$(".productSelectOptions").live('change',function () {
  var price = 0;
  $(".productSelectOptions option:selected").each(function () {
    price += parseFloat($(this).data('price'));
    $.ajax({
      type: "POST",
      dataType: "html",
      url: 'formatdisplayAmount.php',
      data: '&price='+price,
      success: function(returnedData){
        $('#productPriceLabel').html(returnedData);
      },
      error: function(){
        $('#productPriceLabel').html('Error Formatting');
      }
    }); //END Ajax
  });

});
function displayAmount($amount)
{
  $xamount = str_replace('.', '', $amount);
  $type = gettype($xamount);

  if($type = integer) {
    global $Config;
    return $Config['currency'] . number_format($amount, 2);
  } else {
    return $amount;
  }
}

echo displayAmount($_POST['price']);