I have a site where I can update my Database. In the same page I also have a tabel with the most importent information in the database. This tabel is generated by a php while loop, and at the end of each row I have a Edit-button and a Delete-button. But I want to have a confirmation to the deletebutton. But I dont knopw how..
This is my code:
mysql_connect("localhost","user","pass") or die (mysql_error());
mysql_select_db("kalender") or die(mysql_error());
$data = mysql_query("SELECT * FROM event where datum > now() ORDER BY `pub` DESC LIMIT 0, 40") or die(mysql_error());
echo '<table>';
echo '<tr>';
echo '<th>Nr</th> <th>V.</th> <th>Tid</th><th>Dag</th><th>Datum</th><th>Ändra</th><th>Ta bort</th>';
echo '</tr><tr>';
while($info = mysql_fetch_array( $data))
{
$eventTime=$info['datum'];
$tid= strtotime($eventTime);
echo '<td width="20px">' .$info['id'] . '</td>';
echo '<td width="20px">' .$info['vecka'] . '</td>';
echo '<td width="40px">' .date("H:i", $tid) . '</td>';
echo '<td width="40px">'.$info['dag'] . '</td>';
echo '<td width="50px">' .date("j M", $tid) . '</td>';
echo '<td width="70px"><a href="http://kalend.mizgalski.se/update.php?id=' .$info['id'] . '"><button id="checkoutbutton" type="button"><Edit</button></a></td>';
echo '<td width="70px"><a href="http://kalend.mizgalski.se/delete?id=' .$info['id'] . '"><button id="checkoutbutton" type="button">Delete</button></a></td></tr>';
}
echo '</table>';
Please help me!
You try this.
In PHP Script: Update this in while loop
...
...
$tempId = $info['id'];
echo "<td width='70px'><a href='javascript:;' onClick='go_edit($tempId);'><input type='button' value='Edit'/></a></td>";
echo "<td width='70px'><a href='javascript:;' onClick='go_del($tempId);'><input type='button' value='Delete'/></a></td>";
...
...
In Javascript: Add these functions in under php script
<script>
function go_edit(id) {
window.location.href = "http://kalend.mizgalski.se/update.php?id="+id;
}
function go_del(id) {
var choice = confirm('Are you sure want to delete?');
if(choice) {
window.location.href = "http://kalend.mizgalski.se/delete?id="+id;
}
}
</script>
You can do confirmation using javascript on the client side before sending data to the server