Solved

Adding a javascript confirmation box?

Posted on 2007-11-15
10
1,093 Views
Last Modified: 2010-05-18
Hi, in the code attatched the delete button and the activate / suspend buttons carry out different procedures, at the moment i have a javascript alert that brings up a pop up where the use has to click ok to continue, how do i add this type of alert with a cancel button along side the ok button?

<?php 
	  $qur = "SELECT * FROM Users WHERE Activated = '1'";
$result = mysql_query($qur)or die(mysql_error());  
 
echo "<table border='0' class='style1' style='border-width: 1px; border-color: #cccccc; border-style:solid;' align='center'>";
echo "<tr> <th>Company</th> <th>Account Reference</th> <th>Password</th> <th>Email</th><th>Phone</th><th>Date</th><th>Suspend</th><th>Delete</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
     // Print out the contents of each row into a table
     echo "<tr><td>";
     echo $row['Company'];
     echo "</td><td>";
     echo $row['AccountReference'];
     echo "</td><td>";
     echo $row['Password'];
     echo "</td><td>";
	 echo $row['Email'];
     echo "</td><td>";
	 echo $row['Phone'];
     echo "</td><td>";
	  echo $row['Date'];
     echo "</td><td>";
	 $AccountReference = $row['AccountReference'];
	 echo "<form action='{$_SERVER['PHP_SELF']}' method='post' class='style1' >
  <label>
  <input name='suspend' type='submit' id='suspend' value='Suspend' />
  </label>
</form>";
     echo "</td><td>";
     echo "<form action='{$_SERVER['PHP_SELF']}' method='post' class='style1' >
  <label>
  <input name='delete' type='submit' id='delete' value='Delete' />
  </label>
</form>";
     echo "</td></tr>";
 
}
 
echo "</table>";
if(isset($_POST['suspend']))
{
echo '<script>alert("Are you sure you want to suspend this account?");</script>';
mysql_query("UPDATE Users SET Activated ='0' WHERE AccountReference='$AccountReference'")
or die(mysql_error());  
echo '<script>alert("Account Suspended!");</script>';
echo '<script>location.replace("index.php");</script>';	
}
?>
	  <br />
	  <br />
	  <p align="center" class="style1">Suspended Accounts<br /> 
	 
<?php 
	  $qur = "SELECT * FROM Users WHERE Activated = '0'";
$result = mysql_query($qur)or die(mysql_error());  
 
echo "<table border='0' class='style1' style='border-width: 1px; border-color: #cccccc; border-style:solid;' align='center'>";
echo "<tr> <th>Company</th> <th>Account Reference</th> <th>Password</th> <th>Email</th><th>Phone</th><th>Date</th><th>Activate</th><th>Delete</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
     // Print out the contents of each row into a table
     echo "<tr><td>";
     echo $row['Company'];
     echo "</td><td>";
     echo $row['AccountReference'];
     echo "</td><td>";
     echo $row['Password'];
     echo "</td><td>";
	 echo $row['Email'];
     echo "</td><td>";
	 echo $row['Phone'];
     echo "</td><td>";
	  echo $row['Date'];
     echo "</td><td>";
	 $AccountReference = $row['AccountReference'];
	 echo "<form action='{$_SERVER['PHP_SELF']}' method='post' class='style1' >
  <label>
  <input name='activate' type='submit' id='activate' value='Activate' />
  </label>
</form>";
     echo "</td><td>";
     echo "<form action='{$_SERVER['PHP_SELF']}' method='post' class='style1' >
  <label>
  <input name='delete' type='submit' id='delete' value='Delete' />
  </label>
</form>";
     echo "</td></tr>";
 
}
 
echo "</table>";
if(isset($_POST['delete']))
{
echo '<script>alert("Are you sure you want to delete this account?");</script>';
mysql_query("DELETE FROM Users WHERE AccountReference='$AccountReference'")
or die(mysql_error());  
echo '<script>alert("Account Deleted!");</script>';
echo '<script>location.replace("index.php");</script>';	
}
if(isset($_POST['activate']))
{
echo '<script>alert("Are you sure you want to activate this account?");</script>';
mysql_query("UPDATE Users SET Activated ='1' WHERE AccountReference='$AccountReference'")
or die(mysql_error());  
echo '<script>alert("Account Activated!");</script>';
echo '<script>location.replace("index.php");</script>';	
}
?>

Open in new window

0
Comment
Question by:ashburn69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 29

Expert Comment

by:Badotz
ID: 20294122
Try this:

var ok = window.confirm('ok?');
alert(ok);
0
 
LVL 1

Author Comment

by:ashburn69
ID: 20294210
where would i add that?
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20294294
um, where your alert() is now?

The alert after the confirm is just to show you what you get back from OK/Cancel (true/false) - you do not need it in your code, but you *do* need to assign the results to a variable.
0
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
LVL 29

Expert Comment

by:Badotz
ID: 20294349
Line 42: echo '<script>alert("Are you sure you want to suspend this account?");</script>';

Line 42: echo '<script>var ok=confirm("Are you sure you want to suspend this account?");</script>';

You will have to add more code to decide what to do on the result.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 20294378
Same for Line 94:

echo '<script>var ok = confirm("Are you sure you want to delete this account?");</script>';

and Line 102:

echo '<script>var ok = confirm("Are you sure you want to activate this account?");</script>';
0
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 20294443
You would have to ask the question BEFORE the post - you can not have a server side script controlled in runtime by a client side script.

Meaning as it currently stands the update is done regardless of what the javascript does becuase the echo statement outputs to the buffer - and the PHP script continues execution.

Change the PHP script as follows:

24: echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\" class=\"style1\"  name=\"suspendform\">");

31: echo("<input type=\"hidden\" name=\"delete\"  id=\"delete\" value=\"Delete\">
32: echo("<script> if(confirm('Suspend this account')) { window.document.forms['suspendform'].submit() } </script>");

0
 
LVL 1

Author Comment

by:ashburn69
ID: 20294465
Ok that brings up the cancel option, but how do i make it so if they do click cancel it just does the history back thing, at the moment it carrys out the command if you click either.
0
 
LVL 16

Accepted Solution

by:
grahamnonweiler earned 500 total points
ID: 20294696
Change:

32: echo("<script> if(confirm('Suspend this account')) { window.document.forms['suspendform'].submit() } else { window.history.go(-1) } </script>");
0
 
LVL 1

Author Comment

by:ashburn69
ID: 20299722
Ok i have done the following, iv added
<script type="text/javascript">
<!--
function confirmation() {
      var answer = confirm("Do you want to continue?")
      if (answer){}
      else{
            window.history.go(-1)
      }
}
//-->
</script> into the head of the document and onclick='confirmation()' into the button, what do i have to do to the following script so that it only carries on if the user clicks ok

if(isset($_POST['activate']))
{
mysql_query("UPDATE Users SET Activated ='1' WHERE AccountReference='$AccountReference'")
or die(mysql_error());  
echo '<script>alert("Account Activated!");</script>';
echo '<script>location.replace("index.php");</script>';      
}
0
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 20305724
Any javascript must be carried out in the browser before the POST.

You are using a single PHP script to POST back to itself, so basically you need javascript controlling the original POST.

The original PHP script that you are using is actually very rudimentary and not particulalry user friendly! My advice would be for you to split it in to two seperate scripts - one which creates and outputs the form and the second which performs the various tasks.

You have the basic understanding of how the javascript "confirm" option works so now you can apply that within the PHP script handling the creation of the FORMs.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question