I have a series of PHP statements that I only want to run if javaScript is enabled.
if($js_enabled == 'yes') {
//Run a series of PHP statements
}
My problem is that I want to create that $js_enabled variable with javaScript. Therefore, if javaScript is enabled, the variable will be created and the PHP statements will be run, but if javaScript is not enabled, the variable will never be created and the PHP statements will not run.
How can I create a variable with JavaScript that PHP can recognize?
You can do
$browser = get_browser();
if ($browser['javascript'] == 1) {
// your code here
}
Please read the documentation for get_browser for caveats, especially
Note: In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system.
browscap.ini is not bundled with PHP, but you may find an up-to-date ยป php_browscap.ini file here.
While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.
Also checkout the link given in the comments to get_browser:
http://code.google.com/p/phpbrowscap/
EDIT As you correctly point out in the comments, the above will not tell you if JavaScript is enabled but just whether the browser is capable of running it. Finding out about the clientside from the serverside is impossible, because the serverside runs first. If anything, inform the serverside after a page has been served, e.g. like #mck89 suggested or simply set a cookie you can read on each subsequent request.
But generally speaking, if your site requires JavaScript enabled, you should simply add a message to inform the user about this requirement, e.g.:
<noscript>This page requires JavaScript to run properly</noscript>
In other words, don't check from PHP. Just set every variables the browser might use, if JavaScript is enabled and consider using graceful degradation or progressive enhancement.
Why don't you simply an ajax request? If javascript is disabled the ajax request can't be done so the PHP script will not be executed, if javascript is enabled the ajax request starts the execution of the PHP script.
You can store that value in a hidden field. and check it on server side.
If i would need it that badly I would use JavaScript to set a form variable (prefferably hidden) on page before and then check it when processing form.. Otherwise i would make my pages work without JS...
This can't be done. PHP is server side while javascript is interpreted only after the php interpretation has already been done and the code already sits in the user's browser. Try a workaround using ajax. if javascript_enabled -> call with ajax some php page.
Related
I looked for answers to this question, and it seems that most people have a specific problem. I'm looking for a more general answer. I have a text box where a user will type in their name and I would like to have PHP constantly monitor the text box and change the value of the $name as it is typed or edited.
I would also like to do the same with buttons, and as different buttons are clicked, the content of a variable would change to match that which the button represents. Basically, is there a way to get PHP to constantly run on the page and gather information from a user as it is changed?
It seems like it should be possible, but my experience with PHP is limited, and I'm not sure how to begin, so I don't have any code to really show.
This sounds to me like you require an ajax script checking for input changes(eg/ keystroke, on_blur or on_click for your buttons) and sending back to a php script that will update your variables/tables and return the new variables to the ajax script once they are updated.
1 - Ajax checking for changes on the page, and firing off to a php script on server.
2 - Have a method in your js that waits for the action to be completed and load the new variables into the HTML document.
Basically look up Ajax/PHP - Check username availability, Then adapt to your specific needs.
:)
Simple ajax script will be what your after, there are many scripts available for checking username availability --- As for a lone PHP script, this will not be possible as the PHP code has already ran on the server before the html document is rendered to the browser.
My first answer so my wording may not be perfect comment back if i have confused you more.
It seems like it should be possible, but my experience with PHP is limited, and I'm not sure how to begin, so I don't have any code to really show.
Yes, it's possible, but most likely only due to the nature that the browser (e.g. Firefox) and PHP itself are Free Software.
A proof of concept is missing so far, so you really need to start at a very basic level.
You can download these software packages and modify them to your needs, e.g. make the browser interactively corresponding to the DOM and DOM events process PHP scripts that you embed like script tags inside HTML.
But well, as you wrote, you're starting, so I guess, you don't want to start with rewriting the browser and the PHP interpreter, so even if possible, it's perhaps best to stick that interactive part inside the browser to Javascript and some HTTP request / response programming on the server with PHP.
I would like to detect whether java is enabled in the user's browser when they use my website(php).
If java is enabled a certain div loads on the website if not another div loads.
I do the following:
in the PHP header file: (so whatever loads it checks whether java is enabled)
<?php $no_java=0;?>
<noscript><input type="image" src="whatever.jpg" id="java_checker" value="1"></noscript>
This a 1px by 1px transparent image which can't be seen but it's better than a text box which can be seen.
if(java_checker==1){
$no_java=1;
}else{
$no_java=1
}
I've been programming in PHP for a few months now. I know that the if(java_checker==1) is not right but I don't know how to check the value without submitting a form etc...
Anyone could tell me how to do it? the value="<?php $no_java=1;?>" is not good because the php part seems to load regardless to the <noscript> tags :(
Any idea? Or any other idea how I could "tell" php that there is no java so do whatever...
PHP is a server-side language whereas Java applets run inside the browser.
That means PHP can't directly detect whether the useragent has a Java plugin installed or not. In theory, it could, if there was a relevant HTTP request header but there isn't.
At best, PHP can emit JavaScript which then runs some tests inside the browser and reports back to server via AJAX.
See How can I detect the Java runtime installed on a client from an ASP .NET website? for details.
You are taking it wrong way.
It's not in PHP you have to detect if Javascript enabled, but in JS itself.
Just make your PHP always enabled, but if JS is present too, let it take some job. Easy
First, I think you are confused between Java and JavaScript. Lack of JavaScript is dealt with the <noscript> tag, but I guess this approach will work fine for "Java applets" as well.
The key idea here, is that the only information PHP has about the browser is the "User Agent" string. This specifies which browser this is. The process of making a website work well with or without Javascript is called "Graceful Degradation". You can read about it elsewhere.
There is no way for the PHP to tell whether JavaScript is enabled or not. The least complicated workaround is to create a test page, which looks like this:
<html>
<script type="text/javascript">
window.location.href = "myapp.php?js=enabled";
</script>
<noscript>Click here to continue</noscript>
this would allow you to say $javascript_enabled = $_GET['js'] === 'enabled'; and determine whether JS is enabled or not.
Note that this is a very wrong method of building applications. You need to account for both cases.
How to get a value from a JavaScript variable to a PHP variable without AJAX, Jscript, HTML hidden field, or a cookie ?
(from PHP to JavaScript: var javaScriptVar = "<?php echo $someVar; ?>"; )
So is there nothing like that?
thanks
You can do it like in your example, but your javascript must either be inside a script tag which is in a PHP file, or in an external 'js' file that you save with a php extension.
Or, you can set your server up so that all files with a 'js' extension also get parsed by PHP, but that's probably more than overkill.
However, since the PHP will only be executed once before the file is returned to the client, there's no way to make that dynamic. If your objective is to do something simple with data that resides on the server (and more than likely will not change between page accesses) it won't be too horrible to accomplish what you want to do that way.
There is no way to get a Javascript value from PHP, because the response line goes from server to browser, never the other way around. AJAX is your best bet in this case.
There is nothing like that. The data needs to be transfered to the server, either by a form variable or a url variable. Either with an AJAX request or a normal request. I never thought about doing it with a cookie, but I guess that is an option too.
No.
PHP is a server sided language, javascript is a client sided language.
In order for PHP to receive information, you must talk to the server. The only way to do so is to submit a form, or use AJAX.
Short answer is you can't. There are two main ways to send content to the server from the client, which are cookies and submitted data (either posted or through the querystring). If you were so inclined you could add it to a request header but that is plain obscure and would require an AJAX request.
how we can disable save as option of browser by php?
Impossible. The user already has your page. It was transferred to their computer before it could be displayed on their screen. At this point no code you write can stop them from keeping what they already have.
You can not do that.
PHP is processed server-side. Your browser is client-side. Thus, you can not achieve this.
its not possible becaouse browser is on client side.
PHP stands for PHP: Hypertext Pre-processer. It can only process your PHP script. Thus, when you run your script, it just generates HTML output(in most cases), and sends that to the users browser. When you have already done the handling on the server side, and it has been sent to the browser, PHP can't change it. This means that this is not possible via PHP. You might be able to disable Control+S via Javascript, though.
It's possible to disable the "save" with PHP if you access the COM elements of the browser and control them before outputting. Google "accessing COM objects with PHP"
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.