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)

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.