I want to stop php code from executing!

Discussion in 'PHP' started by bookimal, Mar 24, 2007.

  1. #1
    Hi all,

    I'm really stuck on this...

    so I have a page called commands.php, which allows someone to delete a command from the mysql database..

    
    <html>
    <head>
    <script type="text/javascript">
    function my_alert(){
      var where_to= confirm("Do you really want to delete this command??");
      if (where_to== true) {
        //do nothing, deleteCommand.php will be called
      } else {
        //stay here!!!!!
        window.location="commands.php";
        return false;
      }
    }
    </script>
    </head>
    <body>
        <b>Delete a unit: </b><br>
        <form name="myform" action="deleteCommand.php" method="post">
        Please select the unit number:
        <select name="unitNum"> 
    <?php
        $unit_commands = array();
        $unit_commands[0] = 1;
        $unit_commands[1] = 2;
        $unit_commands[2] = 10;
        for($j=0; $j < count($unit_commands); $j++) {
         // print $unit_commands[$j];
          print "<option value=$unit_commands[$j]>$unit_commands[$j]</option>";
        }
    ?>
        </select><br>
        <input type="Submit" name="a" value="Delete Command" onclick=my_alert();>
        <br></form></body></html> 
    
    Code (markup):
    deleteCommand.php:
    
    <?php
      if (isset($_POST['unitNum'])) {
        $unitNum = $_POST['unitNum'];
      }
      
      // establish link
    	$username="root";
    	$database="red1";
                 $password="abc";
    	$dbtable = "commands";
    	$link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");
    
    	//select database
    	@mysql_select_db($database) or die("Unable to select database");
    	
      $query="DELETE FROM `$dbtable` WHERE `$dbtable`.`unit_id` = $unitNum LIMIT 1;";
    	$result=mysql_query($query, $link) or die("Unable to delete");
    
    	//close link
    	mysql_close($link) or die("Unable to close database");
    
    ?>
    Code (markup):
    So my problem is... Even if the user clicks "cancel" in the confirm box, deleteCommand.php is still executed! I did the window.loaction thing, but thats not working... How can I stop it.. :(

    Thanks in advance!!
     
    bookimal, Mar 24, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Actually, it's a javascript problem, but here's the solution:

    1. in my_alert(), change this:

    
      if (where_to== true) {
        //do nothing, deleteCommand.php will be called
      } else 
    
    Code (markup):
    to this:
    
      if (where_to== true) {
    	this.submit();
      } else {
    
    Code (markup):
    2. in your form, change this:
    
    <input type="Submit" name="a" value="Delete Command" onclick=my_alert();>
    
    Code (markup):
    To this:

    
       <input type="Submit" name="a" value="Delete Command" onclick="my_alert(); return false;" />
    
    Code (markup):
    Tested and works.
     
    sea otter, Mar 24, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    To stop the form from submitting, you want the onclick to return false. It's actually just your function that is returning false, which would be the equivalent of onclick="false". You need onclick="return false;" so you want the onclick attribute to return the value from the function.

    In other words, replace:
    <input type="Submit" name="a" value="Delete Command" onclick=my_alert();>
    HTML:
    with
    <input type="Submit" name="a" value="Delete Command" onclick="return my_alert();">
    HTML:
    You can also get rid of the window.location bit in the function. In fact, it doesn't really require a function at all.
    onclick="if(!confirm('Do you really want to delete this command?')) return false;"
     
    rodney88, Mar 25, 2007 IP