Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy save javascript cart to mysql using PHP

    is anyone familiar on nopdesign'n freecart?.,

    http://www.nopdesign.com/freecart/

    i am using it to store the customer's desired products even if they are not yet logged in to my created PHP site..,

    the problem is how can i save all the datas stored in that cart to mysql database..,

    can anyone pls help me

    or can anyone redirect me to post related to my inquiry..,tnx in advance!..,
    mabuhay kayong lahat
    Last edited by alison3113; 01-20-2009 at 04:13 AM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Which cart? How do you want the saving of data from the "cart" to your DB?

    This could be achieved using this logic:
    1. My bet is that you will be using the select element. You can call a function onchange
    2. The function should connect to your DB, and on ready state (4), pass the value to a page that will accept the value and store it on DB. Something like this:
      Code:
      <script type="text/javascript">
      function addDb(val){
      // Do some error checking for XMLHTTP request
      
      xmlhttp.onreadystatechange=function(){
      if(xmlhttp.readystate==4){
      // Do something here
      }
      }
      xmlHttp.open("GET","acceptdb.php?val="+val,true);
      xmlHttp.send(null);
      }
      </script>
      <select onchange="addDb(this.value)">
      <option value="val1">Option 1</option>
      <option value="val2">Option 2</option>
      <option value="val3">Option 3</option>
      <option value="val4">Option 4</option>
      <option value="val5">Option 5</option>
      </select>

      ...on acceptdb.php:
      PHP Code:
      $val mysql_real_escape_string($_GET['val']);
      // Establish connection
      $sQuery "INSERT into table_name (val_field) values ('$val')";
      mysql_query($sQuery); 
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry..,i am not familiar with that XMLHTTPrequest..,i wish i could study that but i really have no time ..,by the way..,i have this code to help you relate to my problem

    Code:
    for ( i = 1; i <= iNumberOrdered; i++ ) {
          NewOrder = "Order." + i;
          database = "";
          database = GetCookie(NewOrder);
    
          Token0 = database.indexOf("|", 0);
          Token1 = database.indexOf("|", Token0+1);
          Token2 = database.indexOf("|", Token1+1);
          Token3 = database.indexOf("|", Token2+1);
          Token4 = database.indexOf("|", Token3+1);
    
          fields = new Array;
          fields[0] = database.substring( 0, Token0 );                 // Product ID
          fields[1] = database.substring( Token0+1, Token1 );          // Quantity
          fields[2] = database.substring( Token1+1, Token2 );          // Price
          fields[3] = database.substring( Token2+1, Token3 );          // Product Name/Description
          fields[4] = database.substring( Token3+1, Token4 );          // Shipping Cost
          fields[5] = database.substring( Token4+1, database.length ); //Additional Information
    
          fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );
          strTotal    = moneyFormat(fTotal);
    
          
    
       }
    what i want i that each time this iterates i wanna get the value of this fields
    Code:
          fields[0] = database.substring( 0, Token0 );                 // Product ID
          fields[1] = database.substring( Token0+1, Token1 );          // Quantity
          fields[2] = database.substring( Token1+1, Token2 );          // Price
    to store in into my mysql using PHP

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    This isn't tested, but the logic should work:

    You need to call a function and pass the fields you want to be added to your DB:
    Code:
    fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );
    strTotal    = moneyFormat(fTotal);
    updateDb('mysql_page.php','get',{fields:[fields[0],fields[1],fields[2]]});
    ...and have this function:
    Code:
    function updateDb(url,method,obj){
    	var xmlHttp;
    	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;
    			}
    		}
    	}
    	
    	xmlHttp.onreadystatechange=function(){
    		if(xmlHttp.readyState==4){
    			alert(xmlHttp.responseText); // If successful, will alert the "echoed" part of your php
    		}
        }
    	
    	xmlHttp.open(method,url+'?val1='+obj.fields[0]+'&val2='+obj.fields[1]+'&val3='+obj.fields[2],true);
    	xmlHttp.send(null);
      }
    ...as an example, I'm assuming that the name of your page that will add the data to your DB is mysql_page.php:
    PHP Code:
    $user "root"// DB Username
    $pass "database_password"// DB pass
    $name "database_name"// DB Name
    $host "localhost"// DB Host
    $webmaster "webmaster@domain.com" // Set the webmaster's email here


    $db=mysql_connect($host,$user,$pass) or die("Connection refused. Please check your database or contact the <a href='mailto:".$webmaster."'>webmaster</a>.");

    mysql_select_db($name,$db); // Establish connection

    $val1 clean($_GET['val1']); // Get val1 and rectify it
    $val2 clean($_GET['val2']); // Get val2 and rectify it
    $val3 clean($_GET['val3']); // Get val3 and rectify it

    $sQuery "INSERT INTO table_name (column1,column2,column3) values ('$val1','$val2','$val3')"// Create query string
    mysql_query($sQuery) or die(mysql_error()); // Execute query

    echo mysql_affected_rows()?'Values were added on DB':'Query fails.';

    function 
    clean($str){
        return 
    mysql_real_escape_string($str);

    Hope that makes sense.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Tags for this Thread

    Posting Permissions

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