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
https://forums.phpfreaks.com/topic/213631-run-php-within-js-function/
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);

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.