Passing variables between Javascript and PHP

Q

q-rious

Hello All,

1. I would like to pass some variables (x and y below) from Javascript
to PHP, process them in PHP, and return some other variables (a abd b)
back to Javascript.

[JS] --(x,y)-->
PHP:
 --(a,b)--> [JS]

2. The variables x and y are not known to HTML directly, so I can not
pass them to PHP as:

<script source="myPHPfile.php?var1=x&var2=y" type="text/javascript"></
script>

3. x, y, a and b are not static.

My questions are:

A. What are ways to pass variables from Javascript to PHP under these
conditions?Real code example will be much appreciated.
B. Same questions as above but for PHP to Javascript direction.

Thanks.
 
E

Evertjan.

q-rious wrote on 19 aug 2008 in comp.lang.javascript:
1. I would like to pass some variables (x and y below) from Javascript
to PHP, process them in PHP, and return some other variables (a abd b)
back to Javascript.

[JS] --(x,y)-->
PHP:
 --(a,b)--> [JS]

2. The variables x and y are not known to HTML directly, so I can not
pass them to PHP as:

<script source="myPHPfile.php?var1=x&var2=y" type="text/javascript"></
script>

3. x, y, a and b are not static.

My questions are:

A. What are ways to pass variables from Javascript to PHP under these
conditions?Real code example will be much appreciated.[/QUOTE]

PHP being a language on the server, you cannot "pass to" PHP from the
client [if you menn that] unless you pass it to the server.

Try a javascript image object:

var myImage = new Image();
myImage.src = "http://mydomain.xxx/myfile.php?x=" +x+ "&y=" +y";

Or use Ajax.
[QUOTE]
B. Same questions as above but for PHP to Javascript direction.[/QUOTE]

Off topic on this NG. But simple.
 
O

optimistx

Evertjan. wrote:
....
PHP being a language on the server, you cannot "pass to" PHP from the
client [if you menn that] unless you pass it to the server.

Try a javascript image object:

var myImage = new Image();
myImage.src = "http://mydomain.xxx/myfile.php?x=" +x+ "&y=" +y";

Or use Ajax.
B. Same questions as above but for PHP to Javascript direction.

Off topic on this NG. But simple.

Sending data from js to a server is on topic, receiving from the server to
js is off topic? Why?
 
E

Evertjan.

optimistx wrote on 19 aug 2008 in comp.lang.javascript:
Evertjan. wrote:
...
PHP being a language on the server, you cannot "pass to" PHP from the
client [if you menn that] unless you pass it to the server.

Try a javascript image object:

var myImage = new Image();
myImage.src = "http://mydomain.xxx/myfile.php?x=" +x+ "&y=" +y";

Or use Ajax.
B. Same questions as above but for PHP to Javascript direction.

Off topic on this NG. But simple.

Sending data from js to a server is on topic, receiving from the
server to js is off topic? Why?

Because that is simply rendering the html stream by php in this case,
[unless you are using Ajax.]

In the ASP case it cou;ld be on topic when serverside jscript is used.
 
Q

q-rious

optimistx wrote on 19 aug 2008 in comp.lang.javascript:




Evertjan. wrote:
...
PHP being a language on the server, you cannot "pass to" PHP from the
client [if you menn that] unless you pass it to the server.
Try a javascript image object:
var myImage = new Image();
myImage.src = "http://mydomain.xxx/myfile.php?x=" +x+ "&y=" +y";
Or use Ajax.
B. Same questions as above but for PHP to Javascript direction.
Off topic on this NG. But simple.
Sending data from js to a server is on topic, receiving from the
server to js is off topic? Why?

Because that is simply rendering the html stream by php in this case,
[unless you are using Ajax.]

Just to confirm, you mean something like this from the PHP side:

echo "[all javascript here]";

Right?
 
Q

q-rious

q-rious meinte:
Just to confirm, you mean something like this from the PHP side:
echo "[all javascript here]";

Yes.

Gregor

--http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com     ::: Bildagentur für den alpinen Raum

Thank you all for helping me out. I tried to use JS Image object, but
I do not think this is working for me. The PHP code (loadXML.php) is
working for me correctly standalone. It correctly writes low, high
etc. in that case.

But when I use the following code, image.complete is never true.

var waitFor = function ( params ) {
var condition = params.condition;
var callback = params.callback;
var interval = params.interval || 100; // try every 100 millisec
var maxTries = params.maxTries || 5; // maximum 5 tries
var currentTry = params._currentTry || 0; // private
// If condition passes, run the code
if ( condition() === true )
return callback();
// Limit the # of attempts
if ( currentTry < maxTries ) {
// Increment the attempt #
params._currentTry = currentTry+1;
// Create the recursive call
var f = function() { return waitFor( params ); }
// Wait for one interval and execute
setTimeout( f, interval );
}
else {
alert( 'Maximum tries used for waitFor()...quitting' );
}
};

function loadXMLPhp (str_val) {
var low, high, icon, condition;

//------------------ PHP STARTS HERE ------------------------
var myImage = new Image();
myImage.src = "loadXML.php?thisZip=" + str_val;
//------------------ PHP ENDS HERE ------------------------
waitFor ( {
condition: function() {
return myImage.complete === true;
},
callback: function () {
// do not do anything
}
} );
alert('Low = ' + low + ', High temp = ' + high);

}

Thanks.
 
T

Thomas 'PointedEars' Lahn

q-rious said:
q-rious meinte:
Just to confirm, you mean something like this from the PHP side:
echo "[all javascript here]";
Right?
Yes.
[...]

Please trim your quotes to the minimum required to retain context;
do not quote signatures (unless you refer to them).
[...] I tried to use JS Image object,

There is no such thing really. `Image' was part of the JavaScript
programming language from version 1.1 to 1.3 only because JavaScript was
associated with a single DOM API then, that of Netscape Navigator.

Since JavaScript 1.4, host objects like `Image' have been removed from the
JavaScript programming language, its reference material thus being renamed
from *Client-Side* JavaScript Reference (1.3) to *Core* JavaScript Reference
(1.4+). While those host objects are still supported for backwards
compatibility and lack of alternatives, the Gecko DOM provides the API instead.

In other ECMAScript implementations, such as Microsoft JScript as
implemented by MSHTML/IE, (until proof to the contrary is presented) this
feature and other features have never been part of the programming language
but of the object model of one corresponding host environment, the MSHTML
DOM, instead.
but I do not think this is working for me. The PHP code (loadXML.php) is
working for me correctly standalone. It correctly writes low, high
etc. in that case.

But when I use the following code, image.complete is never true.

So as `Image' is a host object and not part of any public standard, it is
implemented differently by user agents, and several of its properties,
including `complete', are either deprecated or obsolete nowadays.

Since you have neither told which environment (browser) you have tested with
nor have you posted the URL of a test case (and so there is no way to refute
or back up your claims), one has to assume that either loading the image
never completes or that the `complete' property is unsupported there. If
the latter, there are two likely possibilities: either it would yield `true'
always, such as in Fx 3, or `undefined' which is never strictly equal to `true'.

Your best chance of detecting whether an Image object has completed loading
the image data is to use the proprietary `onchange' event handler property
instead:

var img = new Image();

img.onload = function() {
window.alert("image loaded");
};

img.src = "...";

(You could also try the standards-compliant `load' event instead, but the
MSHTML DOM does not implement W3C DOM Level 2+ Events and the corresponding
methods.)
//------------------ PHP STARTS HERE ------------------------
var myImage = new Image();
myImage.src = "loadXML.php?thisZip=" + str_val;
//------------------ PHP ENDS HERE ------------------------

This is not PHP code.


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top