I heard people said php server site script cannot communicated with html and javascript client-side script. But when I test it, seem php can tell html and javascript what to do. Here is my codes :
<?php if ((isset($username1))&&($username1 == $username2)){ ?>
<div style="position:relative;">
<img src="http://plekz.com/images/layouts/theme.png" onClick="showThemeDiv(); hideThemeTip();" style="margin-bottom:3px; margin-left:1px; position:relative;" onMouseOver="showThemeTip();" onMouseOut="hideThemeTip();" />
</div>
<?php } ?>
I tested it on IE, Firefox and Chrome, all works perfectly. But I still worry code this way will lead to problem when I move all the file to other online webhosting/server... Do I need to put all html and javascript codes into php echo? Or I can just code like this without having any problem in future? Is it standard way to code like this?
What you are doing is just PHP, in your code there is no actual communication between "html and javascript", which is on the client's side. And PHP, which is on the server side.
What people mean with communication is that the user can change something on the webpage dynamically without reloading the webpage. Such a thing can be done using AJAX.
In short, the code you are using will work on any webserver and with any browser.
It's a bad way when html and php code are in one file and much better when separate. But you can code like this and should not put all html to php function 'echo'. It's the same instead you don't need to trigger the php function. And more much understandable to read a code, IMHO.
PHP is simply a scripting language which gets interpreted at runtime whenever a request is made; people say it cannot communicate with HTML/Javascript because it is interpreted on the server side, it is not visible/accessible to the user agent once the page is rendered.
It is however possible to communicate with PHP scripts through AJAX calls.
Related
<?php
include 'components/server.php';
Is it possible to make it include server.php for desktops and server-mobile.php for mobile devices?
While technically possible, it's absolutely not the best way of doing things.
Why?
Because PHP runs on the server and only the output of that PHP execution is given to the browser. You would probably be wanting something using javascript which can load and then seamlessly react to the browser conditions, such as screen size and/or dimensions.
If you're trying to change which PHP script is running based on the browser criteria (as mentioned above) this sounds very much like your programming logistics are simply wrong.
If you somehow really do need to change PHP script execution based on end-client (browser) characteristics you could do this by calling a script based on javascript AJAX or using mechanisms mentioned in comments above, but as said, you're almost certainly "doing it wrong".
Alternative
It would be far better to load everything you need in PHP and then pass all of that content to the browser (as output; HTML, CSS, Javascript, etc.) for the Javascript in the browser to then decide which parts of the data it needs to use and ignoring the others.
After realising that my web server wouldn't run php inside my html file I used an iframe which points to my php script.
It works as expected and now my site has a nice little comment form that the user can fill in and submit.
I opted for this instead of changing my hhtpd.conf because I don't think my web host allows it.
So my question is; is there any real danger of doing this? If the comment.php file were to mysteriously disappear an error would appear in my html which wouldn't affect the rest of my code. I can't think of any drawbacks unless there some server overhead I'm unaware of.
Any information would be welcomed.
Cheers!
If they (the html and php files) are located on the same server — should be no danger.
Just to clarify :
If you can 'run' the php in an iFrame, then you're able to run it in the main frame as well. the php that is generated for your iframe could as well be generated for the main frame.
So, no, there is no danger at at, but no, you don't need an iframe, I think you misunderstood somehow how php is working.
There is no php in html, php is (simplified) used in 2 scenarios :
first is to generate html that will be sent to the web browser,
the second is a script, that doesn't render any php but affects some internal files, like databases and such.
I have a small script that pulls HTML from another site using Javascript.
I want to include that static HTML that gets pulled in a PHP page without any of the Javascript code appearing in the final PHP page that gets displayed.
I tried doing an include of the file with the Javascript code in the PHP page, but it just included the actual Javascript and not the results of the Javascript.
So how would I go about doing this?
You would need to fetch the page, execute the JavaScript in it, then extract the data you wanted from the generated DOM.
The usual approach to this is to use a web automation tool such as Selenium.
You simply can't.
You need to understand that PHP and Javascript operate on different places, PHP on the server and Javascript on the client.
Your only solution is to change the way all this is done and use "file_get_contents(url)" from PHP to get the same content your javascript used to get. This way, there is no javascript anymore and you can still pre-process your page with distant content.
You wouldn't be able to do this directly from within PHP, since you'd need to run Javascript code.
I'd suggest passing the URL (and any required actions such as click event, etc) to a headless browser such as Phantom or Zombie, and capturing the DOM from it once the JS engine has done it's work.
You could also use a real browser, but of course you don't need a UI in your case, and it might actually get in the way of what you're trying to do, so a headless browser might be better.
This sort of thing would normally be used for automated testing of a site (ie Functional Testing).
There is a PHP tool named Mink which can run these sorts of scripts from within a PHP program. It is aimed at writing test scripts, but I would imagine you could use it for your purposes.
Hope that helps.
ive got a script i need to load into a page but its sensitive so i dont want to have to reveal the source of the script. Im aware that js runs client side, but is there some way i could load it server side with php / or just embed it server side to still be run on the users system ?
The script itself is a simple external js script like this :
<script type="text/javascript" src="myscripts.js"></script>
cheers
If you want your browser to run a script, it must receive the source code for that script. Your statement "embed the script server side" doesn't make much sense, nor does "loading it server side with .php". Either the browser receives the script code to execute (and thus, it is visible to the end-user), or it doesn't.
You can try using various obfuscation methods to make the source code harder to understand, but anyone with sufficient determination will get around this.
You could conceal the source location of the script that way, but not the source code. If you want it to execute on the client then you must sent it to the client. If you send it to the client then the user can execute it. Don't put secret business logic on the client side, handle it server side.
No there is not. The best you can do is to obfuscate your code to make it hard to reverse-engineer for non experienced user.
I can't really find a topic on how to print a file with the printer.
First off, I dont't mean print like echo, but a real printer.
I want to have the user click on a button, and than it starts printing the page.
The same as javascript:window.print() does, but than with PHP. Is there such option?
Thanks!
Do you mean printing on server-side or client side?
For client-side you can include a javascript snippet which calls the JS function you mentioned:
<?php
print '<input type="button" value="Print this document" onclick="window.print();" />\n';
?>
For server-side: it is definitely more complicated. You could either find a PHP interface, e.g. to CUPS (the standard UNIX printing system) or call a system program that prints the document. Svish posted a link to a CUPS interface as far as I see an interface to printing in MS Windows (useful only if the server is running under Windows, of course).
Edit: If you expect to find a solution which prints the page PHP outputs rendered by the client browser and but not printed on the client, but on the server -- this should be impossible. I guess you could implement sending the output of PHP the a system program that renders the page itself but it will never look 100% like on the client. Maybe it's acceptable to call a printing program on the server, pass all necessary information to it and it renders some document of its own, e.g. an RTF text file?
PHP works server-side and therefore cannot print anything on the client-side. It can't do anything on the client-side actually, other than via generated HTML, CSS, JavaScript, etc. PHP can talk to a printer though, but it would be a server-side printer as far as I know.
SO, if you want the user to click a button to print a page, on their printer, it would have to be something client-side. For example JavaScript.
As PHP is a server-side scripting language, this wouldn't be possible.
You cannot have the user print a document from his printer using PHP.
If the design of the printed document is undesirable, you can either add a print stylesheet.
Or have a printer friendly page.
For example; passing a get variable called printer will have your php script echo another design of your page.
If you are talking about a server-side printer, you can.