Jump to content

Run PHP within JS function


coupe-r

Recommended Posts

I am trying to display a js dialog box when someone wants to delete something.  Currently I have it all with buttons and PHP in the background.  When they click the delete icon, I want a dialog box to appear with OK or Cancel.  On cancel, return back to the same page, If OK, I want it to run the PHP/mysql code that deletes the record from the DB.  Once deletes, I want the box to appear again saying it was deleted successfully.

 

I found this but now sure how to make it work.

 

function show_confirm()

{

var r=confirm("Press a button");

if (r==true)

  {

  alert("You pressed OK!");

  }

else

  {

  alert("You pressed Cancel!");

  }

}

Link to comment
Share on other sites

You would have to be looking at ajax here. It's pretty simple

 

1 call function

 

2 if they confirm

    call the ajaxfunction

 

3 when the  ajax function is complete call the alert box again

 

this should help

 

ajaxget("delete.php?id=xxxx","resultsDiv","confirmdelete")

 

function confirmdelete(){

alert("Delete Done");

}

 

function ajaxget(url,outputid,funcToCall){

var AJAX;

  try { 

  AJAX = new XMLHttpRequest(); 

}

catch(e) { 

  try  {   

  AJAX = new ActiveXObject("Msxml2.XMLHTTP"); 

  }

  catch(e)  {   

  try  {

    AJAX = new ActiveXObject("Microsoft.XMLHTTP");     

  }

  catch(e)  {     

    alert("Your browser does not support AJAX.");     

    return false;     

  }   

  } 

}

AJAX.onreadystatechange = function()

{

  if(AJAX.readyState == 4)  {

  if(AJAX.status == 200)  {

  //execute the function

  eval(funcToCall  + '()'); 

  //write the resulting html to the div (or whatever)

document.getElementById(outputid).innerHTML=AJAX.responseText; 

  }  else  {

    alert("Error: "+ AJAX.statusText +" "+ AJAX.status);

  }

  } 

}

AJAX.open("get", url, true);

AJAX.send(null);

}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.