php - Using jQuery/Ajax to get data from database to apply discount -
php - Using jQuery/Ajax to get data from database to apply discount -
i'm wanting know best way create form not utilize button homecoming valid discount code , apply cost , show on page.
<input type="text" name="discount_code" value="" class="text" id="discount_code" title="discount code">
this posts same page.
i understand onchange , ajax used not sure how this, looking help/examples on please.
many thanks.
fully working script, developed lastly week only, need understand steps, not going explain.
<script type="text/javascript"> function applycouponcode() { $("#csuc, #cerr").hide(); $("#csuc, #cerr").html(''); var coupon = $("#ccode").val(); if(coupon!='') { $("#cload").show(); $.post('ajax/coupon.php', { coupon: coupon }, function(data) { /*optional stuff after success */ switch(data) { case 'a': $("#cerr").html('please come in valid coupon code'); $("#cerr").show(); $("#cload").hide(); break; case 'b': $("#cerr").html('you have used coupon code'); $("#cerr").show(); $("#cload").hide(); case 'c': $("#cerr").html('please come in valid coupon code'); $("#cerr").show(); $("#cload").hide(); default: var coupon = data; // calculate %age var cost = $("#event_subtotal_price").val(); var pctge = parsefloat(price) * ( parsefloat(coupon) / 100 ); var npric = parsefloat(price) - parsefloat(pctge); pctge = pctge.tofixed('2'); // total cost dicount npric = npric.tofixed('2'); // new cost after discount // display discount in totalling menu $("#csuc").html('coupon applied.'); $("#csuc").show(); $("#cload").hide(); } }); } else { $("#cerr").html('please come in coupon code'); $("#cerr").show(); } } </script> <div style="margin: 15px 85px;"> <p> coupon code <br /> <span><input type="text" name="ccode" id="ccode" value="" /></span> <span id="cbtn"><input type="button" value="apply" style="margin-bottom: 5px;" onclick="applycouponcode();"></span> <span id="cload" style="display:none;"><img style="margin-bottom: -3px;" src="loader.gif"></span> <span id="csuc" style="color:#016b06; display:none;"></span> <span id="cerr" style="color:#e40303; display:none;"></span> </p> </div>
coupon.php
<?php $coupon = $_request['coupon']; $user = $_session['picturebooth_customer_id']; // coupon detail $coupon_count=$wpdb->get_var("select count(*) `table_coupon` `coupan_code` = '$coupon' , `status`='1'"); if($coupon_count) { $result = $wpdb->get_row("select * `table_coupon` `coupan_code` = '$coupon'"); $discount = $result->discount; $today = strtotime(date("y-m-d")); $from = strtotime($result->from_date); $to = strtotime($result->to_date); $total = $result->total_number; $used = $result->used_number; // checking if user has has applied coupon $uc_count = $wpdb->get_var("select count(*) `user_coupon` `user_id` = '$user' , `coupon_id`='$result->id'"); if($uc_count) { echo 'b'; } elseif( $today < $from ) { echo 'c'; } elseif( ($today > $to) || ($used >= $total) ) { $wpdb->update('wp_coupons', array( 'status' => '0'), array( 'id' => $result->id )); echo 'c'; } else { $wpdb->query("update `table_coupon` set used_number = used_number+1 `id` = '$result->id'"); $wpdb->query("insert `user_coupon` set `user_id` = '$user', `coupon_id`='$result->id', `date_added` = now()"); echo $discount; } } else { echo 'a'; }
php jquery ajax
Comments
Post a Comment