Refresh Div Containing PHP Function - php

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

Related

Using PHP to call jQuery on form submission to $_SERVER['PHP_SELF']

I have a single page of HTML that includes PHP code.
I am using the PHP to send data from a form to the same page using $_SERVER['PHP_SELF'] as the form action.
There is an isset($_POST... condition in the PHP that detects if the page has loaded with data via POST. If this is true, the PHP compares the value sent to a set maximum value.
If the data is less than the maximum value, a table is displayed.
If the data exceeds the maximum value, I would like to display an error message using jQuery: $('.error_box').append("Error");
The error message is not displaying. I think this is because the PHP is trying to make changes to .error_box before it has loaded.
How can I make sure that the error display function is available, given that I am validating the form data via PHP as soon as the page loads?
You don't have to use javascript or jQuery to add content to '.error_box'
You should add it with PHP directly.
EDIT:
You must know that you can't directly execute javascript via PHP.
Here is how your page is created then rendered (this is simpler than reality) :
Your server receive the request to display a page
The PHP code is executed and output an HTML (+CSS +JS) page
The output is send back to the client who ask for page
The client browser parse the HTML and render it using css rules you specified
The javascript is executed
Wrap it in a document ready function, like so:
$(document).ready(function() {
$('.error_box').append("Error");
});
Edit:
AMDG is probably right also...

Refresh content of a div without ajax

I have a div with id=mydiv and inside of this div I have some content that I am grab it dynamically from database with a while.
Is there any way to use a button to refresh or reload the content of my div without using .load() ajax method?
$("#mydiv").load("index.html")
example:
<div id="mydiv">
<?php
$trtyb = mysql_query ("SELECT * FROM xxx WHERE xxx= '$getcat'");
while($fina = mysql_fetch_assoc($trtyb)) {
echo $fina['name'];
echo "<br>";
}
?>
</div>
You need to get the fresh content. The only way you can do it is with an http request. The only ways to make the http request are:
1) reloading the entire page.
2) using ajax - If you don't want a delay and that is why you don't want to use ajax, you could set up a javascript timer that would make periodic ajax calls and continuously update the data into a variable. Then when the user clicks the button to refresh the data, you can just pull the data from the stored variable.
3) put all of the content in an iframe, and that would need to be refreshed (I see no benefit to using this).
Yes you can load any div without refresh any page and without calling ajax
here is working example
// your code $("#mydiv").load("index.html")
Working code is here:
// $("#mydiv").load("'your url here + #your div name'")
$("#mydiv").load(url + "#mydiv")
Don't forget to add space in the div if you will not make space in div the program will not work anymore.

Save HTML from one page to another page onclick

I know I could use a php include to read the html from a file on the server but how do I write a file to the server once the user clicks to navigate to the next page?
I have a div that is changed by jquery on the 1rst page. I want to read the changed div when the user clicks to go to the 2nd page and write the html from the 1rst page to the 2nd page.
You can use combination of javascript and php code.
jquery
get the value of changed div and place it into a hidden field, wrapped within a form tag and submit the form to the next page
php
and on next page, get your hidden field value from $_post array and display it.
So you have a div that is changed via jQuery:
<div id="something">something here</div>
To access the HTML inside your div,
var myHTML = $('#something').html();
Then Use AJAX to send the value to the second page:
$.ajax({
url: 'secondpage.php',
data: {
'key' : myHTML
}
type: 'post'
});
In secondpage.php, check for $_POST['key'] as follows
if( isset($_POST['key']) ) {
// myHTML was sent successfully
}
Not sure if it's a good idea to do this as users are clicking because it would take a while for the document to get updated, and what if multiple users were trying to access the same page? You can use something called a cronjob, which basically executes PHP from your site's server at a specified time interval. I did this to update my website with my Twitter feed every 10 minutes, but doing it on every click would be too slow. What exactly are you trying to do?
When the user clicks on "go to next page" link, send AJAX request to
some PHP file.
The AJAX request should contain the div html (use
jQuery: $(this).html())
In the php file write the html, and return
information to AJAX (true/false).
When AJAX success go to the next page.

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.

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