...

View Full Version : save javascript cart to mysql using PHP



alison3113
01-20-2009, 05:07 AM
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 :thumbsup::p:thumbsup:

rangana
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

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:


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

alison3113
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

rangana
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);
updateDb('mysql_page.php','get',{fields:[fields[0],fields[1],fields[2]]});


...and have this function:


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:


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum