Solved

how can i fix this ajax xml response?

Posted on 2008-10-01
5
271 Views
Last Modified: 2012-05-05
Hello i need your help to fix this code , the purpose is to set a ajax xml request and get back an xml response where each xml TAGid can be inner in a different div id .
var xmlHttp;

	function check()

	{

		try

		{    

			// Firefox, Opera 8.0+, Safari    

			xmlHttp=new XMLHttpRequest();    			

		}

		catch (e)

		{   

		 // Internet Explorer    

			try

			{

				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      

			}

			catch (e)

			{      

				try

				{        

					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        

				}

				catch (e)

				{

					alert("Your browser does not support AJAX!");       

					return false;        

				} 	    

			}    

		}

		return true;

	}

	

<script type="text/javascript">

	function coupon()

	{  

		if(check())

		{

			var str;

			str="";

			var coupon0,coupon, price0, price;

			coupon0=document.getElementById('coupon');

			coupon=coupon0.value;

			price0=document.getElementById('price');

			price=price0.value;

			str="coupon=" + coupon +"&price=" + price;

			document.getElementById("resultcoupon").innerHTML='<img src=images/ajax-loader.gif width=43 height=11>';

			xmlHttp.onreadystatechange=function()

			{

				if(xmlHttp.readyState == 4)

				{		

					var result, ob; ob1, result ;					

					result=xmlHttp.responseXML;

					ob=xml.getElementsByTagName("coupon");

ob1=xml.getElementsByTagName("price");					document.getElementById("resultcoupon").innerHTML=ob;

document.getElementById("resultcoupon").innerHTML=price;

				}

			}

			xmlHttp.open("GET","verifycoupon.php?"+str,true);

			xmlHttp.send("");

		}	

	}

</script>
 
 

-----------------

php side
 
 

<?php

session_start(); 

header ("content-type: text/xml");

include_once("newincludes/sql.php");

echo '<?xml version="1.0"?>';

echo "<coupon>";

if (isset($_REQUEST['coupon']))

{

   $price=$_REQUEST['price'];

   $sqlcoupon=sql('customer' , "customer_id=$_SESSION[id]");

   $rowcoupon=mysql_fetch_array($sqlcoupon);

   

     if ($rowcoupon['coupon'] == 0)

     {

      echo" N/A!";

     }

	 

     if ($rowcoupon['coupon'] != $_REQUEST['coupon'])

	 {

	  echo "NOT CORRECT!";

	 }

	 

	 if ($rowcoupon['coupon'] == $_REQUEST['coupon'])

	 {

	 $cal=$price*$rowcoupon['coupon_value']/100;

	 echo"<font color=#ff0000><b>-</b></font>" ; echo $cal ; echo "USD";

	 echo "</coupon>";

	 }

echo "<price>";

echo $price;

echo "</price>";

}

?>

Open in new window

0
Comment
Question by:Audiominds
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
Comment Utility
try:
<?php
session_start(); 
header ("content-type: text/xml");
include_once("newincludes/sql.php");
echo '<?xml version="1.0"?><request>';
if (isset($_REQUEST['coupon']))
{
	echo "<coupon><![CDATA[";
	$price=$_REQUEST['price'];
	$sqlcoupon=sql('customer' , "customer_id=$_SESSION[id]");
	$rowcoupon=mysql_fetch_array($sqlcoupon);
   
	if ($rowcoupon['coupon'] == 0)
     {
		echo "N/A!";
     } 
	elseif ($rowcoupon['coupon'] != $_REQUEST['coupon'])
	{
		echo "NOT CORRECT!";
	}
	elseif ($rowcoupon['coupon'] == $_REQUEST['coupon'])
	{
		$cal=$price*$rowcoupon['coupon_value']/100;
		echo"<font color=#ff0000><b>-</b></font>";
		echo $cal;
		echo "USD";
	}
	echo "]]></coupon>";
	echo "<price>$price</price>";
}
echo "</request>";
?> 

var xmlHttp;
      function check()
      {
            try
            {    
                  // Firefox, Opera 8.0+, Safari    
                  xmlHttp=new XMLHttpRequest();                      
            }
            catch (e)
            {   
             // Internet Explorer    
                  try
                  {
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
                  }
                  catch (e)
                  {      
                        try
                        {        
                              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
                        }
                        catch (e)
                        {
                              alert("Your browser does not support AJAX!");       
                              return false;        
                        }           
                  }    
            }
            return true;
      }
      
