Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post

    Can php talk to javascript, or does it just echo it to the browser?

    I know ajax is a combination of javascript that is sent to the browser ( so it can run on it) and a cgi language like php that runs on the server.

    I know the js on the browser posts 1 or more key/value pairs to the php script, which echo's something back to the javascript. That something echoed back becomes the responseText part of the javascript code that did the post.

    Is is true that the data the php script echo's back must go back to the same block of javascript on the same browser that did the post to the php script?

    I'm looking for a way to use the js part of ajax on my own browser to click a button that tells the js to post something to a php script on my server.

    That php script would tell the js, that is sitting on the server waiting to be downloaded by a browser sometime in the future, to change the instructions it will give to a browser that downloads it.

    Is this possible?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by milesdriven View Post
    I know ajax is a combination of javascript that is sent to the browser ( so it can run on it) and a cgi language like php that runs on the server.
    AJAX is not sending JavaScript, it is sending only data (as you mentioned below).


    Quote Originally Posted by milesdriven View Post
    Is is true that the data the php script echo's back must go back to the same block of javascript on the same browser that did the post to the php script?
    since it is a HTTP request, the response must go back to the same device, i.e. the same browser. but I wouldn’t call it the "same block of JS", you have an XHR object that handles Request and Response, nothing more, nothing less.


    Quote Originally Posted by milesdriven View Post
    I'm looking for a way to use the js part of ajax on my own browser to click a button that tells the js to post something to a php script on my server.
    look into Event handling (that has nothing to do with AJAX, though).


    Quote Originally Posted by milesdriven View Post
    That php script would tell the js, that is sitting on the server waiting to be downloaded by a browser sometime in the future, to change the instructions it will give to a browser that downloads it.
    ???

    what does that mean?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    what does that mean?
    Javascript sits idle on in a file on a server. I say idle because php doesn't parse it. When the browser downloads the file, php echoes that javascript to the browser, which does interpret and run it.

    That javascript has what I called "instructions" in it for any browser that goes to the server. I use the word instructions because javascript is used to tell the browser what to do. It could tell the browser to display a clock, etc.

    Some of those instructions can be toggled, like the value of a css declaration. I was looking for a way to change that value to something else( like display: block, in the javascript file as it sits idle on the server just by clicking an ajax button on the browser I'm using.

    I think the only way to do this is to use a combination javascript, php and a mysql table. The table is the only real "connection" between 2 different browsers that log onto the same server.

    Thanks for your help

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    not sure if that helps ... you can of course write a PHP script that echos JS code as desired (i.e. depending on the URL parameters).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    I've never heard the term URL parameters and javascript in the same sentence before. Could you explain how URL parameters relate to javascript?

    BTW, I appreciate all the replies I get. I know it takes time and effort to read and reply.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by milesdriven View Post
    I've never heard the term URL parameters and javascript in the same sentence before.
    strange. they are one important base of client-server communication.

    Quote Originally Posted by milesdriven View Post
    Could you explain how URL parameters relate to javascript?
    if JS wants to send data to the server (via GET method) it needs to place the data into the URL, et voila you have URL parameters in JavaScript.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Got it. I've only used POST so far.

  • #8
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    www.domain.com/file.ext?param1=a&param2=b

    URL parameters can be accessed via document.URL. To get parameters, split document.URL using "?" as delimiter which will give you "param1=a&param2=b", split that using & as delimiter and place into an array, giving you "param1=a" and "param2=b", split those using "=" as delimiter and you've got your name/value pairs.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    I wish there were a native JS function for parsing URLs.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    I've been using POST because all of this is going over https. I've been studying POST so much, I didn't know what a URL parameter was - funny how things work sometimes.

    Thanks WolfShade and Dormilich.
    Last edited by milesdriven; 06-04-2012 at 02:35 PM.

  • #11
    Regular Coder
    Join Date
    Dec 2011
    Posts
    186
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Dormilich View Post
    I wish there were a native JS function for parsing URLs.
    There will be when enough people want it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •