Page 1 of 2

how to use a javascript variable in a php page.

Posted: Fri Apr 30, 2010 4:36 am
by amitdubey2
hello all,

i am nw working on a shopping cart project in which i want to make a paypal gateway for the code.Bt i stucked in between...
The problem is that ...i am getting the total price for a user's shopping bt it is showing the total amount in the same page (fly-to-bucket.php)using some javascript...bt i want the total to another page (example: billing.php),where i get the amount for billing and all.
I am attaching the files with this...can someone suggest me how to make the billing.php page where i can able to fetch Total amount... :idea: :?: .

Please check the attachment bellow...

Regards
Amit

Re: how to use a javascript variable in a php page.

Posted: Fri Apr 30, 2010 6:58 am
by amitdubey2
Hey somebody save me plzzz...its really urgent... :roll:
I need help for the above ....plz..plz... :idea:

Re: how to use a javascript variable in a php page.

Posted: Fri Apr 30, 2010 9:26 am
by hypedupdawg
I cant access the files as I dont have a .rar decrypter where I am at the moment. However, it sounds like all you need to do is send the total amount using the $_GET method. Note: This should only be used for non-secure data.

This should be put on the first page:

Code: Select all

<?php
$total = 99 //I am using this as your total amount
?>

<script type="text/javascript">
//This is a standard go-to-url function generated from Dreamweaver.
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
var url = "your/url/address/here.php?total=<?php print $total; ?>";
MM_GoToURL('top',url);
</script>
What this does is set an URL with the $total printed into it...
...then on the recieving page it should say:

Code: Select all

<?php
$total = $_GET['total'];
?>
You can then use the $total variable for anything you like. Let me know how it goes!

Re: how to use a javascript variable in a php page.

Posted: Fri Apr 30, 2010 11:35 am
by amitdubey2
Dear hypedupdawg,

Thanks for your concern...Although your logic and syntax may be correct bt it seems a bouncer to me ,because first time i am dealing with javascript... :?: :?: :?:

So can you please do a little more favour and check the attached .rar file ...which i am using...Because i dont knw where to use ur syntax in that .....i just need the total amount (which is stored in var "shopping_cart_totalprice" as i think) in a fresh php page..so that i can use it for some billing purpose...!

once again thanks for your concern and support ... :)

Regards
Amit

Re: how to use a javascript variable in a php page.

Posted: Fri Apr 30, 2010 12:23 pm
by requinix
He would have checked it had you not wrapped a RAR in a ZIP. He can't open RAR files.

Re: how to use a javascript variable in a php page.

Posted: Fri Apr 30, 2010 12:38 pm
by amitdubey2
thanks....

But now both .rar and .zip are available above...please check whtever suits you best.





regards
amit

Re: how to use a javascript variable in a php page.

Posted: Sat May 01, 2010 10:59 am
by amitdubey2
hello....

hypedupdawg where are you.... :?: :?: :?:
Help me plzzzz....i am nt able to get out of this problem... :idea:
Is somebody else there to solve this :?: :?: :?:

Re: how to use a javascript variable in a php page.

Posted: Sat May 01, 2010 5:16 pm
by hypedupdawg
Not being cruel, but these are the basic files that seem to have been downloaded straight from the site without any modifications. There are no descriptions or even prices set up for them. I will be happy to help you, but you really need to have a working total before you can send it.

Saying that, I will look over them tomorrow in the morning light - I'm only awake on caffeine :lol:

Re: how to use a javascript variable in a php page.

Posted: Sun May 02, 2010 2:19 am
by amitdubey2
Dear hypedupdawg ..

You are right ...i have downloaded these files ... bt it is because i only need this type of thing...and i think total is working properly...when you add some product to basket ..it will be in cart ...which is right side of the product list and below that the "total" will come ....which i want in the other page for billing . You can just test it on localhost...the page is fly-to-basket.html .
there they are using
<div id="shopping_cart_totalprice"></div>

which is calling a function to update the total from .js file...everytime we added some product in the cart.So how to fetch this <div> thing to other page to use it for billing?? :?: :?: :?:
Thanks...

Re: how to use a javascript variable in a php page.

Posted: Sun May 02, 2010 3:19 am
by hypedupdawg
ok! answer time...

First of all, in the fjy-to-basket.js file the totalPrice variable needs to be moved so that it is a global declaration:

Code: Select all

var totalPrice = 0; //New Location
function updateTotalPrice()
{
	var itemBox = document.getElementById('shopping_cart_items');
	// Calculating total price and showing it below the table with basket items
	//var totalPrice = 0; //Old Location
Then, an onClick event needs to be added to some text / button etc. anywhere on your .html page:

Code: Select all

<p style="text-decoration:underline" onClick="MM_goToURL('top','billing.php?tot='.concat(totalPrice))">Billing ></p>
Then, a php page needs to be added with the following code:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Billing</title>
<?php

$totalPrice = $_GET['tot'];

?>
</head>

<body>
<p align="center">Total Price: <?php print $totalPrice; ?></p>
</body>
</html>
And that's it! However, at the moment it will only display NaN until you have the price list set up - here are the new files...

Re: how to use a javascript variable in a php page.

Posted: Sun May 02, 2010 11:40 am
by amitdubey2
Thanks...
Hypedupdawg thanks for help..I really appriciate your concern with the depth of my heart ...
keep working like this.. :D :) :D :)




Regards
Amit :drunk:

Re: how to use a javascript variable in a php page.

Posted: Sun May 02, 2010 11:42 am
by hypedupdawg
no problems - to be honest, it's always great to get someone else's viewpoint - and it gives my logical mind a workout occasionally!

Re: how to use a javascript variable in a php page.

Posted: Fri May 07, 2010 6:56 am
by amitdubey2
hey hypedupdawg,

when you are making total as a global variable it is nt working properly...can you please again give a look for that thing..
This total is coming as some random number...








regards
amit

Re: how to use a javascript variable in a php page.

Posted: Fri May 07, 2010 9:20 am
by hypedupdawg
hypedupdawg wrote:However, at the moment it will only display NaN until you have the price list set up
I reiterate the above - any problems issuing from the code will only be because there is no database attached to the other end - however, if you attach me the files again (I have deleted them) I will have a look at any price lists you are using to see where the problems are coming from.

Also - could you be more specific than just saying "random numbers"?

- Give me an example (or more than one).
- Tell me what you have done to the files since they were last working.
- How are you adding prices to the site at the moment?

Re: how to use a javascript variable in a php page.

Posted: Fri May 07, 2010 2:34 pm
by amitdubey2
Thanks For concern hypedupdawg,

Actually for this script i dont want any database to be attached in it...i am using addproduct.php script for adding the products and their prices....(actually it is a small project which requires no database, for database i am also making a shopping cart which is about to complete)....

The problem is that when i am making the variable totalPrice Global, it is redirecting the total to billing page but its calculation is like ........it is adding the price of first product which we select .............to every product which we select after the first one...( you can see this in fly-to-basket.html page where i mentioned 8 teams as product list...simply add these teams to cart and check the total each time you will see the difference)
and if we are nt making totalPrice Global, It is giving the right total but then it is nt redirecting the total to billing page...
i m nt sure how to modify the code so that it should work properly for total as well as redirect it to billing page...
I am uploading the files you modified earlier please go through them.
Waiting for your reply ...Thanks


Regards
Amit