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

Edited by requinix
fixed broken bbcode
Link to post
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 post
Share on other sites

The query is vulnerable to SQL injection attacks. Learn how to use prepared statements.


Actually, the whole approach with all those hidden fields and client-side parameters is strange. Do you want the user to set an arbitrary price? Or shouldn't the price be calculated by the server after the user has chosen a shipping option?

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.