Executing PHP code from HTML file loaded by PHP - php

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.

Related

Reload parent page from PHP page loaded in DIV tag

Hi I have a PHP page loading in a DIV tag from a javascript function. I also have a button on this PHP page that sends information to another PHP page. I was using header to go back to the page I was on (still in the div tag) but I was wondering if there is a way to reload the original parent page. The pages are all saved in separate files so I was unsure how to do this. I have tried using Javascript such as top, window, and opener for location.reload() e.g.
top.location.reload()
window.location.reload()
and saving the function on each page as
function myFunctio(){
location.reload();
} and then calling it on each page again using top, window, and opener but nothing has worked. If I use a header it will only load the page in the DIV.
You can use them in sequence to get the result you're looking for give something like window.opener.location.reload(false); a try

How can I refresh embedded PHP code from the client side?

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/

AJAX and file_get_contents

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>

Why would a.php file open as if it were an html file, rather than execute?

I have a .php file that I'm trying to execute. It's referred to in the 'action' part of an html form.
For some reason when the form submits it opens the .php file in the browser as if it were an html page (a blank one).
The .php file doesn't have anything out of the ordinary in it, but I'm not sure it's getting to the point of executing it anyway.
My opening form tag looks like this: <form action="my_script.php" method="post">
What am I missing?...
In all likelihood your script is executing. By default, HTTP headers will be sent indicating that the script's content is HTML, hence that's how your browser will treat it. But if you don't actually send any output, it'll appear as a blank page.
If you want the form to do something but not open a new page, maybe you could use AJAX to submit the form data without leaving the page. Alternatively, you could just add at the end of the script
echo 'Finished :)';
so that you know it has gotten to the end, and presumably done something.
When you submit a form, your browser posts data to the URL specified by the action attribute, as part of the request for that page.
Your page at my_script.php should output some HTML.
If instead you see your PHP code when you view the source of this new page, then PHP is not configured to be parsed on the server.
euh... php
Use: http://www.apachefriends.org/en/index.html instead of opening a html file directly in your browser.

Variable from JavaScript to PHP

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

Categories