View Full Version : save javascript cart to mysql using PHP

01-20-2009, 05:07 AM
is anyone familiar on nopdesign'n 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 :thumbsup::p:thumbsup:

01-20-2009, 08:05 AM
Which cart? How do you want the saving of data from the "cart" to your DB?

This could be achieved using this logic:

My bet is that you will be using the select element. You can call a function onchange

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:

<script type="text/javascript">
function addDb(val){
// Do some error checking for XMLHTTP request

// Do something here
<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>

...on acceptdb.php:

$val = mysql_real_escape_string($_GET['val']);
// Establish connection
$sQuery = "INSERT into table_name (val_field) values ('$val')";

01-20-2009, 04:18 PM
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

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

01-21-2009, 02:26 AM
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:

fTotal += (parseInt(fields[1]) * parseFloat(fields[2]) );
strTotal = moneyFormat(fTotal);

...and have this function:

function updateDb(url,method,obj){
var xmlHttp;
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
catch (e){
// Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
catch (e){
alert("Your browser does not support AJAX!");
return false;

alert(xmlHttp.responseText); // If successful, will alert the "echoed" part of your php


...as an example, I'm assuming that the name of your page that will add the data to your DB is mysql_page.php:

$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.