<script type="text/javascript">
      function coupon()
      {  
            if(check())
            {
                  var str;
                  str="";
                  var coupon0,coupon, price0, price;
                  coupon0=document.getElementById('coupon');
                  coupon=coupon0.value;
                  price0=document.getElementById('price');
                  price=price0.value;
                  str="coupon=" + coupon +"&price=" + price;
                  document.getElementById("resultcoupon").innerHTML='<img src=images/ajax-loader.gif width=43 height=11>';
                  xmlHttp.onreadystatechange=function()
                  {
                        if(xmlHttp.readyState == 4)
                        {            
                              var result, ob; ob1, result ;                              
                              result=xmlHttp.responseXML.documentElement;
                              ob=result.getElementsByTagName("coupon")[0].firstChild.nodeValue;
						ob1=result.getElementsByTagName("price")[0].firstChild.nodeValue;                              document.getElementById("resultcoupon").innerHTML=ob;
						document.getElementById("resultcoupon").innerHTML=price;
                        }
                  }
                  xmlHttp.open("GET","verifycoupon.php?"+str,true);
                  xmlHttp.send("");
            }      
      }
</script>

Open in new window

0
 

Author Comment

by:Audiominds
Comment Utility
Thanks Here the code paste it again , the first TAG coupon work but the tag price didn't show and give me an error ! so how can i retrieve correctly the <price> tag and inserted in the DIV tag, thanks Hielo i much appreciate your permanent help.

don't miss the check() i remove from my script below as i included in the header .
<?php

session_start(); 

header ("content-type: text/xml");

include_once("newincludes/sql.php");

echo '<?xml version="1.0"?><request>';

if (isset($_REQUEST['coupon']))

{

        echo "<coupon><![CDATA[";

        $price=$_REQUEST['price'];

        $sqlcoupon=sql('customer' , "customer_id=$_SESSION[id]");

        $rowcoupon=mysql_fetch_array($sqlcoupon);

   

        if ($rowcoupon['coupon'] == 0)

     {

                echo "N/A!";

     } 

        elseif ($rowcoupon['coupon'] != $_REQUEST['coupon'])

        {

                echo "NOT CORRECT!";

        }

        elseif ($rowcoupon['coupon'] == $_REQUEST['coupon'])

        {

                $cal=$price*$rowcoupon['coupon_value']/100;

                echo"<font color=#ff0000><b>-</b></font>";

                echo $cal;

                echo "USD";

        }

        echo "]]></coupon>";

        echo "<price><![CDATA[".$price."]]></price>";

}

echo "</request>";

?>
 
 
 
 
 
 

<script type="text/javascript">

 function coupon()

      {  

            if(check())

            {

                  var str;

                  str="";

                  var coupon0,coupon, price0, price;

                  coupon0=document.getElementById('coupon');

                  coupon=coupon0.value;

                  price0=document.getElementById('price');

                  price=price0.value;

                  str="coupon=" + coupon +"&price=" + price;

                  document.getElementById("resultcoupon").innerHTML='<img src=images/ajax-loader.gif width=43 height=11>';

                  xmlHttp.onreadystatechange=function()

                  {

                        if(xmlHttp.readyState == 4)

                        {            

                              var result, ob , ob1 ;                              

                              result=xmlHttp.responseXML.documentElement;

                              ob=result.getElementsByTagName("coupon")[0].firstChild.nodeValue;

                              ob1=result.getElementsByTagName("price")[0].firstChild.nodeValue;                              

							  document.getElementById("resultcoupon").innerHTML=ob;

							  document.getElementById("price").innerHTML=ob1;

                        }

                  }

                  xmlHttp.open("GET","verifycoupon.php?"+str,true);

                  xmlHttp.send("");

            }      

      }

</script>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
Comment Utility
from your original post, based on this:
                  price0=document.getElementById('price');
                  price=price0.value;

I suspect you have:

<input id="price"...>
 

input elements do not have an innerHTML property. They have a value. So instead of:

document.getElementById("price").innerHTML=ob1;
 

you need:

document.getElementById("price").value=ob1;

Open in new window

0
 

Author Comment

by:Audiominds
Comment Utility
I think this is not our problem here listed again the files hope you can get this work !! thanks Hielo
PHP

---------------

<?php

session_start(); 

header ("content-type: text/xml");

include_once("newincludes/sql.php");

echo '<?xml version="1.0"?><request>';

if (isset($_REQUEST['coupon']))

