473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Passing variables between Javascript and PHP


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.

Aug 19 '08 #1
3 2276
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
Aug 19 '08 #2
On Aug 19, 1:23*pm, Gregor Kofler <use...@gregorkofler.atwrote:
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.
Aug 22 '08 #3
q-rious wrote:
On Aug 19, 1:23 pm, Gregor Kofler <use...@gregorkofler.atwrote:
>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
Aug 24 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Consuelo Guenther | last post by:
Hello, I am having problems with passing variables between pages. I have the following: First asp page has the function: -----------------------------------------------------------------------...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Jim Banks | last post by:
Greetings I'm opening a pop up window with a html form, (in one document) and I want to pass a variable to the html form called from the hyperlink. Here's the code I'm using to pop up the...
2
by: brianwmunz | last post by:
Hi, I'm having a problem passing a variable through a URL because the variable is supposed to hold a URL that has a variable of its own. Here is an idea of what I'm trying to do: href="javascript:...
6
by: Eric Johnston | last post by:
I want the visitor to enter three numbers on the page and then click a button "generate image" which will I hope cause a generated gif image to be displayed alongside on the page. This involved...
9
by: Max | last post by:
I'm new with Javascript and can't seem to figure out what I'm doing wrong here as I'm not able to pass a simple variable to a function. In the head of doc I have: <script...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
3
by: jsdeveloper | last post by:
Hi, I just started programming in javascript, and I'm having a problem passing variables between javascript and asp code in .asp page. Can you please help? I've given the sample code below. ...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.