views:

86

answers:

5

I need to have a button that calls a php function to resort displayed data from the db. How the heck do I do this? I just found out that because php is server side this sucks. So any help please?

+2  A: 

It should probably sit inside a form field, something like this:

<form action="YOUR_PHP_SCRIPT.php">
<input type="submit" />
</form>

When the submit button is pressed, the action for the form is triggered.

There may be a swathe of other things you'll need to take into consideration from this point onward, but this is a start.

Danjah
A: 

what Yacoby said, you'll need to use AJAX to make the call to the server, or something like this: http://www.ajaxdaddy.com/demo-sorted-table.html

Andre
+2  A: 

Yeah because PHP is server-side, you have two options. One is to make a button that calls the PHP script and renders a completely new page. The other is to use AJAX (asynchronous javascript and XML) on the page, see jquery.com for a good way to do that, and only re-render the table that is displaying data.

Sam
+3  A: 

You can't directly call a PHP function pressing a button for the reason you stated yourself. In order to execute PHP code you need to make a new request to the server. The request could be made to another script or to the same that produced your page by calling it again with some parameter to control its behavior.

Alternatively, you can call a PHP script via Javascript (AJAX) so that you can handle its output and update the page without a full reload.

The second option is neater but more complex, the first one might look less pleasing to the eye, but works regardless of the user's browser having Javascript enabled or not.

kemp
OH MAN. I know nothing about AJAX but this is probably the way to go.Thanks for your help.
creocare
A: 

This is a job for ajax, as others mentioned. If I may elaborate, if you're starting out, I HIGHLY recommend using a javascript library to make your life easier. With prototype, here's what your button might look like:

<input type="button" id="button_foo">Button</input>

Here's what your javascript might look like:

$('button_foo').observe('mousedown',function(e){
    new Ajax.Request('handler.php',{
        method:'post',
        onSuccess:function(t){
            $('table_bar').update(t.responseText);
            }
    });
});

This may seem a little daunting at first, but I think basic js has a pretty manageable learning curve when using a library. The above code would take whatever handler.php outputs, and replace the contents of an element with and id of "table_bar" with the returned html.

if you do decide to use prototype, the docs are really helpful and easy to understand, and there is a really excellent book by pragmatic press on the subject that'll have you understanding it very quickly.

Hope that helps!

Jesse
Thanks this is great. I ended up using something similar to this but I may change my code out for this. I love SOF. Thanks for your help.
creocare