{

	echo "<coupon><![CDATA[";

	$price=$_REQUEST['price'];

	$sqlcoupon=sql('customer' , "customer_id=$_SESSION[id]");

	$rowcoupon=mysql_fetch_array($sqlcoupon);

   

	if ($rowcoupon['coupon'] == 0)

     {

		echo "N/A!";

     } 

	elseif ($rowcoupon['coupon'] != $_REQUEST['coupon'])

	{

		echo "NOT CORRECT!";

	}

	elseif ($rowcoupon['coupon'] == $_REQUEST['coupon'])

	{

		$cal=$price*$rowcoupon['coupon_value']/100;

		echo"<font color=#ff0000><b>-</b></font>";

		echo $cal;

		echo "USD";

	}

	echo "]]></coupon>";

	echo "<price>".$price".</price>";

}

echo "</request>";

?> 
 
 
 

HTML

------------------------------------------------

<?php

include ('headertop.php'); include ('header.php');

?>

<script type="text/javascript">

      function coupon()

      {  

            if(check())

            {

                  var str;

                  str="";

                  var coupon0,coupon, price0, price;

                  coupon0=document.getElementById('coupon');

                  coupon=coupon0.value;

                  price0=document.getElementById('price');

                  price=price0.value;

                  str="coupon=" + coupon +"&price=" + price;

                  document.getElementById("resultcoupon").innerHTML='<img src=images/ajax-loader.gif width=43 height=11>';

                  xmlHttp.onreadystatechange=function()

                  {

                        if(xmlHttp.readyState == 4)

                        {            

                              var result, ob; //ob1;                           

                              result=xmlHttp.responseXML.documentElement;

                              ob=result.getElementsByTagName("coupon")[0].firstChild.nodeValue;

						      //ob1=result.getElementsByTagName("price")[0].firstChild.nodeValue;                              

							  document.getElementById("resultcoupon").innerHTML=ob;

						      //document.getElementById("resultprice").innerHTML=ob1;

                        }

                  }

                  xmlHttp.open("GET","verifycoupon.php?"+str,true);

                  xmlHttp.send("");

            }      

      }
 

</script>

<table  class="normal-font" style="width: 800px" align="center" cellpadding="3">

	<tr>

		<td class="header-cart" colspan="2">

		<img alt="checkout" src="images/checkout.jpg" width="150" height="30"></td>

	</tr>

	<tr><td colspan="2"></td></tr>

	<tr>

		<td bgcolor="gray" style="font-family:Tahoma ; font-size:x-small ; color:white" colspan="2" ><strong>YOUR CART</strong></td>

	</tr>

	<tr>

		<td colspan="2">

		<table class="normal-font" style="width: 100%">

			<tr>

				<td width="545" align="center" style="	border-bottom-style: solid; border-bottom-width: 1px; width: 545px;" ><strong>Product details </strong></td>

		    <td width="88" align="center"  style="border-bottom-style: solid; border-bottom-width: 1px;" ><strong>

				U.price</strong></td>

			  <td width="44" align="center"  style=" border-bottom-style: solid; border-bottom-width: 1px; " ><strong>Qty</strong></td>

			  <td width="91" align="center" style="border-bottom-style: solid; border-bottom-width: 1px; "   ><strong>T.price</strong></td>

		  </tr>

            <?php 

			$myTotal=0;

$sqlcart=sql('customer_cart , products', "customer_cart.customer_id=$_SESSION[id] AND customer_cart.productcart_id=products.product_id  AND customer_cart.statue='cart' ");

while ($rowcart=mysql_fetch_array($sqlcart))

