Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing variables between Javascript and PHP

Reply
Thread Tools

Passing variables between Javascript and PHP

 
 
q-rious
Guest
Posts: n/a
 
      08-19-2008

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.

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      08-19-2008
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.


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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
optimistx
Guest
Posts: n/a
 
      08-19-2008
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?


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      08-19-2008
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
q-rious
Guest
Posts: n/a
 
      08-19-2008
On Aug 19, 11:03*am, "Evertjan." <(E-Mail Removed)>
wrote:
> 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?
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      08-19-2008
q-rious meinte:

> Just to confirm, you mean something like this from the PHP side:
>
> echo "[all javascript here]";
>
> Right?


Yes.

Gregor


--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
 
Reply With Quote
 
q-rious
Guest
Posts: n/a
 
      08-22-2008
On Aug 19, 1:23*pm, Gregor Kofler <(E-Mail Removed)> wrote:
> q-rious meinte:
>
> > Just to confirm, you mean something like this from the PHP side:

>
> > echo "[all javascript here]";

>
> > Right?

>
> 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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-24-2008
q-rious wrote:
> On Aug 19, 1:23 pm, Gregor Kofler <(E-Mail Removed)> wrote:
>> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Training Institute In Delhi, Live Projects on PHP. Short TermPHP Courses, PHP Scripts, PHP Training with Live Projects. Rajive Narain Java 0 09-18-2009 10:47 AM
passing php variables with javascript? dkirkdrei@yahoo.com Javascript 1 11-10-2006 03:35 AM
Writing a PHP and Javascript generated page to a PHP variable... jonnyblazed@gmail.com Javascript 3 09-04-2006 12:45 PM
Trouble passing JS parameter to PHP program /or/ executing the PHP Nautilus Javascript 2 10-13-2004 09:55 PM
Passing variables between ASP and Javascript Gung Ho ASP General 2 11-28-2003 03:28 PM



Advertisments