Link to home
Start Free TrialLog in
Avatar of xedstr
xedstr

asked on

Asking confirmation before DELETE

Hi,

I use fllowing parts in my code :

CODE =>
...
if($_GET['action']=="delete")
{ $result2 = mysql_query("DELETE FROM link WHERE QuestionNr=$id",$con);
....//and other delete actions
}

<form>
       <input type=hidden name="id" value="<?php echo $id ?>">
       <input type='submit' value='Delete question' name='delete'  o       onclick='checkBeforeDeleteQuestion()'>

       <input type="button"  Name= "delete" value="Delete" onClick="
      if (confirm('Do you wish to delete?'))
      {this.form.action='<? $_SERVER['PHP_SELF']."?action=delete" ?>';this.form.submit();}">
</form>

___________________________
This code is not doing what I intended to do...
If I press OK in the confirm-screen => I get only redirected to the same screen without any delete.

Can anyone help me out of here? Is there a better solution (using only PHP for example)?

Thanks in advance,
EDS

Avatar of bljak
bljak

this line
{this.form.action='<? $_SERVER['PHP_SELF']."?action=delete" ?>
should be rather
{this.form.action='<? echo $_SERVER['PHP_SELF']."?action=delete"; ?>

and i am not sure about JavaScript syntax this.form.action

//bljak
CODE =>
...
if($_GET['action']=="delete")
{ $result2 = mysql_query("DELETE FROM link WHERE QuestionNr=$id",$con);
....//and other delete actions
}

<form name=toto method=post action="">
      <input type=hidden name="id" value="<?php echo $id ?>">
      <input type="button"  Name= "delete" value="Delete" onClick="
     if (confirm('Do you wish to delete?'))
     {document.toto.action='<? $_SERVER['PHP_SELF']."?action=delete" ?>';document.toto.submit();}">
</form>


I don't think this is necessary :
      <input type='submit' value='Delete question' name='delete'  onclick='return checkBeforeDeleteQuestion(),'>

However tested your code how it is and it redirects me to same page with ?id=xxx
meaning it submits name and value from hidden
i am not great JavaScript expert but it's JavaScript error however

//bljak
in my proposal, and althought it's considered bad practice :D, I left the $action being in $_GET and the $id in $_POST

for security and such
Avatar of xedstr

ASKER

I used before the following javascript but it was not correct because, even if the user presses CANCEL, the delete is performed...

<script language="javascript" type="text/javascript">
function checkBeforeDeleteQuestion()
{ var answer;
  answer=confirm("Are you sure this question must be deleted?");
  if(answer!=true)
    { return answer;
    }
  else
    {
      return false;
    }
}
</script>

-----------------
So, I prefer another way to solve this problem... using php.
The first time the users enters the page : only the id-number is mentioned.
On the page, the visitor can view the information and eventualy delete the item in the database.
When he wants to delete the information, the user must receive a page that asks his confirmation. Only if confirmed, the cancel-proces may take place.

I think this must be possible to code in PHP, from 1 page. Can anyone help me to get me on track?

EDS

ASKER CERTIFIED SOLUTION
Avatar of VGR
VGR

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xedstr

ASKER

I have in fact reformed completely my page and used PDP this way :

Below you will find the structure .
In the delete I ask the user for confirmation and after that he has choosen YES he gets the confirmation for the delete with possibility to go to another page.

<?php

if(isset($_GET['action']))

    $action = $_GET['action'];

else

   $action = '0';



switch ($action) {

    case 'add':

        echo "Add.";

    break;

    case 'edit':

        echo "Edit.";

    break;

    case 'deleteConfirmed':

        echo "DeleteConfirmed.";

    break;

    case 'delete':

        echo "Delete.";

    break;

    default:

?>



<html>
<title></title>
<body>
<a href="/checkbook/index.php?action=add">Add</a><br>
<a href="/checkbook/index.php?action=edit">Edit</a><br>
<a href="/checkbook/index.php?action=delete">Delete</a><br>
</body>
</html>

<?php

    break;    

}