{

?>

			<tr>

				<td style="width: 545px ; border-bottom-style:dashed; border-bottom-width: 1px; "   ><span style="width: 348px"><?php echo "<b>"; echo "<a href=product.php?productid="; echo $rowcart['product_id']; echo ">"; echo ucfirst($rowcart['title']); echo"</a></b><br>"; echo $rowcart ['overview']; ?></span></td>

				<td align="center" style="border-bottom-style:dashed; border-bottom-width: 1px;" ><?php

      if ($country=="Lebanon" || $country=="International")

	  {

	  $price=$rowcart['discountprice'];

	  }

	  else

	  {

	  $price=$rowcart['usadiscountprice'];

	  }

	  echo $price;

	  ?></td>

				<td align="center" style="border-bottom-style:dashed; border-bottom-width: 1px;" ><?php echo $rowcart['product_qty'] ?> </td>

				<td align="center" style="border-bottom-style:dashed; border-bottom-width: 1px;"  ><?php  $qtyprice=$rowcart['product_qty']*$price; echo $qtyprice ; echo " USD";  $myTotal +=$qtyprice; ?></td>

			</tr>

            <?php } //close shopping cart listing array ?>

			<tr>

				<td colspan="3"  align="right" ><strong>Total price (excluding shipping):</strong></td>

				<td align="center"   ><b><?php

		 echo $myTotal;  echo " USD"; ?><input id="price" type="hidden" value="<?php echo $myTotal; ?>"  /> </b></td>

			</tr>

			<tr>

				<td colspan="3"   >

				<table  align="right">

					<tr>

						<td style="color:orange" class="normal-font">If you have a discount coupon 

						enter here </td>

						<td><input id="coupon" class="normal-font" style=" color:orange " type="text"></td>

						<td>

						<img alt="apply" src="images/apply.jpg" width="45" height="22" onclick="coupon()"> </td>

					</tr>

				</table>				</td>

				<td align="center"   ><div id="resultcoupon"></div></td>

			</tr>

			<tr>

				<td align="right" colspan="3"   ><strong>Total price after discount</strong></td>

				<td align="center"   ><div id="resultprice"></div></td>

			</tr>

			<tr>

				<td align="right" colspan="3"   ><strong>Shipping fee:</strong></td>

				<td align="center"   ></td>

			</tr>

			<tr>

				<td  style="color:red" align="right" colspan="3"   ><strong>Final total price 

				including shipping :</strong> </td>

				<td align="center"   ></td>

			</tr>

			</table>

	  </td>

	</tr>

	<tr>

		<td bgcolor="gray" style="font-family:Tahoma ; font-size:x-small ; color:white" colspan="2">

		<strong>SHIPPING DETAILS</strong></td>

	</tr>

	<tr>

		<td>

		<!--start table for shipping details-->

		<table class="normal-font" style="width: 400px">

			<tr>

				<td align="right" style="width: 96px"><strong>Company:</strong></td>

				<td>

				<input name="scompany" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px"><strong>Land line:</strong></td>

				<td>

				<input name="slandline" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px"><strong>Mobile line:</strong></td>

				<td>

				<input name="smobileline" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px"><strong>Adress:</strong></td>

				<td>

				<input name="saddress" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px"><strong>City:</strong></td>

				<td>

				<input name="scity" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px"><strong>State / Town: 

				</strong> </td>

				<td>

				<input name="sstate" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px"><strong>Zip code :</strong></td>

				<td>

				<input name="szipcode" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px"><strong>Country :</strong></td>

				<td>

				<input name="company" type="text" class="normal-font" style="width: 273px"></td>

			</tr>

			<tr>

				<td align="right" style="width: 96px">&nbsp;</td>

				<td >

				<img alt="update shipping fee" src="images/updateshipping.jpg" width="170" height="22"></td>

			</tr>

		</table>

		<!--end table for shipping details-->

		</td>

		<td>&nbsp;</td>

	</tr>

	<tr>

		<td bgcolor="gray" colspan="2" class="style1"><strong>PAYMENT METHOD</strong></td>

	</tr>

	<tr>

		<td>

		<table class="normal-font" style="width: 400px">

			<tr>

				<td>Kindly select your payment method:</td>

			</tr>

			<tr>

				<td><form method="post">

	<select class="normal-font" name="Select1">

	<option></option>

	<option selected="selected">Select</option>

	<option value="Bank wire">Bank wire</option>

	<option value="Check">Check</option>

	<option value="Credit card">Credit card</option>

	<option value="Paypal">Paypal</option>

	<option value="Moneygram">Moneygram</option>

	<option value="Western union ">Western union</option>

	</select></form>

</td>

			</tr>

			</table>

		</td>

		<td>&nbsp;</td>

	</tr>

	<tr><td align="right" colspan="2">

		<img alt="complete your order" src="images/completeorder.jpg" width="239" height="39"></td></tr>

</table>

<?php

include ('footer.php');

?>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
This:

echo "<price>".$price".</price>";
 

has a syntax error. Change it back to what I had before- it works, I tested it:

echo "<price><![CDATA[" . $price . "]]></price>";
 

On your original post you had:

>>document.getElementById("resultcoupon").innerHTML=ob;

>>document.getElementById("resultcoupon").innerHTML=price;
 

incorrectly adding two different results to the same element "resultcoupon". Based on my first post, most likely what you need is:

document.getElementById("resultcoupon").innerHTML=ob;

document.getElementById("resultprice").innerHTML=price;

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now