When a user wants to delete a row from a database via my webpage, I want to prompt a message-box asking, "Are you sure you want to delete?". Then in my php code, I want to track if the user press Ok or Cancel and execute the delete operation as shown bellow. I am new to web-programming and any help would be appreciated.
<?php
.
.
.
if (user wants to delete a row from database)
{
echo "<script type=\"text/javascript\"> confirm(\"Are you sure you want to delete?\"); </script>";
if(user press OK to delete")//How can I get input from the dialog box?
{
$deleterow = "DELETE FROM ElectronicShop WHERE WorkOrder = $i";
$datadelete = sqlsrv_query($connectString, $deleterow) or die(print_r(sqlsrv_errors(SQLSRV_ERR_ALL), true));
if($datadelete)
echo "<br>you have deleted: $i Successfully";
else
echo "<br>could not be deleted: $i";
}
else
echo "User pressed Cancel";
}
?>
You can simply add the question to your onclick event
...
or with jquery
...
$(".delete-confirm").click(function(e) {
if (!confirm("are you sure"))
e.preventDefault();
});
Related
I want to go back to previous page after pressing "OK" on the function message prompted.
Php Code
function function_alert($message) {
// Display the alert box
echo "<script>alert('$message');
document.location='javascript://history.go(-1)';
</script>";
}
// Function call
function_alert("This email has already subscribed");
}
Alert Box
Thank you so much!
Tried this
function function_alert($message) {
echo "<script>
alert('$message')
history.back()
</script>";
}
Below is my delete code using php,I want to get confirmation after delete link was clicked from the user using php.
<?php
include('conn.php');
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[st_id]'");
if($query)
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Succesfully deleted')
window.location.href='mark-details1.php';
</SCRIPT>");
}
else
{
echo "Check your Server";
}
?>
Please anyone can tell me how to do this? Thanks in advance!
you can use this javascript event on your html tag.
onclick="return confirm('you sure?');"
you can also use this :
if your link will send a get like "?delete=(id)"
<?php
include('conn.php');
if(isset($_GET['delete']) && is_numeric($_GET['delete'])==1){
echo (a page with a form with confirmation question content that will sent a get for example (?checked_delete=(id)));
}elseif(isset($_GET["checked_delete"]) && is_numeric($_GET["checked_delete"])==1){
// TODO : deleting record.
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[checked_delete]'");
header("location:mark-details1.php")
}else{
echo (normal page);
}
?>
I suggest using Ajax. Here I will use jQuery to do it
<a class="wh" data-id="<?=$rows['student_id']?>" href="edit-mark.php?st_id=<?=$rows['student_id']?>" title="Edit">Edit Marks</a><span class="confirmation"></span>
using
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
$.get(this.href,function(data) { // does the student still exist?
if (confirm("delete" +data+"?")) {
$.get("otherphp.php?st_id="+$(this).data("id"),function(data) {
$(this).next().html(data); // show response
});
}
});
});
});
Or to hide the href from spiders
<a class="wh" href="onlyworkswithjavascript.html"
data-id="<?=$rows['student_id']?>" data-href="edit-mark.php?st_id=<?=$rows['student_id']?>"
title="Delete">Delete Marks</a><span class="confirmation"></span>
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
if (confirm("delete" +data+"?")) {
$.get($(this).data("href"),function(data) {
$(this).next().html(data); // show response
});
}
});
});
when we are inserting the value there is a column name code that is auto generating in mysql. I want to display the last inserted value code column in pop up window.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
require_once 'db.php';
// Escape user inputs for security
$company = mysqli_real_escape_string($test_db, $_POST['company']);
$project = mysqli_real_escape_string($test_db, $_POST['project']);
$revision = mysqli_real_escape_string($test_db, $_POST['revision']);
// attempt insert query execution
$sql = "INSERT INTO live (company, project, revision, code) VALUES ('$company', '$project', '$revision', '')";
if(mysqli_query($test_db , $sql)){
echo '
<script type="text/javascript">
alert("i want to show last inserted code here and redirected to a page on clicking close button");
</script>';
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($test_db);
}
// close connection
mysqli_close($test_db);
?>
Assuming that your "live" table has a primary key auto-increment column, then this should work:
if(mysqli_query($test_db , $sql)){
echo '
<script type="text/javascript">
alert("ID of last inserted row: '.mysqli_insert_id($test_db).'");
</script>';
}
I have a remove page where it can remove an entry from my database which is pre loaded into the option box. However, it redirects to my 'remove-module-complete.php' page when I type in any random text. I only want it to direct to the 'remove-module-complete.php' if their is that actual entry from the database. My javascript doesn't seem to be working.
I currently have some validation to say that the entry 'didnt match any item' from jQuery but it still directs me to 'module remove complete' if I press submit.
Any help?
Here is my PHP
<?php
if (isset($_POST['submit'])){
$moduleCode = $_POST['moduleCode'];
$moduleCodeLen = strlen($moduleCode);
if ($moduleCodeLen=6){
$sqlTwo = "DELETE FROM MODULES WHERE id = '$moduleCode'";
mysql_query($sqlTwo);
$resTwo =& $db->query($sqlTwo);
if(PEAR::isError($res)){
$errorVar = ($res->getMessage());
echo "<script type='text/javascript'>alert('Invalid entry, please try again. Error code: $errorVar');</script>";
die;
}
else echo "<script> location.replace('http://project.ac.uk/remove-module-complete.php'); </script>";
}
else echo "<script type='text/javascript'>alert('Invalid entry, please try again');</script>";
}
?>
Seems like you have some errors(commented lines):
if (isset($_POST['submit'])) {
$moduleCode = $_POST['moduleCode'];
$moduleCodeLen = strlen($moduleCode);
// '==', not '='
if ($moduleCodeLen==6) {
$sqlTwo = "DELETE FROM MODULES WHERE id = '$moduleCode'";
mysql_query($sqlTwo);
$resTwo =& $db->query($sqlTwo);
// there is no $res variable in the given part of the code
// maybe you need to use $resTwo variable here?
if (PEAR::isError($res)) {
$errorVar = ($res->getMessage());
echo "<script type='text/javascript'>alert('Invalid entry, please try again. Error code: $errorVar');</script>";
die;
}
else echo "<script> location.replace('http://project.ac.uk/remove-module-complete.php'); </script>";
}
else echo "<script type='text/javascript'>alert('Invalid entry, please try again');</script>";
}
When a row's select button is clicked, a confirmation box pops up.
Upon confirming the box, the select-button shall become disabled, and a message is appended that shall fade out.
Both, button disabling and message appending work but only for a second.
The button does not remain disabled. Also the message appends correctly but only for a second too.
I could change the fadeOut(Int) to any Integer but still the message only shows up for a second. Why does functionality only work for a second?
<script>
$('#button_<?php echo $platform->id; ?>').click(function() {
var choice = confirm('Please confirm that you wish to do the following Platform: <?php echo $platform->company; ?>');
if (choice == true) {
$('#button_<?php echo $platform->id; ?>').prop("disabled", true);
$('.job_confirm').css('visibility', 'visible');
$('.job_confirm').append('The Job has been added to your Userarea -->');
$('.job_confirm').fadeOut(5000);
}
});
</script>
try event.preventDefault();
<script type="text/javascript">
$('#button_<?php echo $platform->id; ?>').click(function(event) {
event.preventDefault();
var choice = confirm('Please confirm that you wish to do the following Platform: <?php echo $platform->company; ?>');
if (choice == true) {
$('#button_<?php echo $platform->id; ?>').prop("disabled", true);
$('.job_confirm').css('visibility', 'visible');
$('.job_confirm').append('The Job has been added to your Userarea -->');
$('.job_confirm').fadeOut(5000);
}
});
</script>