I have a script which uses file_get_contents and I then decode the returned JSON...
$urlone = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=false&screen_name=".$screen_name."&count=".$tweet_count."&page=1"),true);
What I would like to achieve is a callback so I can have a nice loader and then animate contents in when complete. Is there a way to use JQuery to do an AJAX call if Javascript is enable and fallback to the default PHP if it is disabled?
PHP doesn't know if the user has JavaScript enabled when it handles the request from the user, at least not on the first request.
Normally you would assume the user has no javascript and enhance from there, but as you want to 'fallback' to PHP, you would need to render the JSON anyway regardless of whether the user had JavaScript functionality - so using JavaScript would now be a tad pointless as you (and the user) already have the data.
what you can do is:
Confirm js is enabled in a previous request. If enabled link to php file that depends on javascript enabled. if not, link to php file that directly shows output. You could do this with a combination of Meta redirect and Javascript.
Or call the php file with direct output in an iframe that is inside a <noscript> tag, belonging to a tag that does the jquery.
in the first option you would have file.php link to either fileWithJs.php or fileWithoutJs.php
in the second option you would have something like this:
<script>
// jquery here
</script>
<noscript><iframe src="data.php"></iframe></noscript>
Related
Basically, at the click of a button I need to refresh a div that contains embedded PHP. I tried using .html() in jQuery to reload the contents of the div, but it didn't change anything so I'm assuming it's not recalling the PHP (because the PHP should be changed at this point). It's probably just rewriting the HTML that was outputted by the PHP when the page loaded. I also tried appending something to the new HTML load so I could see it was at least refreshing the HTML code, and it was. It looked something like (if "updateObject" is a variable that contains the location of the div):
updateObject.html(updateObject.html() + " random text");
I also fiddled around with the .load() jQuery method, but it seemed to be for external PHP files (and at this point I can't change my embedded PHP to external PHP).
Any thoughts? Thanks!
EDIT: The biggest problem I've had is that, with my limited knowledge of web dev, I need to have the PHP embedded. If I make it a separate file, I'd have a very difficult time finding it because I honestly don't understand how the files get put together through the framework we're using (Drupal). I did try using an external PHP file but I couldn't figure out how to find it. Even if I put it in the same directory as this HTML file, it doesn't seem to be easy to find.
You are to have 2 files:
PHP
This contains the data you want to show.
HTML
This is the one showing the php content.
In PHP you can put whatever you want. But the HTML once is loaded is loaded and the only way to have some more content into it is to make what is called an asynchronous call, which means a call after the page has been loaded.
To do this you are to use jQuery and call the $.post (or $.ajax), using this syntax:
$.post('filename.php', function(dataFromPhp){
//actions to do once the php has been read
})
So in your case you can make a function that reads the php every time the click is done on a div/button/other DOM object; like so:
function updateDivContent(whatDiv){
$.post('filename.php', function(dataFromPhp){
if(dataFromPhp){
$(whatDiv).html(dataFromPhp)
}
})
}
So if you want the div to be refreshed (and reload the php) you are to connect that function to an event and specify the div you want to show the data:
$('#myBtn').click(function(){
updateDivContent('#myDiv');
})
Refer to this documentation for further informations: https://api.jquery.com/jQuery.post/
I created a little DIV in HTML which gets filled by a main_login.php
main_login.php loads the homepage with member content on the side if there is a session started
If there is no session started, the main_login.php file loads the homepage with loginfields.html on the side
main_login.php: http://pastebin.com/vkhccGSB
loginfields.html: http://pastebin.com/fDJjTjsf
Now whenever loginfields.html is loaded, the button on that page doesnt execute whenever I press it. When I open the localhost/loginfields.html it works like a charm.
What is preventing the login_check.php to load?
I'm going to take a wild guess here, and say that you're loading in main_login.php via AJAX and using innerHTML (or jQuery's html()) to put it into the page. I'm also guessing that your button is powered by a <script> tag.
<script> tags loaded with innerHTML are not executed. You have to either load it in as an external script (even then I'm not sure if innerHTML will load it in), or use proper DOM functions to append the script to the page, or separate the HTML and JavaScript from the response and run the JS through eval.
If you're simply trying to load loginfields.php into the page, you could just use include('loginfields.php') instead of your fopen() function.
I'd like to pass some data from PHP to JavaScript without JSON.
The reason is because I don't want the data been readable by anyone if clicks on view page source.
So, I have a PHP like
print(<script type="text/javascript">a = "aaa";</script>);
In my HTML code this will be
<script type="text/javascript">a = "aaa";</script>
I can remove this in the client side, after loading the variable. By for example with jquery
$('script[type="text/javascript"]').remove();
And after the DOM will not have anymore the script tag, but the variable a.
Later if I type to the console window.a will be aaa.
But i do not want to show the <script type="text/javascript">a = "aaa";</script> in my HTML source code. Is this possible, to pass the PHP variable directly to the DOM?
Thanks for the help.
JavaScript is a client-side language. Whatever you pass to it (by whatever means) will be readable by the end user.
Removing the Script DOM won't help, as "view source" shows the HTML code as it was during download. If that is what you are concerned about, you can fetch the variable via an AJAX once the DOM has been loaded.
(But it still is readable by anyone who can read JavaScript (an re-run the AJAX call), use Firebug or Wireshark. It really only helps against a simple "view source".)
Is there a way to check whether Javascript is enabled or supported by the browser? If it's not supported, I would like to redirect the user to a user-friendly error page.
I am using jQuery and the PHP Zend Framework.
<noscript><meta http-equiv="refresh" content="1;url=error.html"></noscript>
This will redirect to an error page if script is disabled. Just replace error.html with the URL of your error page.
As yet another option, you can (though it requires a second page visit) use javascript to set a cookie.
If the cookie exists server-side (they have javascript) render the page as normal. During the absense of the cookie, you can either use a Location redirect, or render the appropriate [stripped-down] template to accommodate their lack of javascript support.
page html
<script type="text/javascript">
document.cookie = 'hasJS=true';
</script>
page php
if (isset($_COOKIE['hasJS'])){
// normal page render
}else{
header('Location: http://mysite.com/index-nojs.php');
}
Whether or not javascript is enabled is only known from within the context of a browser, so the best you can do is write some browser-side code to set a flag based on if javascript is enabled or not. One possibility is do something like
<noscript><img src="/javascript_disabled.php"></noscript>
and
// contents of javascript_disabled.php
$_SESSION['javascript_disabled'] = 1;
As the default, send out the version without javascript. There you include a little piece of javascript that redirects to the dynamic version. This will only get executed when js is enabled.
You can make a simple "landing page" for users without javascript AND add a javascript redirection to the javascript-enabled version of site.
Something like this:
...
<html>
...
<script type="text/javascript">
window.location.replace("/hasjs");
</script>
Piece of cake!
Encompass your entire Javascript page in one DIV.
Overlay a second DIV of equal size that contains your non-Javascript page. Give that DIV an id and a high z-index:
div id="hidejs" style="z-index: 200"
In this second non-JS DIV, have your very first code be Javascript that sets this DIV's visibility to hidden:
document.getElementById.("hidejs").style.visibility = "hidden";
No Javascript enabled? Nothing will happen and they'll see the overlying non-Javascript page.
Javascript enabled? The overlying non-Javascript page will be made invisible and they'll see the underlying Javascript page.
This also lets you keep the whole page in one file, making modifications easier, rather than trying to manage two files for the same page.
Use <noscript> tags to include a meta-redirect if the page does not have JavaScript.
<noscript> tags are called when the browser connecting to the page does not have JavaScript.
Bring the user to the error page by default, and have a javascript redirect execute in the section of the page. Have the redirect push the user to the real page.
If they don't have javascript enabled, they won't get redirected and the rest of the page (error page) will load.
Yes. Make you have the latest jQuery.
Javascript:
$(function(){ $('#jsEnabled2').html('Yes it is') })
PHP:
$js - 'No';
$jscheck = 'Javascript Enabled: ';
$jscheck .= '<span id="jsEnabled">'.$js.'</span>';
print $jscheck;
Here is the code:
$('#sousmenu a').click (function (){
startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
return false;
});
The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the ?folder=portraits works.
Here is the page.
**** Simpler *****
Maybe I am not clear, it happens sometimes!
I want the link href to be send to this PHP function,
<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?>
so clicking on the link animaux will send animaux to the glob() PHP function and will get all the .jpg files in the photos-animaux folder.
Clicking on the portraits will send the photo-portraits, etc.
If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.
If your site is on http://example.com and you wish a myparam with value test to be passed to PHP you should add something like this in your JS:
document.location = 'http://example.com?myparam=test';
This will reload your page adding a new param which can be accessed in PHP by simply using $_GET['myparam'] variable.
You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.
Look at the source in your browser.
Php is server-side, and that means you need to use ajax or reload whole page to get a response.
There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax