I
I
Igor2012-02-01 10:10:19
PHP
Igor, 2012-02-01 10:10:19

How to execute html code before php execution?

There is a php file. Its appearance is approximately the following (I replaced the angle brackets with square brackets, because Habr does not like them.)

[html]<br/>
<br/>
[script type=&quot;text/javascript&quot;]<br/>
тут тело скрипта<br/>
[/script&gt;]<br/>
[/html]<br/>
<br/>
&lt;?php<br/>
sleep(5000);<br/>
php-код<br/>
?&gt;

When this file is executed in the browser, the php code is executed after 5 seconds, the html code does not work at all (viewing the page source code through the browser shows emptiness). If you remove all the php code, the html code works fine and can be seen through viewing the html page.
There was an attempt to solve the problem in this way:
&lt;?php<br/>
echo='[html]<br/>
[script type=&quot;text/javascript&quot;]<br/>
тут тело скрипта<br/>
[/script]<br/>
[/html]';<br/>
sleep(5000);<br/>
php-код<br/>
?&gt;

But it didn't change anything at all.
What is the problem? Where to dig?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
0
0xA0, 2012-02-01
@shanker

The final solution might look something like this:

[script>
$(document).ready(function()
{
$("#redirect").load('index.php');
});
[/script>
[html>
[div id='redirect',name='redirect'>
[/div>
[/html>

D
demark, 2012-02-01
@demark

There is no problem here . There is a misunderstanding of how different entities work - server and client.
php is executed not in the browser, but on the server and sends the sourcing to the browser through the web server, and then javascript (if enabled) comes into play in the browser.
in your case, the only way to execute js first is to load the page, and then use Ajax to make a request to the server to the php script.

A
Andrey Privalov, 2012-02-01
@negasus

From the generated html page using ajax, after the necessary pause (setTimeout), pull the desired php script and do what you need.
Sleep, etc. - crutches, which are better not to use.

M
MastaEx, 2012-02-01
@MastaEx

Your main problem is that sleep(5000) is not 5 seconds, but 5000 seconds. Wait 5000 sec. and you will have html :-)

Z
zapimir, 2012-02-01
@zapimir

Read what an output buffer is and its management functions ( flush , ob_flush , etc.)

M
Melkij, 2012-02-01
@melkij

Run long running processes in a separate thread is better.
The view of a tab hanging in the “loading” status for an hour and a half (5000 seconds) is somehow completely unattractive.

A
Alexander, 2012-02-01
@Beketov

Try creating (or adding to an existing) .htaccess file
With the line:
RemoveHandler .html .htm
AddType application/x-
httpd-
php .php .htm .html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question