Re: Embedding HTML Within a PHP Statement [message #175985 is a reply to message #175984] |
Mon, 14 November 2011 10:57 |
Erwin Moller
Messages: 228 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 11/14/2011 11:39 AM, Tim Streater wrote:
> In article <mhg1c7p6qe5ng95rfiguu1b0tu18143m4a(at)4ax(dot)com>,
> Call Me Tom <noemail(at)nowhere(dot)com> wrote:
>
>> I was under the impression that a section of PHP code (the part
>> between <?PHP and ?>) was independent of any other section of PHP
>> code. Today I found the following code in a working program:
>>
>> <?php if (!empty($error_message)) { ?>
>> <p class="error"><?php echo $error_message; ?></p>
>> <?php } ?>
>>
>> This is the first time I have seen a section of PHP code end in the
>> middle of a PHP statement and restart after some HTML was inserted. I
>> was surpised that the PHP interpreter was able to connect the two
>> sections of PHP code.
>>
>> My question is not how the interpreter does it (that's way beyond my
>> level of knowledge) but rather 1. Does this only work in special cases
>> or does it work in all
>> cases?
>> 2. Is it considered good programming, bad programming or personal
>> preference?
>
> Perfectly valid. I used to use that approach, but I find it makes it too
> hard to keep it clear what I am doing. I have separate PHP, html, and JS
> files. I've given up on using php to directly build pages in that way.
>
> What I do instead is to use ajax to collect some data from a php script
> and then use JS to interpret that data and adjust the page as necessary.
>
> E.g. you might imagine that the user makes a selection. Instead of
> submitting the page and then presenting them with an error message, far
> better IMO to have a <div> that is normally empty, use ajax to send the
> user's request to a PHP script. The PHP script attempts to perform the
> user's request and then returns a results message (based on
> success/failure, for example), that you can present in the <div> using
> JavaScript. Much cleaner and much better from the user's PoV.
>
Hi Tim,
I don't think that approach is very recommendable.
At least not as a general recipe.
A few drawbacks:
- Your full ajax approach results in multiple requests to the server
(where one request would suffice without AJAX.).
- It also demands the client to have Javascript enabled.
- Many searchengines and their associated crawlers don't execute
Javascript, so they will be blind for that fetched content.
- In many cases the back button will give unexpected results, and
bookmarking a page becomes a guessing game.
And there are more drawbacks.
I also fail to see why you claim "much cleaner for the user POV".
Regards,
Erwin Moller
--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
|
|
|