Forum Moderators: coopster

Message Too Old, No Replies

Print a javascript variable on a PHP page?

I am so stuck.

         

intwo

3:09 am on Mar 28, 2004 (gmt 0)

10+ Year Member



Hi, I'm new to javascript and thrashing my way through the code. But I can't figure out something. And I'm not even sure how to ask it. Here goes:

My free javascript shopping cart (NOPCart) creates a table on the checkout page from a javascript routine.

But I want to be able to carry those values forward, and I can't figure out how to do it. I'm on a PHP page.

Example:

Shopping cart returns a total order amount of, say, $99. It does this using a "drawcart()" function.

How do I add a line later that says:

"I authorize you to charge my card $99"?

I've tried <? echo="$Total"?> but it didn't work. Surely this is an easy thing to do, and its just my newbieness getting in the way. Please help!

(I'm happy to clarify if my question is confusing.)

Thanks!

I2

mep00

7:49 am on Mar 28, 2004 (gmt 0)

10+ Year Member



It's important to remember that the Web is stateless. In other words, once your server delivers a page, it forgets about it.

The problem is one of simulating state in a stateless enviroment. This can be one by passing varriables back to the server. But here in lies another problem: "How do you know that the information being passed back it correct?" The answer is that unless you can verify it, you don't.

In your case you need to verify a dollar amount. But since the only way to do this is to regenerate it, you gain nothing by passing the total back.

I assume that a list of items and quantities is already being passed back (how else would you know what they want to purchase). Use this information and recalculate the total. Otherwise it's possible for someone to manipulate the data so that an order of $1000.00 comes to a total of $1.

If you need a more gereral answer of how to pass back a value, please let me know.

intwo

8:29 am on Mar 28, 2004 (gmt 0)

10+ Year Member



In other words, once your server delivers a page, it forgets about it.

Yes, but it's already delivering the order total somewhere else on the page (in the table that shows the order total)... I just can't print that same variable elsewhere on the page.

In your case you need to verify a dollar amount.

Don't need to verify it. Just need it to appear elsewhere on the page. (Oh, and I also need to pass it forward through the credit card processing form.)

If you need a more gereral answer of how to pass back a value, please let me know.

Yes, I think maybe my question wasn't clear. I'm using a freeware javascript shopping cart script called NOPCart. It's default values are all fine, but on the checkout page, I need some of the results (the total dollar amount) to also show up elsewhere on the page. But I can't seem to figure out how to identify the variable's name, and then how to pop it into the text further down the page.

Any ideas? Thanks!

I2

mykel79

9:44 am on Mar 28, 2004 (gmt 0)

10+ Year Member



Yes, but it's already delivering the order total somewhere else on the page (in the table that shows the order total)... I just can't print that same variable elsewhere on the page.

Try <? echo "$Total"?> instead of <? echo="$Total"?>

Don't need to verify it. Just need it to appear elsewhere on the page. (Oh, and I also need to pass it forward through the credit card processing form.)

Well then, you do need to verify it. Someone could manipulate the javascript data and pass $1 to be billed from his account, as said before.

intwo

10:05 am on Mar 28, 2004 (gmt 0)

10+ Year Member



Try <? echo "$Total"?> instead of <? echo="$Total"?>

It didn't work. But then, I'm not sure I'm using the correct variable. The javascript function draws the cart on the page, and looking through the javascript file, I can't tell what the variable is. Where do I look?

Don't need to verify it. Just need it to appear elsewhere on the page. (Oh, and I also need to pass it forward through the credit card processing form.)

Well then, you do need to verify it. Someone could manipulate the javascript data and pass $1 to be billed from his account, as said before.

I understand what you're saying (personally, I don't care about potential manipulation -- I just wouldn't ship anything)... but can someone point me where to look to learn how to verify it?

Thanks for the ongoing help. Please keep it coming.

I2

mep00

10:31 am on Mar 28, 2004 (gmt 0)

10+ Year Member



It didn't work.

If you want the amount on the same page (no reloading) then it can't work. You'll need to use js to do it.

But then, I'm not sure I'm using the correct variable.
The wrong var is worthless.

The javascript function draws the cart on the page, and looking through the javascript file, I can't tell what the variable is. Where do I look?
In the func which draws the cart or the statment which calls it. It's likely that it is passed as a param. Also, since it needs a way to send info back to you, look in a <form>. You should also check the doc; they should tell you how to use the generated info.

You should also scan both inline js and any external linked files.

intwo

8:42 am on Mar 30, 2004 (gmt 0)

10+ Year Member



If you want the amount on the same page (no reloading) then it can't work. You'll need to use js to do it.

I'm not understanding. Is this because the variable is only appearing on this page as part of the drawcart() function? In other words, the file that tells it HOW to draw the cart would also need to tell it to draw the number later on down the page?

Is it not possible to have the javascript say "$variable is to be used anywhere on this page you reference it"?

The wrong var is worthless.

Don't I know it. <sigh>

It's likely that it is passed as a param.

Not sure what this means. A parameter is different to a variable? Is there a defining feature that would help me easily identify it?

You should also check the doc

I have, but I think my inexperience is getting in my way. Much of it is Greek to me.

I thank you for your help, but could sure use some more. Perhaps a pointer to a good primer on these sorts of issues? Cheers.

I2

mykel79

8:51 am on Mar 30, 2004 (gmt 0)

10+ Year Member



I may have not understood correctly the last time I posted - the amount is stored in a javascript variable or a PHP variable?
If it's a javascript one, then PHP cannot access it. It could only access it on the next page if it was passed as a parameter (like someone suggested). That means for example the next page would be loaded like this:
nameofscript.php?variable1=99

Then you could print out $variable1 when showing the page.

Once the page is shown to the user, PHP does nothing more with it - it's role ended on the server side. For it to show a variable, it has to be passed to it BEFORE the page is shown to the user.

mep00

11:32 am on Mar 30, 2004 (gmt 0)

10+ Year Member



I think my inexperience is getting in my way.
From the sounds of it, you might not be able to solve your problem without more programing experience. And without seeing the code it's very dificult to give you an exact answer on what you need to do.
A parameter is different to a variable?
Actually, yes, but that's not what I meant. A prameter is an expression of which a variable is but one type. But what I was trying to say is that if the value is being passed to the function as a prameter, then by checking the call statement you could find what the variable name is.
javascript say "$variable
"$variable" is a php variable and not a js variable. The only way for php to give js a value is by writing it into the js code when it generates the page. But I don't thing that's what you're looking for.
the file that tells it HOW to draw the cart would also need to tell it to draw the number later on down the page?
Something like that. See mykel79 answer above.

intwo

11:29 pm on Mar 30, 2004 (gmt 0)

10+ Year Member



the amount is stored in a javascript variable or a PHP variable?

mykel79, yes, it's in a javascript variable at the moment.

I think my inexperience is getting in my way.

From the sounds of it, you might not be able to solve your problem without more programing experience. And without seeing the code it's very dificult to give you an exact answer on what you need to do.

mep00, I learn by deductive reasoning, so I'm pretty sure I can figure this out, if not for a couple little holes in my logic.

I wonder if I can show you the code? It appears pretty simple, and I would think someone who knows what they're doing could spot my problem quickly. (If I'm asking too much, let me know.)

I've posted for you to see (wasn't sure the appropriate way to post code around here).

[writingfast.com...] (41kb)
is the shopping cart javascript code.

[writingfast.com...] (6kb)
is the sample form the processing gateway company provides.

[writingfast.com...] (6kb)
is the script the sample form invokes to process the payment.

I'm basically trying to marry the first and the second, so that the order total can appear on the checkout page *and* be invoked by the third as the order amount.

If you have a minute, I would be extremely grateful. This seems to me like a simple problem that many people would encounter.

Your efforts are very much appreciated.

I2