Hi I have the following php code that I would like to be able to have it fire on page load. Any idea how this can be done.
action('clear_session')?>
thanks Maria
PHP runs on the server, but the PagLoad event occurs on the client. You can't directly call a PHP function from the PageLoad event. You might be able to do what you want while PHP is generating the page, or you could perhaps use an AJAX call to achieve the desired result.
Related
I am retrieving Google's weather API XML and using PHP. I'm retrieving the weather for any searched city.
Now, this "app" is under a tab and whenever I submit the form it refreshes and I want to prevent this.
Is it possible? This will be implemented in a dashboard - thats the reason I want to prevent the refresh.
This is what I mean: http://www.screenr.com/30As
The entire code is here: http://jsfiddle.net/ZshpF/
Simply, copy paste the code in a php file and it will work.
You'll want to use AJAX. Since you mention jQuery in the tags, it has a very handy function for making the call to the server. But making the call from the JavaScript is only half the story, you'll also need something on the server listening for that call. It would essentially be another PHP script which acts as a page in and of itself, but would return data in the form of (most likely) JSON instead of HTML. It's not meant to be human-readable, but rather to be a sort of web service for your JavaScript code to use.
You can find a simple example here.
I think you can use jQuery ajaxForm
I have a PHP-script that loads page-content from another website by using CURL and simple_html_dom PHP library. This works great. If I echo out the HTML returned I can see the div-content there.
However, if I try to select only that div with the simple_html_dom, the div always returned empty. At first I didn't know why. Now I know that it's because its content apparently is populated with javascript/ajax.
How would I get the content of the site and then be able to select the div-content AFTER the javascript has populated it with the correct content?
Is it even possible?
Thanks!
Yes its piece of cake if you are interested only in that particular html which is returned by ajax.
Gather information like url, parameters and request type (post/get) from that ajax request.
Generate the same request from your php/curl code and you got it.
And hope that server logic will not check who sent the request.
For this kind of screen scraping you could try phpQuery or Snoopy.
phpQuery has a web browser plugin and scoopy claims to simulate one
you can always bind to the event that is fired when the xhr returns data to the browser and do your operations there.
var xhReq = createXMLHttpRequest();
xhReq.open("GET", "ur_php_url.php");
xhReq.onreadystatechange = onResponse;
xhReq.send(null);
function onResponse()
{
// do the necessary
}
Yes, it is possible.
What you need to do is the following:
Create a CURL call to that webpage in order to retrieve any parameter used in the Ajax call that loads the content, which you are looking for.
Create another CURL call to the file called by that webpage Javascript using the parameters that you have gotten using step number 1.
ex. Say you want to get the content of http://www.domain.com/page.html and this page.html retrieves some other data using Ajax, say $("#div").load("http://www.domain.com/ajax/data.php?time=48484&c=487387").
What you will do is to make a CURL request to page.html first, and get the full URL of the Ajax call using preg_match() PHP function or any equivalent function in any other language. After that, create another CURL request to that URL - http://www.domain.com/ajax/data.php?time=48484&c=487387 - and get its content.
You're all set!
Unfortunately Javascript is run client-side, in a browser, so unless the page is loaded in a web browser there is no simple way to do it.
The only way I can think of, is having a browser running in a server’s background, reloading and saving the generated page automatically in a file which will be available for a PHP script to fetch.
Well... I don’t know about anyone who has implemented such an idea.
Better try to get the URL where the div is being populated from. If the div contents are generated through AJAX for example, maybe if you fetch the data-origin URL with cURL, the data will be available for you as well.
I have a php function that I like to run in my code when a particular image is clicked. Currently I'm using an onclick function in javascript but it seems like the function is run everytime I refresh the page as opposed to just when I click the image. Can you guys please help me understand this? Should I be running a AJAX script?
This is an "order of execution" problem: All PHP code is executed before any HTML is presented to the user. If you want PHP to execute based on a user action, you need to initiate a new request to the server (via AJAX or a standard link).
Why don't you just link the image to the PHP file that contains your function call?
If you want it without having to refresh the page, then you can make an asynchronous call via AJAX to the said PHP file, and perform the necessary functions.
I have a php script which takes about 10 seconds to run because it is pulling in data and storing it in the db, I want to display a loading bar whilst this script is running and then once its done load the page the user is on...has anybody any ideas how I can do this? Thanks
At first you need to call the script using an Ajax request. All jQuery ajax methods offer function callbacks that are called when the call is completed. You can show the progress bar when you make the request and hide it, when it is finished:
$("#MyProgressBar").show();
$("#placeholder").load(myurl, function() {
$("#MyProgressBar").hide();
});
You could also use a plug-in like BlockUI.
can any one please help how to get the values from the javascript to php other than while using submit button.
scenario:
i am searching for record, if the record found then, i need confrim alert asking to continue or not, if he click continue how can i say he selected continue
If you want to check without having a page reload, you probably want to execute an AJAX call, then depending on the result returned by the underlying PHP script, take the appropriate action. If you have no knowldege of how to implement this, take a look here
You can never use JavaScript to communicate with the page while it is loading, you can only send a new request to the web server from the JavaScript layer... although you can send that request to the same script that's already running, it will still be a new instance of the PHP script, just like when you open a new browser tab to the same page.
The only way for JavaScript to communicate with PHP at all, is by sending an HTTP request. But you don't have to refresh the page in order to do that if you use AJAX.
AJAX is basically a word to describe JavaScript exchanging information with web pages without refreshing the page. But note that it will still not be able to change variables in the PHP script which is running when the JavaScript code is executed.
In the case of PHP, I've used the open-source library SAJAX which is quite simple. You will find it at http://www.modernmethod.com/sajax/
Hope it helps and good luck!
You can use this as an example using jquery and PHP:
$.post('searchimage.php', { action: 'searchimage', imgreference: $(this).val() },
function(data) {imgsample.html(data);}
);
Basically apply the above function in a document ready function so its run when the page loads.
This can be triggered using $("#prodcode").click() or what ever event handler you want to use.
The php page in my example will get sent the value from imgreference as a post, you can do whatever you want in the php page then return the value which gets added to the imgsample (in this case a td)
hope this helps.