calling a php function in my code with clicking an image - php

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.

Related

Calling a php function on page load

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.

How to call sql command in php without submitting a form?

I want to execute sql command such as
$query_run = mysql_query($query);
in php. But the only way I can find to trigger php event is to submit a form using:
if(isset$POST['submit']) {
$query_run = mysql_query($query);
}
But this would refresh the page and wipe off every thing that dynamically created on the page.
What I want is to have a button, press which to trigger sql commands without reloading the page like onclick in js. However, sql command cannot be run inside js function (as I can find).
PHP is parsed server side which means you can't have it run code without making a server request. Once the page is delivered to the client any php parsing is complete.
You can create a button or link that will send off an http request through javascript, an AJAX call, to a small php script that does the database work you want done. You wouldn't have it call your whole page again. The output from this gets returned to the javascript and can be used to update a CSS block element with success or failure notices.
Here's the W3C Ajax tutorial http://www.w3schools.com/ajax/default.asp
What you need to do is an AJAX call. This requires Javascript (preferably with the help of a library such as jQuery) to assemble the date from the page, make the call to the PHP page, and to present the results onto the page.

Calling PHP function after jquery code

Dear Firends I have large number of forms on a single web page all of them calls a single PHP function. However what I want is that the forms should call a jquery function and if there is a need then jquery should let it call the PHP function.
I do not want to use Ajax just want to create a PHP function call if the matter can not be solved by jquery.
Each of the form is associated with some data. how ever all the data that is displayed on the page is not available all the time. So what I want is
if (data == available) { call PHP}
elseif (data != available) { jquery alert('sory bro');}
if data can not be seen now just use jquery to say sorry (no need to check from server). When a page is loaded we know which all pieces of data can not bee seen and are given in different color.
The forms are generated using a PHP loop with each form showing different data but of same type (each form is assocaited with a sort of Article).
All the questions that I have seen are about Ajax. Where as my current PHP code is working fine. all I want it that before making a trip to server if the data is not available the jquery shoould say so. We already know which data is not avaiable so far.
I hope I have explained it
Thanks a lot
**I think I have not made my point clear.. When the page is loaded is already know which data is not available for display and it is marked in seperate color and the div has different arrtibute...*is there some way so that I do not call PHP function for those forms?
PHP executes on the server side. Javascript (jQuery) executes on the client-side. So PHP is completely done executing before Javascript starts executing.
That's why everyone is saying you need to use AJAX. AJAX is a way to make a call back to the server in order to execute PHP code. PHP code only executes on the server. So in order to execute PHP, you have to make a call back to the server.
According to your logic, the data is present on the server.
If you want to know if the data is available or not then you have to contact the server right.
If that's the case how can it be solved without sending an ajax request..
You need to make the request as jQuery is a client side code and cannot contact the server directly.. You need the server side script to execute it which is PHP in your case
The easiest solution:
if (data == available) { $("form").trigger('submit') }
elseif (data != available) { jquery alert('sory bro');}
Obviously you need to adapt the selector according to your specific form / requirements.
Just make sure your form does not get submitted accidentally when you press submit by adding something like event.preventDefault(); in your function.

How to get content of a javascript/ajax -loaded div on a site?

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.

passing values from java script to php

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.

Categories