How insert PHP output into html div replacing temp graphic - php

I need to display an html page and within the page is a div into which I want to echo the output of a PHP page.
While the PHP page is being fetched and prepared I want to display a "Loading" msg and a temporary gif.
I have the html code working
and I have the PHP code working/echo'ing.
How do I echo the contents of the PHP page to replace the original contents of the Div.?
(I guess - with an include:file and an innerhtml statement, but how/where?)
A Javascript solution would be fine.
Thanks

Add a div to your page with the image in it, then using jQuery:
$('#myDiv').load('mypage.php');

You're essentially looking to make an asynchoronous call to your php script once the page is loading, so JavaScript is your friend.
When the page loads, include the loading message and the temporary gif, then do an ajax call to your php script and insert the response into the relevant div.
As #Sjoerd points out - jQuery will do this for you nicely.

take a look into ajax:
The Low-down
Using ajax with jQuery

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

Refresh Div Containing PHP Function

So I have the following code:
<div id="currentmotto"><?php mottoGrab($name); ?></div>
And what this does is it uses curl to screen scrape a users motto and display it on the site. What I need it to do is for that function to refresh every few seconds to see if the user has updated the motto.
I know with jquery I can use the .load('phpfile.php') but the problem then is if I put that function in that file, it no longer gets the $name variable as that is from another page.
Any ideas?
Pass name to phpfile.php via the query string:
.load('phpfile.php?name=THENAME');
Then grab the name from within phpfile.php using $_GET['name'] and stick it in the function.
OR
Make an AJAX request passing the name and update the page using javascript.
Since PHP is executed server-side, it doesn't change once the client loads the page. The only way to refresh a div without JavaScript is to reload the page.
I would try using AJAX to get the name from the other file and update the HTML with JavaScript

Executing PHP code from HTML file loaded by 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.

How to append a div via PHP and AJAX Post

Good day. I'm trying to make an online quiz application using php. The idea is that the form will add a text field for the question and four other text fields below it for the answers each with a radio button to determine which one is correct. Then below the form is an add a new question button which would call an ajax post function. The problem is instead of appending to the target div, the php code replaces the contents of the div itself. Is there a way for php to append to the div instead of replace its contents?
PHP is server-side, it can't do anything in the browser, only deliver content.
To add a div, you use Javascript in the page, e.g.
var el = document.getElementById('idoftarget');
el.innerHTML += '<div>new stuff</div>
... I think. I always use jQuery:
$('#idoftarget').append('<div>new stuff</div>');
how are you handling the ajax response? maybe instead (since you don't have code here it's a guess) of
document.getElementById('someDiv').innerHTML=ajaxResponseText;
you want
document.getElementById('someDiv').innerHTML+=ajaxResponseText;
(appending rather than overwriting)
This is the right one:
document.getElementById('exampleDIV').innerHTML+=ajaxResponseText;

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