Jump to content

Field values are not updaing using AJAX and json


Recommended Posts

Hi all


I am trying to update field values using AJAX and json.


the ajax call is made using this code



<script type="text/javascript">
$(document).ready(function() {

$("#cboShipping").change(function() {
             var data = {
                 shippingOption: $('#cboShipping').val()
                     data: data,
                     success: function(data){


The PHP is


$zone = $_POST['shippingOption'];
$query="SELECT * FROM tbl_shipping_zones where id = $zone";
echo $query;

$shippingCost = $row['shipping_cost'];
echo $shippingCost;
$shippingZone = $row['name'];
echo $shippingZone;
$total = $_POST['subTotal'] + $shippingCost - $_POST['discount'];
echo $total;
'shippingCost' => $shippingCost,
'shippingCostDisplay' => displayAmount($shippingCost),
'shippingZone' => $shippingZone,
'total'        => $total,
'totalDisplay' => displayAmount($total)
echo json_encode($array);
The correct values are being generated in the php


Th fields and div containers I am trying to update are in the HTML below


<span id="total">£30.00</span>
<input name="hidDiscountType" type="hidden" id="hidDiscountType" value="" />
<input name="hidDiscountCode" type="hidden" id="hidDiscountCode" value="" />
<input name="hidDiscount" type="hidden" id="hidDiscount" value="0" />
<input name="hidTotalDiscount" type="hidden" id="hidTotalDiscount" value="0" />
<input name="hidTotal" type="hidden" id="hidTotal" value="30" /></td>

any guidance would be appreciated

Link to comment
Share on other sites

First thing you have to do is get rid of those echo()s and print_r()s. They'll mess up the AJAX.


Second, and more to the point, is

success: function(data){
The only variable you have to work with is data. There is no "shippingCost" or "totalDisplay" or "total". You have to get those values from with the data object.

success: function(data){
Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.