I have a question, there is a way to update $error when his value change on external listPagPrinc.php?
<div id="statoPag">
<h3> Stato : <?php echo $error; ?> </h3>
</div>
<div class="headerCont">
<?php
include('procedure/listPagPrinc.php');
?>
</div>
Not as-is, no. Think of these HTML/PHP files like an office printer, once it prints out each line, you can't "go back" and print over it
In this example, all of the first 5 lines are run and effectively "set in stone" before anything is called in procedure/listPagPrinc.php.
If, and this is just speculation, you can't simply include procedure/listPagPrinc.php before you render $error because it also is printing additional HTML, you just need to encapsulate its code in functions as best as possible: One to set the value of $error, and a separate one to output the HTML you need.
You need to update the text content of the tag; you can do this with e.g. jQuery at runtime. This is the preferred way if some tags, and only those, change during the lifetime of the application page, and you do not wish to reload the whole page from scratch.
In this case, from listPagPrinc.php, you can output some Javascript code:
echo <<<JAVA1
<script>
alert("Ciao, mondo");
</script>
JAVA2;
or in your case using jQuery
echo <<<JAVA2
<script>
$('#statoPag h3').text("Errore!");
</script>
JAVA2;
Very likely the call will need to be inside a jQuery onDocumentReady function to be sure that it executes.
A better and faster way (and as #arkascha observed, nicer and more robust): you can generate the header from listpagPrinc.php or from a wrapper.
// file listPagPrincWrapper.php, replace your current file
// Ideally listPagPrinc could return a text value. In case it is
// printing it, as seems likely, we capture the output. This way
// we don't neet to modify the existing code.
ob_start();
include('procedure/listPagPrinc.php');
$lpp = ob_get_clean();
// At the end, we do the output part.
print <<<HTML
<div id="statoPag">
<h3> Stato : {$error}</h3>
</div>
<div class="headerCont">{$lpp}</div>
HTML;
Related
<section>
<?php echo(file_get_contents('http://server_address'));?>
</section><script>
<!--//--><![CDATA[// ><!--
$("ul").html('http://server_address');
//--><!]]>
</script>
What may cause that first server gets me the content of the link, the second displayed only text "http://server_address". Both servers are Apache2.
In the first case you use file_get_contents in PHP, that is actually doing what you need. The second use you just output the text. So your program is doing exactly what you wrote.
If I understood correct what you need is to output content from the link into ul element. This can be done l
<section>
<?php echo(file_get_contents('http://server_address'));?>
<!-- save content to variable, escaping quote chars -->
<?php $content = addslashes(file_get_contents('http://server_address')); ?>
</section><script>
<!--//--><![CDATA[// ><!--
$("ul").html('<?php echo $content; ?>');
//--><!]]>
</script>
Hello I am a noob in php. But I have something like this:
<div id="signupbox">
<!--A lot of stuff-->
</div>
so I have a php script just above this is check if a $get variable is activation_success, so I would echo a javascript to find #signupbox and change innerHTML, but I also want to include a file called login.php inside #signupbox.
The question is if I changed the div's innerHTML using javascript, the included .php file will also disappear.
In what way can I change the innerHTML and include a .php file inside?
I wanted to include login.php file because it is like a snippet so I can include it somewhere else and if I wanted to change, I can change the included file.
What I kinda what to do is echo('<script>$("#signupbox").html("Thank you!");</script>' . include("login.php"));
Use the load() jQuery method :
html = $('#signupbox').html(); // save the previous html
$('#signupbox').load('login.php', function(){
$(this).prepend(html); // add the previous html in the beginning
});
So, if I'm understanding correctly, you want to change the innerHTML of teh #box div but leave teh code generated by your include?
Why don't you make another, smaller div inside of #box and put only the stuff you want to disappear inside of it, then change just the inner div...
<div id="box">
<div id="innerBox">
<!--all the stuff that was in the #box, except for the include -->
</div>
<?php include "myinclude.php"; ?>
</div>
include.php is not disappearing, php is server side language..
you code is erasing existing content and rewriting.
'.html()' will erase all contents inside the target eleiment, include.php contents will be erased while executing .html() function in clientside
while adding more content in javascript,
$('#signbox').append('Thank you!');
instead of
$('#signbox').html('Thank you!');
hope this will help.
I'm not very experienced at php, so if there's an easy function for this, I'll feel like an idiot.
When coding in PHP, at the point where you need to echo some HTML code, I have found I have either one of two options.
A: echo "<!--html text here-->";
B: echo "\t\t\t<!--html text here-->\n";
If I were to use method A throughout the code, looking at the php code from client side using view-source produces a solid block of code which is difficult to read.
If I were to use method B, it looks fine client-side, but the actual source code looks messy.
Is there anyway to keep both server and client-side appearance clean?
Something like this
<?php
//php code here
?>
//html code
<h3> <?= $justADynamicVariable ?> </h3>
<?php
//continue php code
?>
Un-readable front end code is caused by poor factoring of your PHP code
The wall-of-text issue occurs because your code is messy. You have logic and display code in the same place (evident from the fact that you're echoing HTML from a PHP block) and this will always produce unreadable and ugly code.
Look at Model-View-Controller pattern, and separate out your logic code from your display code. Then, write your display code in a primarily HTML format with some in-line PHP:
<div>
Welcome back <?= $this->username; ?>
</div>
If you're having to echo HTML code from a PHP block, your code is probably factored wrong.
Other useful tricks to produce readable code:
Use alternative PHP control blocks
This:
<div id='somediv'>
<?php if($something): ?>
Some stuff
<?php endif; ?>
</div>
is infinitely more readable than this:
<div id='somediv'>
<?php if($something) { ?>
Some stuff
<?php } ?>
</div>
And it's definitely better than this which is probably what you're using now:
<?php
echo "<div id='somediv'>";
if($something) {
echo "Some stuff";
}
echo "</div>";
?>
You can just close the php tag (?>), write the html block and return to php (
Use a templating engine, like Twig http://twig.sensiolabs.org/ it allows you to separate your logic out of your templates so you can write really easy to markup with simple syntax to drop in dynamic variables.
or
Use a browser like Chrome, which will auto indent your source for you when you use the Web Developer Tools http://discover-devtools.codeschool.com/. You can even copy paste the results really quickly to a new file. In general, it's a bad idea to fret over outputting well spaced html since software can clean it up so easily.
There are lot's of ways to produce html, depending or it's a single line or an entire block of HTML.
The important thing is readability.
echo "<div><p>Hi ".$name."</p></div>";
Isn't unreadable persé, it might be annoying because it won't get highlighted in many IDE's.
This is a good reason to always seperate html and php, meaning always put html parts outside of your php tags.
So you could end your php block before and open it again after :
if(TRUE) {
?>
<div><p>Hi <?php echo $name;?></p></div>
<?php
}
If you have PHP within a large file mostly containing html you are better of using php control blocks:
<!--html text here-->
<?php if(TRUE):?>
<div>
<p>Hello <?php echo $name;?></p>
</div>
</php endif;?>
<!--html text here-->
You could also use heredoc
echo <<<EOD
<div>
<p>This is html</p>
</div>
EOD;
Or output buffering:
ob_start();
?>
<div>
<p>Hello <?php echo $name;?></p>
</div>
<?php
$html = ob_get_clean();
echo $html;
I'm using PHP to make my page more dynamic through query passing however I have a big chunk of HTML code that needs to have dynamic content inside but I don't know how to go about doing that without printing every statement:
One part in HTML:
<div class="review">
<p>
<img src="http://www.cs.aub.edu.lb/hsafa/cmps278/hw2/rotten.gif" alt="Rotten" />
<q>Expect no intelligence or subtlety here, but if you're willing to put up with the sheer ridiculousness of it all, you might enjoy wallowing in Bekmambetov's shameless exhibition of narrative lunacy and technical fireworks in this movie.</q>
</p>
</div>
<div class="personal">
<p>
<img src="http://www.cs.aub.edu.lb/hsafa/cmps278/hw2/critic.gif" alt="Critic" />
Frank Swietek <br />
<span class="italic">One Guy's Opinion</span>
</p>
</div>
The above code is for a single review however there could be any number of reviews which I am already taking a count of but I am also changing the image, quotes and text for all the reviews.
Is there a way of including all the tags without printing them all?
The regular way is to "close the php tag" and then reopen it after the HTML:
// php code here
?><html code goes here><?php
// php code here
However, there are a couple other ways. One is to use include.
// php code here
include('template.file.php');
// php code here
and inside your html code, you use have something like this:
<htmltag><?= $php_value ?></htmltag>
You can even use include within a function.
Alternatively, you can use a template system like Handlebars, Mustache, or Twig. Or you can just continue to build large strings, which is what I do. I set up templates, merge them with data to produce strings, and then emit the strings. The main thing I gain from using the templating system is that I can save up the strings to emit them at the end, and thus, have the ability to alter the HTTP header before my output is sent. If I used include() or code blocks, the code is emitted immediately, and I cannot modify HTTP header values.
Additionally, by building up the strings, I can save them to files and use these precalculated chunks to improve the site's speed.
<?php
//PHP code section
$wolrd = 'world';
?>
We are back in HTML
Hello <?= $world /* and some PHP echoing with a 'short tag' on same line with HTML*/ ?>
Suggested reading:
http://www.smarty.net/ (I'm not a fan but you should be aware of it)
MVC - Model - View - Controller
You should only be using PHP to print your dynamic content
<img src="<?php echo $image_url; ?>" alt="<?php echo $image_alt; ?>" />
You can close PHP and start it again at any point
<?php
$string = "hi";
?>
<p>Lot's of HTML</p>
<?php
echo $string;
?>
Well most is in the title. I wonder if it's supposed to be that way or i can do the same without an if(1) condition I'm doing this because my website pages are all as php includes.
Thank you all
Answer retained:
Okay basically the way to do it is simply to include('file.php') as it will be considered out of the current <?php ?> environment.
Putting
<?php if(1): ?>
...
<?php endif; ?>
around your HTML code in a PHP file will have no effect on the result. You will still be able to include the file without it.
You can think of it like the "default mode" for a PHP file is that it contains HTML content. You only need to add <?php ?> tags if you want to add PHP code. If you're just putting HTML code in a PHP file, they're unnecessary.
The beauty of PHP is that you can move "in" and "out" of PHP very easily. You can do the following without issues:
<?PHP
if(whatever) {
?>
your HTML
<?php
include('whatever.php');
?>
more HTML
<?PHP
}
?>
To build on Zak's answer:
You can also use PHP to echo out things that aren't PHP... as long as you quote it appropriately.
<?php
//HTML
while ($x < 5) {
echo "<p> this is html that you can wrap with html tags! </p>";
$x++;
}
//Javascript
echo "<script type='text/javascript'>
some javascript code
</script>"
?>
Although, it's less confusing to just end the php tag to keep things separate.
And you can even use php as you want within html or javascript as long as you put the tags, and as long as the file is saved as a .php file (so PHP can be processed on the server).
Ex:
<script type="text/javascript">
//set a javascript image array to a php value
var imgArray = [<?php echo implode(',', getImages()) ?>];
</script>
But if you want to do this the other way around (IE, assign a browser-compiled value, such as a javascript value to a php value), you'll need to use AJAX.