Delete confirmation in php

I am trying to delete a record in mysql using php. What should I add to my code in order for confirmation to be deleted first? Since in my current code it automatically deletes the entry corresponding to the entered pnum.

<html>
<style>
input { font-size: 16px;}
</style>

<?php include('header.php'); ?>
<div id="main_content">


</div>
<?php include('footer.php'); ?>
<head>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="DeletebyPnumIn.php" method="post">
  <td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="9" style="background:#9ACD32; color:white; border:white 1px solid; text-align: center"><strong><font size="3">Delete In-patient</strong></td>
</tr>
<td><font size="3">Patient #:</td>
<td></td>
<td><input type="text" name="pnum" value="" maxlength="15" /><br/></td>


</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Delete" /></td>
</form>
</tr>
</table>



<body>
</body>
</html>

Here is the action of the form:

<html>
<style>
input { font-size: 16px;}
</style>

<?php include('header.php'); ?>
<div id="main_content">

</div>
<?php include('footer.php'); ?>
<head>

    <?php
    $con = mysql_connect("localhost","root","nitoryolai123$%^");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("hospital", $con);

    mysql_query("DELETE FROM t2 WHERE PNUM='{$_POST["pnum"]}'") ;

    mysql_close($con);

    echo "<script>alert('Record successfully deleted!')</script>";
    ?>

Please, help.

+3
source share
4 answers

The easiest way, given your current code, is to simply add the onsubmit action to your form.

<form action="DeletebyPnumIn.php" method="post" onsubmit="return confirm('Really Delete?');">

or something similar.

+4
source

You need to do the trick in the JavaScript Confirm field!

http://www.tizag.com/javascriptT/javascriptconfirm.php

You can also do this in pure PHP using a different page ... but it can annoy your users ...

+3

, . - , , Javascript. , , "", PHP.

:

<a href="delete.php?id=<?php echo $id; ?>" onclick="return confirm('Delete this?');">Delete</a>

, SQL-. , POST GET SQL.

+3

onsubmit="return confirmationMessage()" <form>. , , JavaScript . confirmationMessage , , true, , false, .

+1

Source: https://habr.com/ru/post/1734162/


All Articles