I am working with PHP and mySQL. On page1.php there will be an image that when the user clicks on it there will be a query that inserts into mysql some data. How can I do this without reloading the page or transferring the user someone else and change the image?
What I did so far is to do this but with reloading the page.
Thanks for your ideas/examples.
It is as simples as executing the post method of jQuery library.
Here is an example that calls a PHP script when clicking any image.
$(document).ready(function() {
$("img").click(function() {
$.post("executequery.php");
});
});
In the "executequery.php" you would need to build the SQL and execute it.
Here is the documentation of the method. http://api.jquery.com/jQuery.post/
Using jquery, add an ajax call to a php script which should be able to run the insert script and return a success message.
Related
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.
I have a PHP file that has an HTML form that submits via AJAX to the database. When I hit the form submit button, every PHP query updates itself. Is this how Ajax normally operates? or if I switch the parent file from PHP to HTML, will it eliminate the unwanted updating of all the PHP on the page?
jQuery ajax submits the request to a seperate php file to load data. You can choose to load only poritions of pages. $.load does this easier. Look here: http://api.jquery.com/load/
You said it submits via AJAX to the database - but have you actually programmed it to do that?
Post a sample of the AJAX code and we can check it for you.
I have a feeling you don't really understand the technology you are using. What is a 'PHP query'? You want to switch the page from PHP to HTML? Well, can you? Does the page have PHP in it or not? It wouldn't make any difference to AJAX code, but I have a feeling you don't actually have AJAX code.
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 page with a mysql connection, a select query and then i'm building a table using PHP. If i wanted to use jQuery AJAX to refresh the data on a setInterval, on the same page, how would i go about doing that? (by the way i can do it to another PHP page but i've never done it if the PHP stuff is on the same page)
If you want to keep making ajax calls i suggest you do long polling, which basically means you have a script that is requesting content via ajax every given time, and it will verify every time if the content has been modified, if not it will wait again and make another call.
I used jQuery Periodical updater for a chat box and it worked perfectly.
If you wnat to learn more about how jQuery and AJAX work, check out this Nettus article
You set the param in request, for instance file.php?ajax=1
The, depending on its value, you render full html or just the necessary elements for ajax
in php:
if($_GET['ajax']) renderAjax();
else renderFullHTML();
in js:
$.get('file.php?ajax=1', function(data) {
$('.result').html(data);
});
Can anyone explain how to run PHP code from within javaScript for submit?
Currently I submit the form to a page.php, connect to MySQL, run some queries then build up the HTML and echo it out.
However, I want to do this without going to the page.php as I am trying to show the results in an ajax dialog using JQuery-UI. It works, just I have to submit to page.php to make it happen.
Would I do something like build up the code in Javascript writelin statements and dynamically load a div or something?
Use jQuery's post() method to send the request to page.php from JavaScript asynchronously. You can use the success option to show the dialog http://api.jquery.com/jQuery.post/