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

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.

Related

PHP and Ajax if ajax calls same php page, is entire page reevaluated/reloaded?

I have a PHP page that displays a form. The buttons are a buttons (not submit). When clicked they call a function in Javascript that issues and Ajax call. The URL that the Ajax uses is to to the same php page, passing variables. The top of the php page checks if these variables exist and if so they do their database work, in objects, echo a message back to Ajax and call exit().
My question is, after the exit() is the rest of the php page re-evaluated/executed? I can't seem to find an answer. Basically, right after I call exit(), if I use the objects to query the database to determine what to display, does this happen?
If the ajax calls the same php page, is the entire php reevaluated?
Short answer: It depends. It should not if is designed correctly.
Long answer. Normally you use PHP scripts to serve HTML, and in your case this is what it does - it returns the HTML that displays the form and probably on submit will interpret the results. But, an PHP script (program) can do much more things. You should consider the URL only your entry point to an application, not to a PHP script. This URL can serve different parts of code depending on the type of request. For example it can detect that some part of the form is doing an AJAX request to the same entry point and divert that call to a part of the code that will respond only to the AJAX request. In PHP this kind of branching can be done using control structures. Branching when some AJAX headers are detected or when a POST request is presented are quite common examples in PHP world.
So to respond again to your question. The entire page should not be reevaluated if you branch your code for this, and then execute only the parts of code that correspond to the AJAX request not the ones that are creating your HTML.
Example:
if ($_SERVER['HTTP_X_REQUESTED_WITH']) {
// treat the ajax request
}
if ($_SERVER['REQUEST_METHOD'] === "POST"){
// do stuff for a POST request
}

Is my logic correct? Ajax and/or PHP with Mysql

I have a page which shows a list of items. Page coded with html, css, php and using mysql db.
On that page a user can request to add one of the items to their special list.
I want to do this within the page without having to do a complete page refresh. So user clicks button to add, item is added to their list and button changed so they can't add it again.
Do I use ajax calls to run code behind the page and then refresh the div?
Or is there a better more efficient way to do it.
I'd prefer a php option of possible in case user has js turned off, but don't know if it can be done with using js.
Any help appreciated.
If you want dynamic content (changing the page without refreshing) you are going to have to use Javascript. To do what you are asking, you could call a PHP script via Ajax that outputs the contents of the div with the new item, and then change the div based on that response.
Dagon is exactly right. Create a form which handles the request and set the action of the form to the PHP script you want to handle the request. Note that although this can be the same php script that you use to process your ajax request, it does not necessarily have to be.
Many times when I implement such functionality, I'll set the PHP to send variables as POST (in the event of JS disabled) and have my ajax request as a GET so I can use a single PHP page to handle the 'same' request. When using AJAX, I'll have the script echo a specific code then have the ajax response handle that return.
if(val == 'OK') {
//in event of success, perhaps you want to hide the original form and show a success message
} else {
//do something like unhide a hidden div to display an error
}
If JavaScript is turned off, the page has to be reloaded. In your case jQuery could be very handy and simply rewrite the element you need to rewrite. The server send's a simple json. Using a PHP Framework might also be a good idea, since the way you ask it seems (with respect, and not wanting to offend), that you are not using any framework and might run into falls making your script vulnerable (sql injections for example)
If your visitor doesn't have JavaScript enabled and you want to serve anyways, then you have to do a page reload. First check if that is worth to do, who is your client/visitor, what browser do they use, ... questions like that help you to design your page/app.

AJAX form with Website optimizer

I'm trying to track conversions on a button that is triggered by AJAX with visualwebsiteoptimizer's Custom Conversion Goal Code which is generated javascript code.
If I put it in php file that is being triggered by AJAX would it run this javascript code aswell sending details back to the visualwebsiteoptimizer?
Or is there a better way of doing this? (ie onclick event on the submit button...)
You are not going to be able to run javascript from inside your PHP script. Javascript needs to be executed by the browser (at least in this case).
Your best bet is to run the custom conversion code in an onclick (or similar) event. You may want to look into using a $('form').submit() event or the callback function for your ajax request.

calling a php function in my code with clicking an image

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.

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