Does php load BEFORE the 'html body'? - php

Difficult to explain this Question, but im currently passing variables in a php page to some html hidden inputs.
Im fetching those values from the hidden inputs with a javascript function. This function gets called like this:
<body onload="function();">
It works on my system now, but is there any chance that the value passed from php might not get through because body has called the function BEFORE the php code sets the input type hidden?
Thanks

You have may have mixed up which part does what.
PHP generates the HTML page on the server side. When the HTML page arrives at the browser, PHP has done its job. There is no way for PHP to do something after it has rendered the HTML.
Javascript is executed in the user's browser after the page has been generated and loaded. (Or during; as theraccoonbear points out, Javasript can run in the browser before the page has loaded completely.)
A Javascript command can not communicate with the PHP script rendering the page, because when Javascript comes into play, PHP is already gone.
So the answer to your question is: No, the JS function can not execute before PHP is done. As several commentators point out, that is not entirely true. A Javascript could come into action before the input HTML elements have been rendered. In your example however, the Javascript triggers only when the document is completely loaded. In that constellation, the answer is no, it can't happen.

That shouldn't be an issue, as you are using the body's onload property, which will ensure the dom and all images etc have loaded.
Using jQuery to it like below would be better in my opinion, fires as soon as the dom is ready, rather than waiting for all images etc.
$(document).ready(function() {
// do stuff here
});
This is also easily done from an external JS file if required, which helps you logically separate your code.

Related

Get HTML ID in PHP

I am new to PHP and I am wondering if I can access an ID inside HTML and do some editing?
Inside my html file, I have this:
<div id="Msg" class="message"></div>
and normally I would edit the text on the client side in javascript using the following:
document.getElementById('Msg').innerHTML = '<font color="red">Some text...</font>';
Can something like this be done on the server side with PHP? I would like to alter the element before I render the page. If so, what should I use to access the ID and edit the elements?
You can use PHP DOM Extension.
See documentation on extension or direct link getElementById
But, it's backend processing.
If you mean altering the HTML via PHP after it's been rendered in the user's browser, that can't be done.
PHP is server-side, which means it runs on a machine different from the user.
Javascript and HTML are client-side, which means they run on the user's machine.
The server should have no control over anything on the user's machine.
Therefore, accessing the ID from PHP is not possible. :)
You've got to stick with Javascript, which only runs client-side.
If you want to alter the HTML structure before you send to the client, that's possible using the DOM, as #DmitryBergstein points out in his answer.
If you need to access something on the server after the page has been rendered, you need to make an ajax request, then alter the DOM using Javascript after the request has returned some data.
PHP generates HTML before sending it to the client.
You need to figure out where in the PHP this HTML is being generated.
If you really want to update an innerHTML content with PHP once the page is fully loaded, you should have a php script that returns an updated value. Then, you call this script and update your page according to the response. It can be done nicely with an ajax call.
$message = htmlspecialchars($_POST["Msg"]);

PHP, HTML, Javascript execution order

I have a test.php file and this file contains some PHP code, HTML elements and some internal JavaScript and some external JavaScript include.
I want to know which is first to load or execute.
PHP or HTML or JavaScript? I want to know execution order.
Your answers are greatly appreciated and very helpful to me and others also.
Pragmatically speaking, this is the typical order:
PHP runs first and constructs the page.
The browser loads the resulting HTML (any JavaScript found gets executed immediately)
Any JavaScript that was tied to the DOM ready or load event gets executed once the whole HTML is read and all objects are loaded respectively.
PHP will execute first, then HTML and finally javascript.
You send request to server, server executes your script
Then returns rendered html to browser, browser parses HTML(inline javascript executed)
Finally executes external included javascript files, one by one in order they are included.

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
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.
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.
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.

Running Script before/after Variables declared?

As i understand javascript .js files are best to put all the way at the bottom of html pages, to speed up loading of rest of page. Advised by Yslow(Yahoo) and Page Speed(google).
Now, when in the middle of page some thing RUNS a javascript script, in Internet Explorer, i see a small warning message saying that the element is: Uncaught ReferenceError: SWFObject is not defined
When i put my all.js file in the had, the error goes away but page load slows doen. What to do?
Actually, i remember it was the same with php variables. If i RUN php but the variable comes later, then it just doesnt work. must define the variable first, for it to run.
How to make this workflow better, in case of php scripts? and in case of javsscripts?
Thanks!
You should put your library scripts that are external in the head (things like swfobject, jquery, etc.). But the actual function call you make (for example to bind an event with jquery, or to initialize a swfobject embed) should go at the end.
This made even esier if you keep calling global functions outside of an event handler to a minium and dont use inline javascript or global variables.
What is that "some thing" that runs javascript in the middle of the page?
We do not use <script> tags, and all javascript code we put is js files, which are loaded in strict sequences (so I definitely know that when code is executed, everything it uses is there). (ok, to speed up page loading we append all files into few, like probably you do, "all.js")
If you use script in html attributes (like onchange events etc.) then try to use unobtrussive javascript (attach your events from javascript files).
If that does not help, then divide your javascript into few parts - minimum needed to load the page and execute some stuff before other part is loaded (in <head> of page). Bigger part of scripts you will load before </body>

PHPLiveX and loading html into DIV

I am using an JavaScript (and AJAX) to dynamically load a PHP page into a DIV, (when a hyperlink is clicked the div gets code from an external file loaded into it without the page refrshing).
The problem i am having is that when i use PHPLiveX (an AJAX framework for PHP) within the DIV it does not work, however when i load the page seperately it does. PHPLiveX creates JavaScript at runtime and puts it within the page body. This may be why the page does not work but i am not sure.
Sorry if this is badly explained. Thanks.
If I understand (and assume) correctly:
You are inserting content into a page using innerHTML
The content includes JavaScript, which isn't being recognised
This is one of the limitations of innerHTML. I'd rewrite it so the scripting was implemented differently. (The specifics would depend on what the script did in the first place)

Categories