Sign in to follow this  
SteveDeFacto

[web] How to execute an server side PHP script from javascript?

Recommended Posts

I have a javascript function that needs to call a php script on the server to delete a database table without redirecting the page. I need to call the delete script in a for next loop then when it's done I need to refresh the page. Basically I have a list of check boxes that it needs to iterate through and delete each message that is checked. Something like this:
function Delete(chk)
{
  for (i = 0; i < chk.length; i++)
  {
    if( chk[i].checked )
    {
      ExecuteServerSideScript("delete_message.php?message=" + i);
    }
  }
  window.location.reload()
}

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Have you looked into using Ajax? There are some rather nice javascript libraries that make it easier, e.g. Prototype. You could also consider making just one request with a list of identifiers rather than N requests.

I personally use jQuery for that... however this is exactly what you need: Prototype or jQuery's Ajax modules. You can write it yourself, however that is a pointless waste of time, especially considering all of the cross platform/cross browser/cross version crap you have to put up with to even get your Ajax calls working right.

Share this post


Link to post
Share on other sites
I use jQuery too. Highly recommended. Doing what you want can be done in like 5 lines of code, max.

/* include jquery JS in header */

$.post(
'server_file.php',
{ var1: 'value', var2: 'value2' /* etc */ },
function( event ) { /* custom callback, optional */ }
);

Boom, yer done.

Quote:
...I have a javascript function that needs to call a php script on the server to delete a database table without redirecting the page...


Delete a whole table? That's the part that worries me. I smell bad DB design or bad word choice, one of the two.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this