Jump to content

Using a javascript popup to control a php function


seniramsu

Recommended Posts

How can I use a javascript confirm pop-up box to tell my php code whether or not to execute?

 

I'm setting up an admin page for a client to self-manage the photos in a photo gallery.

Its similar to facebook's photo management system.

 

I have a delete button within an html form, to be used for removing an html table element from the webpage (which is generated using php echo commands), as well as a table row within my database:

 

     
//make a javascript popup that asks "are you sure you want to delete?"		
     if(isset($_POST['delete_' . $id])) {
          
          $true = $_POST['delete_' . $id];
         
    //begin javascript popup
          echo "<script language='javascript'>\n";
          echo "function show_confirm() {\n";
          echo "var r=confirm(\"Are you sure you want to delete this album?\")\n";
          echo "if (r==true) {\n";
          echo "alert(\"Album Deleted\")\n";
          echo "} //if cancel is clicked, this php delete code will exit (else condition would go here?)\n";
          echo "}</script>";
     }

//delete album
     if(isset($_POST['delete_' . $id])) {
     
    //actually deletes the information from the database
          $queryDelete = "DELETE FROM `ridgeline`.`albums` WHERE `albums`.`album_id` =" . $id . "";
          $resultDelete = mysql_query($queryDelete, $db_server) or die("action failed." . mysql_error());
          
          echo "album: \"" . $name . "\" has been deleted";
     }

I CAN remove the html element with my (other, not shown) php code, as well as remove the item from the database through the query.

 

What I CAN'T do (yet) is be able to cancel out of the delete request using the Jscript popup 'cancel' button. As of now, if I hit delete in my form but click 'cancel' in the jscript popup, the delete request still carries through regardless. No bueno.

 

Suggestions? Thanks  8)

Javascript is browser based, PHP is server based.  You cannot call a PHP function with Javascript.  However you can stop the active link with Javascript.

 

<script type="text/javascript">
<!--
function confirmSubmit()
{
var agree=confirm("Are you sure you wish to delete?");
if (agree)
return true ;
else
return false ;
}
//-->
</script>

<a onclick="return confirmSubmit();" href="?delete=<?php echo $id; ?>">Delete</a>

For sure. I did find out that you can call javascript from within your php (in the body vice the head), by echoing each line of jscript code sequentially in php. (see my code above).

 

It seems logical to me that if i were to echo a javascript function from within php (which works), I should be able to insert my own php variables into the javascript.

If you want to insert the contents of PHP variables into server-side generated javascript, you could do something like this:

 

echo "<script type=\"text/javascript\">";
echo "var foo = {$bar}";
echo "</script>";

 

I think that that should work. However, I'm by no means a Javascript or PHP whiz, so forgive me if that code is wrong.  ;D

You want to look into using AJAX. The following is an example of what you could do.

 

First put your delete code in a separate php file which could be called deletestuff.php

 

Then the ajax would be something like this

 

function doSomething()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET","deletestuff.php",false);
	xmlhttp.send(null);
}
else
{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open("GET","deletestuff.php",false);
	xmlhttp.send();
}
alert('That stuff has been deleted')
}

 

If you need something from the php file to be sent back to your javascript function then you could use responseText like so

 

alert(xmlhttp.responseText);

 

That will pop up a javascript alert box of whatever you echo'd in the php file

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.