Jump to content

Quick Question About PHP vs JavaScript for making live changes to a site page


jimmyoneshot

Recommended Posts

In one of my pages I have a section which contains a hidden line of text. When the user clicks a button the box containing the text will expand showing the line more clearly. I want this to happen live i.e. no page refresh/reload.

 

I've seen such things done on other sites but my question is, is this done using a php function or is JavaScript required to achieve this?

 

I assume that in order for any php functions to take place there must be a page refresh to reload the data/parameters/variables that are required?

 

The site I'm referring to is this one:-

 

http://www.vouchercodes.co.uk/featured-voucher-codes.html

 

If you click on the "Get code and open site" buttons there for any of the vouchers the area expands live and the voucher is revealed however as I say there is no page refresh and it is done live.

 

Is this effect done in JavaScript and not php or is there some way of using php alone to do something similar?

 

Obviously including JScript in a site is more risky as if a user does not have it installed/activated then they may not be able to use such elements hence the reason I want to know how it is done.  :confused:

Link to comment
Share on other sites

I find it helps if you understand the way browsers work in regards to the HTTP protocol.  You might want to read up on that a bit.

 

In a nutshell, PHP is "serverside".  PHP is for all intents and purposes, the same thing as the web server.  The browser makes requests, the webserver returns HTML.  The HTML may specify other parts of the HTML page like images and javascript and css files.  All the server does is send those things when requested.

 

It is the job of the browser to parse the html, take all the components and assemble them into the page that gets displayed.  This is all done "clientside".  To be totally clear, once the server has sent what was requested, it immediately closes the connection with the client and goes on to process the next request, if there is one. 

 

So when you consider this flow, it should be clear that the client initiates these conversations by making requests and the server only answers.

 

The last few years, it has become common for applications to utilize javascript, thanks in a large part to the emergence of cross-browser compatible libraries like jquery.  Any interactivity on the client that doesn't involve a plugin (flash or java) are created through javascript.  This code runs clientside, meaning it is delivered by the server as source code, and it's up to the browser to interpret and run that code.

 

Via javascript you can implement a rich UI where you add or modify elements on the fly, and handle client events like keypresses and mouseclicks.

 

If the application requires communication with the server, you can utilize ajax, which is a feature of javascript that allows a persistent connection to be made from the javascript code running in the browser, to a server.  It sounds like your feature might require the use of ajax.  The implementation details for ajax connections differ between browsers, so again using a javascript framework like jquery is highly advisable.

 

The ajax will let you respond to an event, make an ajax connection to the server, and allow the retrieval of data in a variety of formats (typically either xml or json).  The javascript code can then dynamically modify the rendered html page.

 

This still requires a serverside (PHP) script which, given pertinent input, will return the appropriate data. 

 

So the answer to your question is:  there is both javascript (DHTML and Ajax code) AND serverside PHP code required.  Since you're clearly new to this, I would suggest you google some jquery tutorials and start there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.