Forum Moderators: coopster

Message Too Old, No Replies

How to get variables from javascript to php

         

iflooring

11:50 pm on May 31, 2006 (gmt 0)

10+ Year Member



I have a yahoo store and what I want to do is to track & get some information.

Yahoo allow to get the variables or some info from the confirmation page like price and other stuff.

In their help page it says that you can get some info from this:

<script language=javascript>
<!-- // variables containing order information
var orderSubTotal = 75.00;
var orderTotal = 100.00;
var orderNumber = nnn;
var numOfItems = 2;
var items = new Array('itemid1','itemid2','itemid3','itemid4','itemid5');
var ids = new Array('te');
var codes = new Array('345');
var qtys = new Array(5,2,1,1,1);
var price = new Array(15.12,2.00,1.00,2.12,1.50);
--> </script>

as follows I put my script at the confirmation:

<img width=1 height=1 src='http://www.mydomain.com/conversion.php?t=Sale&a=$price&w=$orderNumber'>

My question is how can I put the variable in my script.

Can somebody help me with this issue?

coopster

3:20 pm on Jun 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How can you put what variable in your script? Are you asking how to get something like the
price
from a JavaScript variable into your query string?

iflooring

5:00 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



yes sir, sorry about that.. what I mean is how can I get the price and the order number on the javascript.

coopster

5:50 pm on Jun 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh, OK. I think I was looking at it backward. So, you have a link and you want the page that you end up on in your domain there to pull have the values from the QUERY STRING and write them into the JavaScript. You simple echo the values in that script. So, somebody clicks the image link you specified:

<img width=1 height=1 src='http://www.mydomain.com/conversion.php?t=Sale&a=$price&w=$orderNumber'>

And in your

conversion.php
script you would do something like the following in your JavaScript:
<script language=javascript> 
<!-- // variables containing order information
var orderSubTotal = 75.00;
var orderTotal = 100.00;
var orderNumber = <?php print $_GET['orderNumber'];?>;
var numOfItems = 2;
...
--> </script>

Now, I would probably scrub that $_GET data prior to putting it into the JavaScript. You know, make sure it is all digits, no goofy stuff going on, etc.

iflooring

7:08 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



Sorry if I wasn't clear before. I am trying to find a way to transfer data from Yahoo that is stored in the previous JavaScript variables (namely the price variable) to my external PHP program via the image I have placed on this page:

<img width=1 height=1 src='http://www.mydomain.com/conversion.php?t=Sale&a=$price&w=$orderNumber'>

I have tried several ways to call the variables in the image tag but none have worked yet.

These are the syntaxes that I have tried:

<img width=1 height=1 src='http://www.mydomain.com/conversion.php?t=Sale&a=$var price&w=$var orderNumber'>

<img width=1 height=1 src='http://www.mydomain.com/conversion.php?t=Sale&a=var price = new Array(15.12,2.00,1.00,2.12,1.50)&w=var orderNumber = nnn'>

How can I transfer these JavaScript variables to my PHP program?

Thanks.

grandpa

7:42 pm on Jun 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure if this will help you.. Take a look at this bit of JS code in one of my scripts. The value of the JS variable total is returned to my form to the icount field, a text field. (Also, I seem to remember having some difficulty with this, and the solution was to echo the JS function from PHP?) The bold lines may be what you are looking for...

echo "<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">\n";
echo "function myfunction(form) {\n";
echo "var total = 0;\n";
for ($i = 1; $i <= $num_rows; $i++) {
echo "var max$i = form.VARquantity$i.value;\n";
echo "var num$i = max$i * 1;\n";
echo "total = total + num$i;\n";
}
echo "document.Add1.icount.value = total;\n";
echo "}\n";
echo "</script>\n";

Now that I have the value of total (via icount) in my form, it's available to the rest of my script. I hope this helps.

For grins, here is the relevant form:
<form action="http://www.domain.net/" method="POST" name="Add1">
<input type="text" name="icount" size="2" value="0">
<input type="submit" name="I1" value="Continue" onClick="myfunction(this.form)">
</form>

coopster

3:58 pm on Jun 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



All right, so I did understand it correctly the first time. However, the image is going to load when the page loads (which includes the current JavaScript variables). If the information is in the JavaScript source already in the page, why not build the image link on the server-side as well? Am I missing something here?

Anyway, if you truly need to build the image on the client side you can do so by *rebuilding* the src attribute value:

<script type="text/javascript"> 
<!-- // variables containing order information
var orderNumber = 123
var price = 1.50
-->
</script>
</head>
<body>
<p>
<img width="150" height="150" id="myLink" src="">
<script type="text/javascript">
var url = 'http://www.example.com/conversion.php?'
url += 't=Sale&'
url += 'a=' + price + '&'
url += 'w=' + orderNumber
document.getElementById('myLink').src = url
</script>
</p>

iflooring

12:51 am on Jun 3, 2006 (gmt 0)

10+ Year Member



Thank you sir Coopster and Grandpa. I will try to make it work..

grandpa

1:10 am on Jun 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd like to know how it works out. This is going to have to be returned to a document element, a form, which is client side. You might want to look at AJAX for a solution.

iflooring

7:31 pm on Jun 8, 2006 (gmt 0)

10+ Year Member



Thanks coopster! It works!

Sorry, I just reply today because I'm kinda busy working on other projects.

The price was working but the order number doesn't. Do you think there is something missing in the order number?

<p>
<img width="150" height="150" id="myLink" src=""> <script type="text/javascript">
var url = 'http://www.example.com/conversion.php?'
url += 't=Sale&'
url += 'a=' + price + '&'
url += 'w=' + orderNumber
document.getElementById('myLink').src = url </script>
</p>

coopster

8:30 pm on Jun 8, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Works fine for me. Did you remember to define the variable in your <script> somewhere as shown in the example earlier?
var orderNumber = 123