SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting value from calculator into PHP

    Hi there, I hope I am posting in the right forum and I am reallly hoping someone can help me.

    I have an order form that uses a javascript calculator to calculate a
    garment size.
    I need to pass the final calculation to the shopping cart but I just can't
    seem to figure it out. Gone blank...again.

    The order form looks like this
    HTML Code:
    [left][small]
    <body onLoad="FillHip()">
    <form name="size" action="checkout_payment.php?products_id=65&action=add_product" method="post">
    <table  border=0 align="center">
    <tbody>
    <tr>
    <td colspan="2" valign="top"><p><strong>PANTS - $279.95</strong> </p>
    Qty &nbsp;<input name=cart_quantity[] id=cart_quantity[] value="0" size="5"><br>
    Waist &nbsp; <input type="hidden" name ="id[txt_7]"> 
    <input name="txtWaist" type="TEXT"  size="5" maxlength="5">&nbsp;
    <select name="waist"  onChange="FillHip()">
    <option value="inc" selected>inches</option>
    <option value="cms">cms</option>
    </select>
    <input type="hidden" name ="id[11]2"> <br>
    Hips&nbsp;<input type="hidden" name ="id[txt_6]">&nbsp;
    <input name="txtHip" type="text"  id="txtHip" size="5">&nbsp;
    <select name="hip" >
    <option value="inc" selected>inches</option>
    </select>
    <br>
    <input name="btnCalc2" type="button" id="btnCalc22" onClick="calcWHR();" value="Calculate">
    <br>
    <br>
    Size&nbsp; <input name="txtWHR" type="TEXT"  onFocus="blur();" size="10" maxlength="4">
    <br>
    Or &nbsp;&nbsp; <select name="id[11]" >
    <option value="" selected>Select Size</option>
    <option value="30">XS</option>
    <option value="31">S</option>
    <option value="32">M</option>
    <option value="33">L</option>
    <option value="34">XL</option>
    <option value="37">2XL</option>
    <option value="35">3XL</option>
    <option value="36">4XL</option>
    </select>
    <br>
    <br>
    <input name="submit" type="button" id="submit" value="Submit" >
    <input type="hidden" name="products_id" value="65">
    </td>
    </tr>
    </tbody>
    </table>
    </form>
    </body>[/small][/left][small]
    [/small]
    The js calculator looks like this -
    Code:
    ========================================= /********** VARIABLES **********/ var szMsg; var fWaist; var fHip; var str_ind; var str; /********** VALIDATION **********/ //FUNCTION TO VALIDATE RANGES FOR NUMERIC ENTRIES function verNumber(szValue,szQuestName,nMin,nMax,bOptional) { var bIsNum = false; //DETERMINE IF BOX IS EMPTY if (szValue.length == 0 && !bOptional) { szMsg += ' ' + szQuestName + ' requires a numeric value with no letters or spaces.\n'; } //DETERMINE IF BOX IS NOT A NUMBER if(str == "inc"){ for (var i=0; i< szValue.length; i++) { if ((szValue.substring(i,i+1) != "0") && (szValue.substring(i,i+1) != "1") && (szValue.substring(i,i+1) != "2") && (szValue.substring(i,i+1) != "3") && (szValue.substring(i,i+1) != "4") && (szValue.substring(i,i+1) != "5") && (szValue.substring(i,i+1) != "6") && (szValue.substring(i,i+1) != "7") && (szValue.substring(i,i+1) != "8") && (szValue.substring(i,i+1) != "9") && (szValue.substring(i,i+1) != ".")) { bIsNum = false; szMsg += ' ' + szQuestName + ' requires a positive numeric value with no letters or spaces.Please take care of format.\n'; break; } else { bIsNum = true; } } }else{ for (var i=0; i< szValue.length; i++) { if ((szValue.substring(i,i+1) != "0") && (szValue.substring(i,i+1) != "1") && (szValue.substring(i,i+1) != "2") && (szValue.substring(i,i+1) != "3") && (szValue.substring(i,i+1) != "4") && (szValue.substring(i,i+1) != "5") && (szValue.substring(i,i+1) != "6") && (szValue.substring(i,i+1) != "7") && (szValue.substring(i,i+1) != "8") && (szValue.substring(i,i+1) != "9") && (szValue.substring(i,i+1) != ".")) { bIsNum = false; szMsg += ' ' + szQuestName + ' requires a positive numeric value with no letters or spaces.Please take care of format\n'; break; } else { bIsNum = true; } } } if (bIsNum) { if ((szValue > nMax) || (szValue < nMin)) { szMsg += ' ' + szQuestName + ' requires a number between ' + nMin + ' and ' + nMax + '.\n'; } } } /********** OTHER FUNCTIONS **********/ //FUNCTION TO HALT SUBMISSION AND GENERATE ALERT MESSAGE //IF GLOBAL VARIABLE SZMSG IS NOT EMPTY. function checkIt() { //CHECK GLOBAL VAR SZMSG. //IF NOT EMPTY PRINT CONTENTS AND RETURN VALUE OF FALSE if (szMsg != "") { //PRINT ALERT BOX alert('________________________________________________________\n\n' + 'The following question(s) was not completed properly:\n\n' + szMsg + '\n________________________________________________________\n\n' + 'Please correct and re-submit form.'); return false; } else { return true; } } /********** CALCULATION **********/ //FUNCTION TO CALCULATE WHR function calcWHR() { szMsg = ""; var xSize = 0; fWaist = document.size.txtWaist.value; fHip = document.size.txtHip.value; str_ind = window.document.size.waist.selectedIndex; str = window.document.size.waist.options[str_ind].value; verNumber(fWaist,"Waist measurement",0,999,false); verNumber(fHip,"Hip measurement",0,999,false); if (checkIt()) { if(str == "inc"){ fWaistMo = parseFloat(fWaist.substring(0, 2)); fHipMo = parseFloat(fHip.substring(0, 2)); fWaistBal = fWaist.substring(2,5); if(fWaistBal != ""){ fWaistP = parseFloat(fWaist.substring(2,3)); fWaistP1 = parseFloat(fWaist.substring(4,5)); decPoint = fWaistP/fWaistP1; fWaistMo = fWaistMo + decPoint; } fHipBal = fHip.substring(2,5); if(fHipBal != ""){ fHipP = parseFloat(fHip.substring(2,3)); fHipP1 = parseFloat(fHip.substring(4,5)); decPoint = fHipP/fHipP1; fHipMo = fHipMo + decPoint; } if(fWaistMo >= 1 && fWaistMo < 38.5 && fHipMo >= 36.5 && fHipMo < 38.5){ xSize = "XS"; } if(fWaistMo >= 1 && fWaistMo < 40.5 && fHipMo >= 38.5 && fHipMo < 40.5){ xSize = "S"; } if(fWaistMo >= 1 && fWaistMo < 42.5 && fHipMo >= 40.5 && fHipMo < 42.5){ xSize = "M"; } if(fWaistMo >= 1 && fWaistMo < 44.5 && fHipMo >= 42.5 && fHipMo < 44.5){ xSize = "L"; } if(fWaistMo >= 1 && fWaistMo < 46.5 && fHipMo >= 44.5 && fHipMo < 46.5){ xSize = "XL"; } if(fWaistMo >= 1 && fWaistMo < 48.5 && fHipMo >= 46.5 && fHipMo < 48.5){ xSize = "2XL"; } if(fWaistMo >= 1 && fWaistMo < 50.5 && fHipMo >= 48.5 && fHipMo < 50.5){ xSize = "3XL"; } if(fWaistMo >= 1 && fWaistMo < 52.5 && fHipMo >= 50.5 && fHipMo < 52.5){ xSize = "4XL"; } }else{ fWaistMo = parseFloat(fWaist); fHipMo = parseFloat(fHip); if(fWaistMo >= 1 && fWaistMo < 96.50 && fHipMo >= 91.50 && fHipMo < 96.50){ xSize = "XS"; } if(fWaistMo >= 1 && fWaistMo < 101.50 && fHipMo >= 96.50 && fHipMo < 101.50){ xSize = "S"; } if(fWaistMo >= 1 && fWaistMo < 106.50 && fHipMo >= 101.50 && fHipMo < 106.50){ xSize = "M"; } if(fWaistMo >= 1 && fWaistMo < 111.50 && fHipMo >= 106.50 && fHipMo < 111.50){ xSize = "L"; } if(fWaistMo >= 1 && fWaistMo < 116.50 && fHipMo >= 111.50 && fHipMo < 116.50){ xSize = "XL"; } if(fWaistMo >= 1 && fWaistMo < 121.50 && fHipMo >= 116.50 && fHipMo < 121.50){ xSize = "2XL"; } if(fWaistMo >= 1 && fWaistMo < 126.50 && fHipMo >= 121.50 && fHipMo < 126.50){ xSize = "3XL"; } if(fWaistMo >= 1 && fWaistMo < 131.50 && fHipMo >= 126.50 && fHipMo < 131.50){ xSize = "4XL"; } } if(xSize == 0){ alert("We apologize but it appears we were unable to calculate your size."); } size.txtWHR.value = xSize; } } function FillHip() { str_ind = window.document.size.waist.selectedIndex; str = window.document.size.waist.options[str_ind].value; if(str == "inc"){ window.document.size.hip.options[0].value = "inc"; window.document.size.hip.options[0].text = "inches"; }else{ window.document.size.hip.options[0].value = "cms"; window.document.size.hip.options[0].text = "cms"; } } =========================================
    The calculator works fine but obviously I am missing something because
    it is not passing the value to the shopping cart. I am hoping for a simple
    solution and not something to complicated for me to understand -
    "beginnerish" in a word.

    The shopping cart I am using is OScommerce - very steep learning curve -
    and all the other values in other sections of the form are passed fine but
    not the one in the text box that I want.

    I would be truly grateful for some guidance from anyone who can see what I am doing
    wrong - aside from biting off more than I can chew !

    Thank you in advance to anyone who takes the time to help me out.

    jenem
    _____________________________________
    To err is human--and to blame it on a computer is even more so
    Last edited by jenem; Nov 8, 2003 at 09:26.

  2. #2
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    NJ/NY
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,
    If you want to pass the value to PHP, you will need to refresh the page (or jump to a new page). The reason is because PHP is serverside, so once the page has finished loading, you can't really use any PHP functions anymore.

    So, I was suggesting something like this... after your javascript calculator calculates a value, refresh the page with a value appended to the url (e.g. url.php?calculatedvalue=##). Your php script then can use $_GET['calculatedvalue'] to process whatever.

    Hope that sounds reasonable... heh.

  3. #3
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your response, I will give it a try and see how I go.

    I appreciate you taking the time to respond.


    Jene


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •