CQM
CQM

Reputation: 44228

PHP website basics, some clarification please

I currently am using PHP to render a dynamic JS+CSS+HTML website via echo statements. The PHP is filling in some of the JS variables.

I want to expand this to include If/Else statements but I have some questions about how PHP interacts with the rendered page

After the site is rendered (all the echo statements have printed) can I still call additional PHP functions? For instance, if I click something can I have that call a PHP function?

Feel free to point me toward PHP tutorials that explain how PHP interacts with the site. Ironically I have done a lot of PHP coding for other tasks I just never thought about it from the ground up

Upvotes: 0

Views: 142

Answers (3)

bpeterson76
bpeterson76

Reputation: 12870

I've learned in coding to never say never....

Aside from standard Ajax, there --IS-- a way to run PHP functions asychronously: xajax In a nutshell, it allows you to call PHP functions directly without reloading the page. A word of warning-- the documentation is weak....

PHP is server side technology as others have mentioned. Getting it to run asynchronously (or after the main page has rended, as the case may be) is only facilitated with some Javascript voodoo, which is what xajax brings to the table. It does a "good enough" job making this possible. I personally have chosen to use Jquery because of the sheer power it brings for UI development. There's tons of options out there to make Ajax calls.

The key to moving your understanding forward is thinking about coding for the FRONT END. When I was beginning my career I was solely a back-end guy, focusing on the standard if/thens/elses and echoing out the relevant code. Once it was on the screen, I was done with it. Moving over to concentrate on the front end requires understanding of the dom structure -- what you'll be using to "grab" elements and then understanding of tech such as javascript to "do stuff" with your data. In a nutshell, it's just a different way of doing what you're used to with a slightly different syntax. Rather than just jumping in feet first to tackle Ajax or this specific task, take some time to familiarize yourself with the basics of traversing the Dom, and perhaps some Javascript or Jquery basics as well. It'll make your job much easier in the long run.

Upvotes: 0

adam_LDN
adam_LDN

Reputation: 56

Basically once the PHP script has finished running and printed (echoed) all the html content to the browswer then it stops running.

Essentially if you are clicking on a web page then that can either call a new page via a form POST or a GET OR maybe javascript can handle the click. And then that Javascript can perhaps perhaps trigger a new request. And that new request can call a PHP script which performs whatever task you want it to.

So if you click on an <a href='action1.php?param1=yes'>Click here</a> link then that will call the script action1.php (with $_REQUEST['param1'] set to 'yes') on the webserver and the webbrowser displays anything it returns. And that new html replaces all the html currently in your browser window.

OR if you have a form:

  <form action='action2.php' method='POST'>
    <input type='text' name='stuff'>
    <input type='submit>
  </form>

And you click on submit then again the webserver will be called but this time the script action2.php will be called with $_POST['stuff'] set to whatever is in the text field. And whatever the script returns will be what is displayed in the browser.

Now if you want to click on something in the browser and just change something small on the page or just perform some action on the server then you should probably investigate AJAX handlers and jQuery in particular

Good Luck.

Upvotes: 2

rid
rid

Reputation: 63472

PHP does not interact with the site. PHP's only function is to output text to the browser.

Look at it this way: if you had no PHP, the website would be composed of HTML pages. HTML pages can contain JavaScript and the user can interact with them. The difference with PHP though is that you can generate these HTML pages dynamically (which by association means that you can also generate the JavaScript contained in the HTML pages dynamically). Nothing else has changed.

Upvotes: 3

Related Questions