Passing variables between Javascript and PHP

Discussion in 'Javascript' started by q-rious, Aug 19, 2008.

  1. q-rious

    q-rious Guest

    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 directlyso I can not
    pass them to PHP 
    as:

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

    3. xyand b are not static.

    My questions are:

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

    Thanks.
     
    q-rious, Aug 19, 2008
    #1
    1. Advertising

  2. q-rious

    Evertjan. Guest

    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 directlyso I can not
    pass them to PHP as:
    >
    > <
    script source="myPHPfile.php?var1=x&var2=y" type="text/javascript"></
    script>
    >
    3. xyand b are not static.
    >
    My questions are:
    >
    AWhat are ways to pass variables from Javascript to PHP under these
    conditions?Real code example will be much appreciated.[/color]

    PHP being a language on the serveryou cannot "pass to" PHP from the
    client 
    [if you menn thatunless 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.
    [color=blue]
    > B. Same questions as above but for PHP to Javascript direction.[/color]

    Off topic on this NG. But simple.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Aug 19, 2008
    #2
    1. Advertising

  3. q-rious

    optimistx Guest

    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?
     
    optimistx, Aug 19, 2008
    #3
  4. q-rious

    Evertjan. Guest

    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)
     
    Evertjan., Aug 19, 2008
    #4
  5. q-rious

    q-rious Guest

    On Aug 19, 11:03 am, "Evertjan." <>
    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?
     
    q-rious, Aug 19, 2008
    #5
  6. Gregor Kofler, Aug 19, 2008
    #6
  7. q-rious

    q-rious Guest

    On Aug 19, 1:23 pm, Gregor Kofler <> 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.
     
    q-rious, Aug 22, 2008
    #7
  8. q-rious wrote:
    > On Aug 19, 1:23 pm, Gregor Kofler <> 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
     
    Thomas 'PointedEars' Lahn, Aug 24, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajive Narain
    Replies:
    0
    Views:
    1,936
    Rajive Narain
    Sep 18, 2009
  2. Gung Ho
    Replies:
    2
    Views:
    142
    Fatbob
    Nov 28, 2003
  3. Nautilus
    Replies:
    2
    Views:
    170
    Nautilus
    Oct 13, 2004
  4. Replies:
    3
    Views:
    217
    frothpoker
    Sep 4, 2006
  5. Replies:
    1
    Views:
    144
Loading...

Share This Page