Connecting Tech Pros Worldwide Forums | Help | Site Map

Not getting correct return values from Ajax script

Newbie
 
Join Date: Apr 2008
Posts: 14
#1: Apr 9 '08
anyone have any idea why i am not getting any return values? it will return the words just not the numbers?

HTML
[HTML]
<HTML>
<head>
<title>Zellers Carpeting Cost Estimate</title>
</head>

<script>

var http = createRequestObject();

function createRequestObject() {

var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

function CarpetJob(argW,argL, argType, argPad, argReq) {

var width;
var length;
var carpet;
var padding;
var install;

width=document.carpetform.width.value;
length=document.carpetform.length.value;
carpet=document.carpetform.carpet.value;
padding=document.carpetform.padding.value;
install=document.carpetform.install.value;

http.open('get', "carpet.php?width=" + argW +"&length="+ argL +"&carpet=" + argType +"&padding="+ argPad +"&install="+ argReq);
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {

if(http.readyState == 4){

document.getElementById("results").innerHTML = http.responseText;
}
}

</script>
<body>
<div id="banner">
<h1>Zellers Carpeting</h1>
<h2>Cost Estimate</h3>
</div>

<div class="instructions">Enter all the relevant parameters and click submit for an estimate of the cost of carpeting your room</div>

<div id="userinput">
<form method="get" name="carpetform" action="#">
<table>
<tr>
<td>Room Dimensions (in ft):</td>
<td><input type="text" name="width"> x <input type="text" name="length"></td>
</tr>
<tr>
<td ><strong>Type of Carpet:</strong></td>
<td>
<select name="carpet">
<option value="">Choose Carpet Type</option>
<option value="B">Budget</option>
<option value="S">Standard</option> <option value="P">Premium</option>
</select>
</td>
</tr>
<tr>
<td ><strong>Type of Padding:</strong></td>
<td>

<select name="padding">
<option value="">Choose Padding</option>
<option value="S">Standard</option>
<option value="P">Premium</option>
</select>

</td>
</tr>
<tr>
<td><strong>Installation Required:</strong></td>

<td>Yes<input type=radio name="install" value=y>&nbsp;&nbsp;&nbsp;No

<input type=radio name="install" value=n></td>
</tr>
<tr>
<td align=center colspan=2><input type="button" value="Submit" onclick="CarpetJob();"></td>
</tr>
</table>
</form>
</div>

<div id="results">

</div>
</body>

</HTML>


PHP PAGE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>Carpet</title>

<link type="text/css" rel="stylesheet" href="carpet.css" />
</head>

<body>
<h1>The Summary of Your Carpet Order:</h1>

<div id="all">

<?php

$W=$_GET['width'];

$L=$_GET['length'];

$carpet=$_GET['carpet'];

$padding=$_GET['padding'];

$install=$_GET['install'];


//The formula for 20% more

$area= $W * $L;

$extra=$area * .20 + $area;


?>

<br />
<br />

<?php

//The carpet price

if($carpet=="B"){
$c="1.00";

}

if($carpet=="S"){
$c="1.50";
}

if($carpet=="P"){
$c="2.00";
}

$carpetCost= $c * $extra;

print ("Your $area ft. of Carpet will cost $$carpetCost");
?>

<br />
<br />

<?php
//Padding Cost

if($padding=="S"){
$cp=.35;
}

if($padding=="P"){
$cp=.50;
}

$cPadding=$cp * $extra;

print ("Your Padding will cost $$cPadding")
?>

<br />
<br />

<?php

//The Install cost

if($install=="n"){
$in=.00;
}

if($install=="y"){
$in=2.00;
}

$installCost=$in * $extra;

print ("Your install cost will be $$installCost");

?>

<br />
<br />

<?php

$salesTax=($carpetCost+$cPadding)*.07;

print ("Your Sales Tax will be $$salesTax");
?>

<br />
<br />

<?php

$totalCost=$carpetCost+$cPadding+$installCost+$sal esTax;

print ("Your Grand Total for your carpet is $$totalCost");
?>

<br />
<br />
<?php

if ($carpet=="B"){

print (' <img src="budget.jpg" alt="Budget Carpet"/>' );
}

if ($carpet=="S"){

print ('<img src="standard.jpg" alt="Standard Carpet"/>');

}

if ($carpet=="P"){

print ('<img src="premium.jpg" alt="Premium Carpet"/>');

}

?>

<?php

if ($install=="y"){

print ('<img src="logo.jpg" alt="logo"/>');
}

if ($install=="n"){

}

?>
</div>

</body>

</html>[/HTML]

Newbie
 
Join Date: Apr 2008
Posts: 14
#2: Apr 9 '08

re: Not getting correct return values from Ajax script


sorry to further clarify...

when i push submit it will return something like

the cost of your carpet is 0

the area is 0

it will get the php page, but not do the calculations...
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Apr 10 '08

re: Not getting correct return values from Ajax script


Since you're not passing the arguments to CarpetJob(), use the variables you've created within the function:
Expand|Select|Wrap|Line Numbers
  1. http.open('get', "carpet.php?width=" + width +"&length="+ length +"&carpet=" + carpet +"&padding="+ padding +"&install="+ install);
Newbie
 
Join Date: Apr 2008
Posts: 14
#4: Apr 14 '08

re: Not getting correct return values from Ajax script


what values would i put in there?
Newbie
 
Join Date: Apr 2008
Posts: 14
#5: Apr 14 '08

re: Not getting correct return values from Ajax script


I posted an earlier question asking about getting all 0's in my return values, could someone help me out with this problem...

HTML

[HTML]<HTML>
<head>
<title>Zellers Carpeting Cost Estimate</title>
</head>

<script>

var http = createRequestObject();

function createRequestObject() {

var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}

function CarpetJob(argWidth, argLength, argType, argPad, argReq){
alert("****")

http.open('get', "carpet.php?Width=" +argWidth+"&length=" +argLength+"&carpet=" +argType+"&padding=" +argPad+"&install=" +argReq);
http.onreadystatechange = handleResponse;
http.send(null);
}

function handleResponse() {

if(http.readyState == 4){

document.getElementById("results").innerHTML = http.responseText;

}
}

</script>
<body>
<div id="banner">
<h1>Zellers Carpeting</h1>
<h2>Cost Estimate</h3>
</div>
<div class="instructions">Enter all the relevant parameters and click submit for an estimate
of the cost of carpeting your room</div>

<div id="userinput">
<form method="get" name="carpetform" action="#">
<table>
<tr>
<td>Room Dimensions (in ft):</td>

<td><input type="text" name="width"> x <input type="text" name="length"></td>
</tr>
<tr>
<td ><strong>Type of Carpet:</strong></td>
<td>
<select name="carpet">
<option value="">Choose Carpet Type</option>
<option value="B">Budget</option>
<option value="S">Standard</option>
<option value="P">Premium</option>
</select>
</td>
</tr>
<tr>
<td ><strong>Type of Padding:</strong></td>
<td>
<select name="padding">
<option value="">Choose Padding</option>
<option value="S">Standard</option>
<option value="P">Premium</option>
</select>
</td>
</tr>
<tr>
<td><strong>Installation Required:</strong></td>

<td>Yes<input type=radio name="install" value=y>&nbsp;&nbsp;&nbsp;No

<input type=radio name="install" value=n></td>
</tr>
<tr>
<td align=center colspan=2><input type="button" value="Submit" name="btnSubmit" onClick="CarpetJob(document.forms.carpetform.width .value, document.forms.carpetform.length.value, document.forms.carpetform.carpet.value, document.forms.carpetform.padding.value, document.forms.carpetform.install.value);"></td>
</tr>
</table>
</form>
</div>

<div id="results">

</div>
</body></HTML>
[/HTML]
PHP

[PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />


<title>Carpet 7</title>

<link type="text/css" rel="stylesheet" href="carpet.css" />



</head>

<body>

<h1>The Summary of Your Carpet Order:</h1>

<div id="all">

<?php

$W=$_GET['width'];
$L=$_GET['length'];

$carpet=$_GET['carpet'];

$padding=$_GET['padding'];

$install=$_GET['install'];


//The formula for 20% more

$area= $W * $L;

$extra=$area * .20 + $area;


?>

<br />
<br />

<?php

//The carpet price

if($carpet=="B"){
$c="1.00";

}

if($carpet=="S"){
$c="1.50";
}

if($carpet=="P"){
$c="2.00";
}

$carpetCost= $c * $extra;

print ("Your $area ft. of Carpet will cost $$carpetCost");
?>

<br />
<br />

<?php


//Padding Cost

if($padding=="S"){
$cp=.35;
}

if($padding=="P"){
$cp=.50;
}

$cPadding=$cp * $extra;

print ("Your Padding will cost $$cPadding")


?>

<br />
<br />

<?php
//The Install cost

if($install=="n"){
$in=.00;
}

if($install=="y"){
$in=2.00;
}

$installCost=$in * $extra;

print ("Your install cost will be $$installCost");
?>

<br />
<br />

<?php

$salesTax=($carpetCost+$cPadding)*.07;

print ("Your Sales Tax will be $$salesTax");

?>

<br />
<br />

<?php

$totalCost=$carpetCost+$cPadding+$installCost+$sal esTax;

print ("Your Grand Total for your carpet is $$totalCost");
?>

<br />
<br />
<?php

if ($carpet=="B"){

print (' <img src="budget.jpg" alt="Budget Carpet"/>' );
}

if ($carpet=="S"){

print ('<img src="standard.jpg" alt="Standard Carpet"/>');

}

if ($carpet=="P"){

print ('<img src="premium.jpg" alt="Premium Carpet"/>');

}

?>
<?php

if ($install=="y"){

print ('<img src="logo.jpg" alt="logo"/>');
}

if ($install=="n"){
}

?>
</div>

</body>

</html>[/PHP]
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Apr 15 '08

re: Not getting correct return values from Ajax script


Quote:

Originally Posted by Justn226

what values would i put in there?

I'm not sure what you mean. You've already got the values - see lines 30-34 in your code.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Apr 15 '08

re: Not getting correct return values from Ajax script


Quote:

Originally Posted by Justn226

I posted an earlier question...

Then there was no need to double-post. Please keep all posts relating to one problem in one thread.

Also, remember to use [code] tags when posting code. Thanks!
Newbie
 
Join Date: Apr 2008
Posts: 14
#8: Apr 15 '08

re: Not getting correct return values from Ajax script


Quote:

Originally Posted by acoder

Then there was no need to double-post. Please keep all posts relating to one problem in one thread.

Also, remember to use [code] tags when posting code. Thanks!

sorry, but i really dont know how to do this crap, i just need someone to show me how to do it so i can learn from it... i know how to test the sever side code by rebuilding the query string, but it just doesnt work. I also test with alert boxes to make sure im hitting the function...

help me out
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Apr 15 '08

re: Not getting correct return values from Ajax script


First check that the PHP script works without Ajax, i.e. when you submit the form to that script. Once you have that working, then one problem I can see with your current script is that to get the radio button, you need to check that the checked property of the radio button is checked. For that, you need to loop over the radio buttons. Let me know if you want a quick example.
Newbie
 
Join Date: Apr 2008
Posts: 14
#10: Apr 15 '08

re: Not getting correct return values from Ajax script


yea i could use an example of that... I just dont understand how it is returning 0's when the query string works if i test it...
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#11: Apr 16 '08

re: Not getting correct return values from Ajax script


Expand|Select|Wrap|Line Numbers
  1. var radio = document.forms["carpetform"].elements["install"];
  2. for (i = 0; i < radio.length; i++) { 
  3.   if (radio[i].checked) {
  4.     val = radio[i].value;
  5.     break;
  6.   }
  7. }// val contains the radio value...
Newbie
 
Join Date: Apr 2008
Posts: 14
#12: Apr 16 '08

re: Not getting correct return values from Ajax script


Quote:

Originally Posted by acoder

Expand|Select|Wrap|Line Numbers
  1. var radio = document.forms["carpetform"].elements["install"];
  2. for (i = 0; i < radio.length; i++) { 
  3.   if (radio[i].checked) {
  4.     val = radio[i].value;
  5.     break;
  6.   }
  7. }// val contains the radio value...


and that would go in my function right?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#13: Apr 17 '08

re: Not getting correct return values from Ajax script


Yes, and val would replace argReq (or you could call it argReq instead of val).
Reply