jQuery.post not working when using data type json
Posted
by
swift
on Stack Overflow
See other posts from Stack Overflow
or by swift
Published on 2013-06-25T04:19:45Z
Indexed on
2013/06/25
4:21 UTC
Read the original article
Hit count: 112
I have been trying to utilize json in this jQuery.post because I need to return two values from my executed php. The code was working when I was not implementing json. I need to see if a promo code entered is valid for a particular broker. The two variables I need back are the instant message whether or not it's valid (this is displayed to the user) and I need to update a hidden field that will be used later while updating the database.
The jQuery.post does not seem to be firing at all, but the code directly above it (the ajax-loader.gif) is working.
I did re-write the whole thing at one point using jQuery.ajax, and had issues there too. Granted, I have probably been looking at this too long and have tried to re-write too many times, but any help is greatly appreciated!!
Here's the jQuery.post
<!-- Below Script is for Checking Promo Code Against Database-->
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#promocode").keyup(function (e) {
//removes spaces from PromoCode
jQuery(this).val(jQuery(this).val().replace(/\s/g, ''));
var promocode = jQuery(this).val();
var brokerdealerid = document.getElementById("BrokerDealerId").value;
if(promocode.length > 0 ){
jQuery("#promo-result").html('<img src="../imgs/ajax-loader.gif" />');
jQuery.post(
'../check_promocode.php',
{promocode:promocode, brokerdealerid:brokerdealerid},
function(data) {
$("#promo-result").html(data.promoresult);
$("#promo-result-valid").html(data.promovalid);
},
"json");
}
});
});
</script>
<!-- End Script is for Checking Promo Code Against Database-->
Here's relevant code from check_promocode.php:
//sanitize incoming parameters
if (isset($_POST['brokerdealerid'])) $brokerdealerid = sanitizeMySQL($_POST['brokerdealerid']);
$promocode = sanitizeMySQL($promocode);
//check promocode in db
$results = mysql_query("SELECT PromotionCodeIdentifier FROM PromotionCode WHERE PromotionCodeIdentifier='$promocode' AND BrokerDealerId='$brokerdealerid' AND PromotionCodStrtDte <= CURDATE() AND PromotionCodExpDte >= CURDATE()");
//return total count
$PromoCode_exist = mysql_num_rows($results); //total records
//if value is more than 0, promocode is valid
if($PromoCode_exist)
{
echo json_encode(array("promoresult"=>"Promotion Code Valid", "promovalid"=>"Y"));
exit();
}else{
echo json_encode(array("promoresult"=>"Invalid Promotion Code", "promovalid"=>"N"));
exit();
}
© Stack Overflow or respective owner