SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript passing value help

    Hi experts, im new to coding and i really need help for this.
    i have done a shopping cart calculation using javascript my question now is, HOW TO SEND JAVASCRIPT VALUE INTO DATABASE (MYSQL) ??

    PHP Code:
    <?php
    require_once 'library/config.php';
    require_once 
    'library/cart-functions.php';

    $action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';

    switch (
    $action) {
        case 
    'add' :
            
    addToCart();
            break;
            
        case 
    'update' :
            
    updateCart();
            break;    

        case 
    'delete' :
            
    deleteFromCart();
            break;
        case 
    'view' :
    }

    $cartContent getCartContent();
    $numItem count($cartContent);

    $pageTitle 'Shopping Cart';
    require_once 
    'include/header.php';

    // show the error message ( if we have any )
    displayError();

    if (
    $numItem ) {
    ?>
    <head>



     <script type="text/javascript">
     
    <!-- Begin
    function  doMath() {
    var one = eval(document.getElementById('txtQty[]').value) //one = quantity 

    var price = 0;
    var state = document.frmCart.state.value;


    var unitPrice = document.getElementById('pd_price').innerText.substring(3);


    if(state == "east"){
        if (one == 1){
            price = 6;
        }
        else if (one == 2){
            price = 8;
        }
        else if (one == 3){
            price = 10;
        }
        else if (one == 4){
            price = 12;
        }
        else if (one == 5){
            price = 12.50;
        }
        else if (one == 6){
            price = 13;
        }
        else if (one == 7){
            price = 14;
        }
        else if (one == 8){
            price = 0;
        }
        
    }

    else if(state == "west"){
        if (one == 1){
            price = 9.30;
        }
        if (one == 2){
            price = 12;
        }
        if (one == 3){
            price = 17.25;
        }
        if (one == 4){
            price = 20;
        }
        if (one == 5){
            price = 21;
        }
        if (one == 6){
            price = 22;
        }
        if (one == 7){
            price = 23.45;
        }
        if (one == 8){
            price = 0;
        }
    }


    document.getElementById('subtotal').innerText =  "MYR" +  unitPrice * one;


    document.getElementById('amount').innerText =  price; ///amount = shipping

    var test = document.getElementById('subtotal').innerText;                                                 

    test = parseInt(test.substring(3)) * one;

    document.getElementById('total').innerText =   price + unitPrice * one; 

    test = "MYR" + test;

    document.getElementById('ct_qty').innerText =  document.getElementById('subtotal').innerText;
    }

        
    </script>
    </head>


    <body onLoad=", doMath()" >
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      

    <table width="900" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td align="center" bgcolor="#b6df51"><table width="800" border="0" cellspacing="0" cellpadding="0"><tr><td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
     
      <tr>
        <td align="center"><table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr><form action="<?php echo $_SERVER['PHP_SELF'] . "?action=update"?>" method="post" name="frmCart" id="frmCart">
            <td align="center" bgcolor="#b6df51"><table width="800" border="0" cellspacing="0" cellpadding="0" >
              <tr>
                <td align="left"><img src="images/title_purchase_online.gif" width="280" height="25" /></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td align="center"><table width="800" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td><table width="800" border="0" cellpadding="0" cellspacing="0" bgcolor="#3b7b37">
                      <tr>
                        <td width="15"><img src="images/title_box_left.gif" width="15" height="30" /></td>
                        <td width="770"><table width="770" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td width="170" align="center" class="yellow_txt"><strong>Item</strong></td>
                            <td width="213" align="center">&nbsp;</td>
                            <td width="141" align="center"><strong class="yellow_txt">Unit Price</strong></td>
                            <td width="106" align="center"><strong class="yellow_txt">Quantity</strong></td>
                            <td width="110" align="center"><strong class="yellow_txt">Total</strong></td>
                            <td width="30" align="center">&nbsp;</td>
                            </tr>
                        </table></td>
                        <td width="15"><img src="images/title_box_right.gif" width="15" height="30" /></td>
                      </tr>
                    </table></td>
                  </tr>
                  
    <?php
    $subTotal 
    0;
    for (
    $i 0$i $numItem$i++) {
        
    extract($cartContent[$i]);
        
    $productUrl "other_products.php?c=$cat_id&p=$pd_id";
        
    $subTotal += $pd_price $ct_qty;
    ?>
                  
                  <tr>
                    <td>&nbsp;</td>
                  </tr>
                  <tr>
                    <td align="center"><table width="770" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="170" align="center" class="yellow_txt"><table width="150" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
                          <tr>                      </tr>
                          <tr>
                            
                            <td width="116" align="center" valign="top" bgcolor="#b6df51"><a href="<?php echo $productUrl?>"><img src="<?php echo $pd_thumbnail?>" width="116" height="116" border="0" /></a><a href="<?php echo $productUrl?>"></a></td>
                          </tr>
                          <tr>                      </tr>
                        </table></td>
                        <td width="220" align="left" class="green_txt"><strong><a href="<?php echo $productUrl?>" style="text-decoration: none"><font color="000000"><?php echo  $pd_name?></font></a></strong></td>
                        <td width="128" align="center"><strong class="black_txt" id="pd_price"><?php echo displayAmount($pd_price); ?></strong></td>
                        <td width="112" align="center"><input name="txtQty[]"  type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty?>" class="box" onKeyUp="checkNumber(this);">
                        <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id?>">
      <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id?>"></td>
                        <td width="110" align="center"><strong class="black_txt" id="ct_qty"><?php echo displayAmount($pd_price $ct_qty); ?></strong></td>
                       <td width="30" align="center" valign="middle"><a href="#"> <img src="images/btn_delete.png" width="16" height="16" border="0" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"?>';" /></a>                  </tr>
                    </table></td>
                  </tr>
                  
    <?php
    }
    ?>

                  <tr>
                    <td>&nbsp;</td>
                  </tr>
                  <tr>
                    <td><table width="800" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="13" height="13"><img src="images/text_box_big_left1.gif" width="13" height="13" /></td>
                        <td width="774" height="13" bgcolor="#ffde00"><span class="style10"><img src="images/spacer.gif" width="13" height="13" /></span></td>
                        <td width="13" height="13"><img src="images/text_box_big_right1.gif" width="13" height="13" /></td>
                      </tr>
                      <tr>
                        <td bgcolor="#ffde00">&nbsp;</td>
                        <td height="50" align="center" bgcolor="#ffde00"><table width="770" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td width="132" align="center" class="yellow_txt">&nbsp;</td>
                            <td width="147" align="left" class="green_txt">&nbsp;</td>
                            <td width="113" align="center">&nbsp;</td>
                            <td width="102" align="center" class="black_txt"><strong>Sub-total</strong></td>
                            <td colspan="2" align="center"><strong class="black_txt" id="subtotal"><?php echo displayAmount($subTotal); ?></strong></td>
                            <td width="19" align="center" valign="middle">&nbsp;</td>
                          </tr>
                          <tr>
                            <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                            </tr>

     
                          <tr>
                            <td align="center" >&nbsp;</td>
                            <td align="left" class="green_txt">&nbsp;</td>
                            <td> </td>
                            <td align="center" class="black_txt"><strong>State</strong></td>
                            <td colspan="2" align="center">    <select name="state">
                            <option value="east" selected>East Malaysia</option>
                            <option value="west">Peninsular Malaysia</option>
                              </select>                        </td>
                            <td align="center" valign="middle">&nbsp;</td>
                          </tr>
        
        
        <tr>
                            <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                            </tr>
                             <tr>
                            <td align="center" class="yellow_txt">&nbsp;</td>
                            <td align="left" class="green_txt">&nbsp;</td>
                            <td align="center">&nbsp;</td>
                            <td align="center" class="black_txt"><strong>Shipping</strong></td>
                            
                            <td width="131" align="right"> <strong class="black_txt">
                              MYR</strong>
                            
                            
                            <td width="122" align="left"><strong class="black_txt">
                              <div name="amount" id="amount"> 0</div></strong></td>
                            <td align="center" valign="middle">&nbsp;</td>
                          </tr>
                           <tr>
                            <td align="center" class="yellow_txt">&nbsp;</td>
                            <td align="left" class="green_txt">&nbsp;</td>
                            <td align="center">&nbsp;</td>
                            <td align="center" class="black_txt33">                        </td>
                            <td colspan="2" align="center" class="black_txt33">
                            <font color="#FF0000">*Purchases of 8 boxes onwards free delivery charges.</font>                        </td>
                            <td align="center" valign="middle">&nbsp;</td>
                          </tr>


                          
                          
                          <tr>
                            <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                            </tr>
                          <tr>
                            <td align="center" class="yellow_txt">&nbsp;</td>
                            <td align="left" class="green_txt">&nbsp;</td>
                            <td align="center">&nbsp;</td>
                            <td align="center" class="black_txt"><strong>Total</strong></td>
                            <td width="131" align="right"> <strong class="black_txt">
                              MYR</strong></td>
                            
                            
                            <td colspan="2" align="left"><strong class="black_txt" id="total"><?php echo displayAmount($subTotal);// + $shopConfig['shippingCost']); ?></strong>                        </td>
                            
                            <td width="4" align="center" valign="middle">&nbsp;</td>
                          </tr>
                          <tr>
                            <td colspan="7" align="center" class="yellow_txt"><img src="images/spacer.gif" width="10" height="10" /></td>
                            </tr>
                          <tr>
                            <td align="center" class="yellow_txt">&nbsp;</td>
                            <td align="left" class="green_txt">&nbsp;</td>
                            <td align="center">&nbsp;</td>
                            <td align="center">&nbsp;</td>
                            <td colspan="2" align="center">
                            <input type="button" class="update_btn" src="images/btn_update_cart.gif" width="102" height="22" border="0"  name="btnUpdate" id="btnUpdate" onClick="doMath()">
                            
                             
                              -->                          </td>
                            <td align="center" valign="middle">&nbsp;</td>
                          </tr>
                        </table></td>
                        <td bgcolor="#ffde00">&nbsp;</td>
                      </tr>
                      <tr>
                        <td width="13" height="13"><img src="images/text_box_big_left2.gif" width="13" height="13" /></td>
                        <td bgcolor="#ffde00"><span class="style10"><img src="images/spacer.gif" width="13" height="13" /></span></td>
                        <td width="13" height="13"><img src="images/text_box_big_right2.gif" width="13" height="13" /></td>
                      </tr>
                      <tr>
    <td>&nbsp;</td>
    </tr>
                    </table></td>
                  </tr>
                  <tr>


    <?php

    $shoppingReturnUrl 
    = isset($_SESSION['shop_return_url']) ? $_SESSION['shop_return_url'] : 'other_products.php';
    ?>
                  <tr>
                 
                    <td align="center"><table width="340" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                      <td>&nbsp;</td>
                        <td><a href="#"> <img src="images/btn_continue_shopping.gif" width="162" height="22" border="0" onClick="window.location.href='<?php echo $shoppingReturnUrl?>';" /></a></td>
                        
                        
                        
                        
    <?php 
    if ($numItem 0) {
    ?> 

                        <td width="20">&nbsp;</td>
                        <td><a href="#"> <img src="images/btn_continue_checkout.gif" width="162" height="22" border="0" onClick="window.location.href='checkout.php?step=1';"/></a></td>
                        
                 
     <?php
    }
    ?> 
                      </tr>
                    </table></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
                <td align="center">&nbsp;</td>
              </tr>
            </table></td></form>
          </tr>
          
        
        </table></td>
      </tr>
    </table>  
              </tr>
    </table></td>
          </tr>
    </table></td>
      </tr>
    </table>



    <?php
    } else {
        
    ?>
                 <?php include("EmptyMSG.php"); ?>

        
              
          <?php ?>     
         


    </body>
    </html>
    Thank you in advanced , any help will be greatly appreciated !

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,824
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You need to do the same calculation again on the server after the cart is submitted. Not everyone will have JavaScript enabled so the JavaScript calculation is only of use to those that do to let them know in advance what the result of the calculation on the server will be.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How is your PHP adding the value to the database? If it uses a GET parameter (which it shouldn't, seriously, don't), like the following...

    PHP Code:
    $value $_GET['x'];
    addToDatabase($value); 
    ...then you can totally cheat and do something like this with your JS:

    Code JavaScript:
    (new Image()).src = 'path/to/addToDatabase.php?x=' + theValue;

    That will load your PHP page, send the value to it, and execute the code, all from JavaScript.

    But man, it's ugly and wrong and shouldn't really be done this way.
    I'm the web overlord for Graphic Business Systems


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
  •