Not getting correct return values from Ajax script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Justn226
    New Member
    • Apr 2008
    • 14

    #1

    Not getting correct return values from Ajax script

    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>Zeller s Carpeting Cost Estimate</title>
    </head>

    <script>

    var http = createRequestOb ject();

    function createRequestOb ject() {

    var ro;
    var browser = navigator.appNa me;
    if(browser == "Microsoft Internet Explorer"){
    ro = new ActiveXObject(" Microsoft.XMLHT TP");
    }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.widt h.value;
    length=document .carpetform.len gth.value;
    carpet=document .carpetform.car pet.value;
    padding=documen t.carpetform.pa dding.value;
    install=documen t.carpetform.in stall.value;

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

    function handleResponse( ) {

    if(http.readySt ate == 4){

    document.getEle mentById("resul ts").innerHTM L = http.responseTe xt;
    }
    }

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

    <div class="instruct ions">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="carpetfor m" 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">Budge t</option>
    <option value="S">Stand ard</option> <option value="P">Premi um</option>
    </select>
    </td>
    </tr>
    <tr>
    <td ><strong>Type of Padding:</strong></td>
    <td>

    <select name="padding">
    <option value="">Choose Padding</option>
    <option value="S">Stand ard</option>
    <option value="P">Premi um</option>
    </select>

    </td>
    </tr>
    <tr>
    <td><strong>Ins tallation 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><inpu t type="button" value="Submit" onclick="Carpet Job();"></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.dt d">
    <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.cs s" />
    </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=$i n * $extra;

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

    ?>

    <br />
    <br />

    <?php

    $salesTax=($car petCost+$cPaddi ng)*.07;

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

    <br />
    <br />

    <?php

    $totalCost=$car petCost+$cPaddi ng+$installCost +$salesTax;

    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.j pg" alt="Standard Carpet"/>');

    }

    if ($carpet=="P"){

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

    }

    ?>

    <?php

    if ($install=="y") {

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

    if ($install=="n") {

    }

    ?>
    </div>

    </body>

    </html>[/HTML]
    Last edited by gits; Apr 9 '08, 09:26 PM. Reason: added code tags
  • Justn226
    New Member
    • Apr 2008
    • 14

    #2
    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...

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Since you're not passing the arguments to CarpetJob(), use the variables you've created within the function:
      [code=javascript]http.open('get' , "carpet.php?wid th=" + width +"&length="+ length +"&carpet=" + carpet +"&padding=" + padding +"&install=" + install);[/code]

      Comment

      • Justn226
        New Member
        • Apr 2008
        • 14

        #4
        what values would i put in there?

        Comment

        • Justn226
          New Member
          • Apr 2008
          • 14

          #5
          ajax help

          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>Zeller s Carpeting Cost Estimate</title>
          </head>

          <script>

          var http = createRequestOb ject();

          function createRequestOb ject() {

          var ro;
          var browser = navigator.appNa me;
          if(browser == "Microsoft Internet Explorer"){
          ro = new ActiveXObject(" Microsoft.XMLHT TP");
          }else{
          ro = new XMLHttpRequest( );
          }
          return ro;
          }

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

          http.open('get' , "carpet.php?Wid th=" +argWidth+"&len gth=" +argLength+"&ca rpet=" +argType+"&padd ing=" +argPad+"&insta ll=" +argReq);
          http.onreadysta techange = handleResponse;
          http.send(null) ;
          }

          function handleResponse( ) {

          if(http.readySt ate == 4){

          document.getEle mentById("resul ts").innerHTM L = http.responseTe xt;

          }
          }

          </script>
          <body>
          <div id="banner">
          <h1>Zellers Carpeting</h1>
          <h2>Cost Estimate</h3>
          </div>
          <div class="instruct ions">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="carpetfor m" 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">Budge t</option>
          <option value="S">Stand ard</option>
          <option value="P">Premi um</option>
          </select>
          </td>
          </tr>
          <tr>
          <td ><strong>Type of Padding:</strong></td>
          <td>
          <select name="padding">
          <option value="">Choose Padding</option>
          <option value="S">Stand ard</option>
          <option value="P">Premi um</option>
          </select>
          </td>
          </tr>
          <tr>
          <td><strong>Ins tallation 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><inpu t type="button" value="Submit" name="btnSubmit " onClick="Carpet Job(document.fo rms.carpetform. width.value, document.forms. carpetform.leng th.value, document.forms. carpetform.carp et.value, document.forms. carpetform.padd ing.value, document.forms. carpetform.inst all.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.dt d">
          <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.cs s" />



          </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=$i n * $extra;

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

          <br />
          <br />

          <?php

          $salesTax=($car petCost+$cPaddi ng)*.07;

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

          ?>

          <br />
          <br />

          <?php

          $totalCost=$car petCost+$cPaddi ng+$installCost +$salesTax;

          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.j pg" alt="Standard Carpet"/>');

          }

          if ($carpet=="P"){

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

          }

          ?>
          <?php

          if ($install=="y") {

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

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

          ?>
          </div>

          </body>

          </html>[/PHP]
          Last edited by acoder; Apr 15 '08, 11:35 AM. Reason: Added code tags + cleaned up!

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            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.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              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!

              Comment

              • Justn226
                New Member
                • Apr 2008
                • 14

                #8
                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

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  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.

                  Comment

                  • Justn226
                    New Member
                    • Apr 2008
                    • 14

                    #10
                    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...

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      [code=javascript]var radio = document.forms["carpetform "].elements["install"];
                      for (i = 0; i < radio.length; i++) {
                      if (radio[i].checked) {
                      val = radio[i].value;
                      break;
                      }
                      }// val contains the radio value...[/code]

                      Comment

                      • Justn226
                        New Member
                        • Apr 2008
                        • 14

                        #12
                        Originally posted by acoder
                        [code=javascript]var radio = document.forms["carpetform "].elements["install"];
                        for (i = 0; i < radio.length; i++) {
                        if (radio[i].checked) {
                        val = radio[i].value;
                        break;
                        }
                        }// val contains the radio value...[/code]

                        and that would go in my function right?

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

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

                          Comment

                          Working...