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"]);
Related
I have a table with a button which onClick calls an external javascript file, which calculates some values and returns them to the table. I need to then write that value back to a file using PHP without leaving the current page. Is there any way to do this? I thought about trying onChange, but I wasn't sure how ot make that work with php since it's usually a javascript call.
PHP is executed on your server
the javascript file, even if external, is run on the client machine.
You can't use PHP on the client machine! You must use javascript to call a php script, stored in your server, that do what you need to.
You're on the good track, you need to use AJAX calls in your javascript.
don't use onclick, search for unobtrusive javascript;
you'd be using jQuery, so you can use its ajax handler;
find out a good reading about the whole topic, before going heads down on writing code.
This sounds like something you could solve by using AJAX. Basically you would communicate with your server in the background, send the data to it so it can process it. You send a separate request in the background which happens without reloading the page.
Please be aware of the distinction between the client (where the JavaScript is run) and the server (where PHP generated the page run by the client).
If you want to use jQuery, that library has some decent functions that makes it easier to implement cross-browser AJAX requests. However I would suggest first researching the topic.
I am a real newbie at PHP and have, I hope, a simple question.
I have found a variable with Firebug in the DOM section and would like to access it to determine if it is true or false. The variable is called isResponsive and it is found under the DOM section of Firebug. I would post a screen capture but the forum won't let me.
In firebug it says:
window > responsiveDesign and then below it lists all of the variables. One of them is call isResponsive and in the column to the right it says either true or false.
Any help in how to access this variable is very much appreciated!!
Thanks in advance,
Doug
You can't, at least not directly.
PHP runs on the server and generates some (mostly) text which it sends to the browser.
The browser parses that text as HTML, JavaScript, CSS, etc and constructs a DOM from it.
The DOM variables exist in the browser, not on the server where PHP runs.
For PHP to get access to them, you would have to use JavaScript (or something else that runs client side and can access the DOM) to serialise the data to strings, and then send them to the server in an HTTP request.
You can't Access DOM vars in php - they are only available over JavaScript. If you want this value the only way is to pass the var via JavaScript to a php script
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.
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.
I am trying to found out how to see if a php file has changed and then show a div with saying Page changed in JQUERY
You'd better do that in PHP using filemtime, no need for JQuery here.
You only need jQuery for this task if you're trying to detect the page change without waiting for the user to request a new page. If not, do as the other responder suggests and use PHP.
But if you need to do it without a page reload, use one of the $.ajax() methods in jQuery in combination with a JavaScript timer. You'll have to poll the server periodically (thus the timer) to ask if the page has been altered.
You would also need to set up something on the server that can tell your page about changes. Perhaps a very simple service that provides the timestamp of the last edit in JSON format. Use $.ajax() to poll for the timestamp, then compare it with the last edit the page knows about. If the timestamp from JSON is more recent, display your div.
Javascript cannot access the server, you will have to use some sort of server side technology. Like PHP that was suggested by Pekka.
In short, javascript is client side, which means it interacts with the user on their side, while php is server side, meaning it interacts with the server. Checking the file modified date is a server side issue, your client isn't serving the pages (unless you're on freenet)
Or you could output a <meta> tag for when the page was updated with PHP or whatever framework or language you are using. Then create a cookie with your JS and compare the cookie with the meta tags content.
Ugly solution but it would work. I wouldn't want to resort that this however.