Forum Moderators: open

Message Too Old, No Replies

javascript call from ajax response

javascript call from ajax response

         

hozyali

2:29 pm on Jul 1, 2010 (gmt 0)

10+ Year Member



Hi,

I have a little problem with javascript.
I have a parent file which has a javascript function with some field calculations.

I also have a form/field for coupon validation which checks for if the coupon is valid or not. the validation process uses small ajax to return the result and if the result is success, I wanted to call the javascript function which I already have on parent form/file.

But for some reasons, it does not work not sure why?

any help?

Fotiman

2:32 pm on Jul 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




function someFieldCalculations() {...}
someAjax.onreadystatechange = function () {
// if success
someFieldCalculations();
};

hozyali

8:09 pm on Jul 1, 2010 (gmt 0)

10+ Year Member



thanks for your help. but I am not really sure how it works. I am kind of dumb in javascript.

hozyali

8:09 pm on Jul 1, 2010 (gmt 0)

10+ Year Member



sorry for double post.

hozyali

6:53 pm on Jul 2, 2010 (gmt 0)

10+ Year Member



can anyone help further with my situation?

daveVk

1:47 am on Jul 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Without knowing what you you are currently trying, it is hard to provide more specific sdvice.

Try posting the relevent code.

hozyali

7:09 am on Jul 3, 2010 (gmt 0)

10+ Year Member



thanks, here is the code stuff.

review.php - which is the parent file having coupon field/form

the function which is called upon coupon form submission is
function getCity(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
//document.getElementById('citydiv').innerHTML='Successful';
alert("good one.");
document.getElementById('citydiv').innerHTML=req.responseText;
//testalert();
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}


there is another function in this same file let's say this

function testalert(){
alert("success. it worked.");
}


this is the form submission code

<form action="" method="post" name="myForm" id="myForm">

<div id='citydiv'>
<label>
<input type="text" name="Coupon" id="Coupon"/>
</label>
<input name="sendamt" type="hidden" id="sendamt" value="<?php echo $amount; ?>" />
<input name="eventidentity" type="hidden" id="eventidentity" value="<?php echo $evid; ?>" />
<label>
<input name="Button" type="button" class="formButton" id="mySubmit" onClick="return getCity('validate_coupon.php?Coupon='+myForm.Coupon.value+'&sendamt='+myForm.sendamt.value+'&eventide='+myForm.eventidentity.value);" value="Update Total" />
</label>
</div>
</form>


the above form goes to validate_coupon.php to verify the coupon. Here is the code for validate_coupon.php
$couponcode = $_GET['Coupon'];
$ttlamt = $_GET['sendamt'];
$cpmevid = $_GET['eventide'];
if ($couponcode<>''){
if (ValidateCoupon($couponcode)) {
$valcpn = ValidateCoupon($couponcode);
//setcookie("couponcode",$valcpn['cpn_code'], time()+3600*24);
$cpnexp = $valcpn['cpn_expiry'];
$cpn_unit = $valcpn['cpn_unit'];
$cpn_amt = $valcpn['cpn_amount'];
$cpn_evid = $valcpn['cpn_evid'];
if ($cpn_unit==2) {
$discount_amt = ($ttlamt * $cpn_amt / 100);
//$discount_amt = $ttlamt - $discount_amt;
} else {
$discount_amt = $ttlamt - $cpn_amt;
}
//$todays = date($cpnexp, '%a, %d %b %Y');// as date;
$nowdate = date('M-d-Y');
$cpnexp=date("Y/m/d", strtotime($cpnexp));
$nowdate2=date("Y/m/d",strtotime($nowdate));

if ($cpnexp<$nowdate2) {
?>
<label><input type="text" name="Coupon" id="Coupon"/></label>
<input name="sendamt" type="hidden" id="sendamt" value="<?php echo $ttlamt; ?>" />
<input name="eventidentity" type="hidden" id="eventidentity" value="<?php echo $cpmevid; ?>" />
<label><input type="button" id="mySubmit" class="formButton" value="Submit" onclick="return getCity('validate_coupon.php?Coupon='+myForm.Coupon.value+'&sendamt'+myForm.sendamt.value+'&eventide='+myForm.eventidentity.value);">
</label>
<?php
echo '<font style="color:#990000;"><strong>The coupon has expired.</strong></font>';
} elseif ($cpn_evid<>0) {
if ($cpn_evid == $cpmevid) {
//echo 'Coupon applied. ';
session_register("cpn_id");
session_register("cpn_code");
session_register("cpn_amount");
#echo 'cpn_id';
$_SESSION['cpn_id']=$valcpn['cpn_id'];
#echo '<br>cpn_code';
$_SESSION['cpn_code']=$valcpn['cpn_code'];
#echo '<br>cpn_amount';
$_SESSION['cpn_amount']=$discount_amt;
/*echo '<script type="text/javascript" language="javascript1.2">testalert();</script>';*/
echo '<font style="color:#009900;"><strong>Coupon applied ('.$couponcode.')</strong></font>';
echo '<input type="hidden" name="cpn_amt" id="cpn_amt" value='.$cpn_amt.'>';
}


in the above code, if you notice the 2nd last line, it says Coupon Applied, that means the coupon was valid. so after this, I need to execute the javascript function to be called which I posted above testalert()

That is the problem, this testalert() function does not execute upon verification of the coupon.
However the getCity function calls perfectly fine if the coupon is not valid.

Please advise.

daveVk

11:53 am on Jul 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check reply content ( validated if contains cpn_amt ) and display required alert(s)

change

document.getElementById('citydiv').innerHTML=req.responseText;

to

var el=document.getElementById('citydiv');
el.innerHTML=req.responseText;
if ( el.getElementById('cpn_amt') !== null ) { testalert(); }

hozyali

12:41 pm on Jul 3, 2010 (gmt 0)

10+ Year Member



thanks for your help,
but it does not seem to be working either.

I also tried by completely removing the code
document.getElementById('citydiv').innerHTML=req.responseText;


but it does not affect. and works the same way

hozyali

12:41 pm on Jul 3, 2010 (gmt 0)

10+ Year Member



sorry for double post

daveVk

11:37 pm on Jul 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then try some more debug

alert("good one.");
var el=document.getElementById('citydiv');
el.innerHTML=req.responseText;
alert( el.innerHTML );
alert( el.getElementById('cpn_amt') );
if ( el.getElementById('cpn_amt') !== null ) { alert('valid'); }
else { alert( 'invalid' ) ; }

should get alerts
- good one
- reply html containing input id='cpn_amt'
- something other than "null"
- valid OR invalid

"Coupon applied" should be displayed.

hozyali

8:53 am on Jul 4, 2010 (gmt 0)

10+ Year Member



Thanks daveVK for your help.

the problem I am seeing is, the returning script is just not calling anything inside the function getCity..

daveVk

10:08 am on Jul 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I take it you are getting no alerts, check you server log file to see if request recd and 200 (OK) reply returned.

Do you get any alerts in invalid coupon case ?

Make sure browser set to report javascript errors.

hozyali

10:56 am on Jul 4, 2010 (gmt 0)

10+ Year Member



yes, when the coupon is not valid, it sends proper reply via php echo.

I will check the stuff you suggested and get back.

thanks again for your help

od3n

4:22 am on Aug 19, 2010 (gmt 0)

10+ Year Member



how to see the code?