User clicks on Delete button. Javascript alert box popups with OK and Cancel. If user clicks OK, then page reloads (post form) and I need to create php variable with value from here name="confirm_delete"
)
Here is my code
<form action="<?php echo (htmlspecialchars($_SERVER["PHP_SELF"])) ?>" method="post">
<input name="delete" type="submit" id="delete" value="Delete">
<input type="hidden" name="confirm_delete" id="confirm_delete" value="0" >
<script>
$(document).ready(function() {
$("#delete").click(function(){
var answer = confirm("Are you sure you want to delete?");
if (answer){
return true;
document.getElementById('confirm_delete').value = 1;
} else {
return false;
document.getElementById('confirm_delete').value = 0;
}
});
});
</script>
Then print_r($_POST['confirm_delete']);
but value always is 0. That means that document.getElementById('confirm_delete').value = 1;
does not work.
Please, advice what need to correct
User clicks on Delete button. Javascript alert box popups with OK and Cancel. If user clicks OK, then page reloads (post form) and I need to create php variable with value from here name="confirm_delete"
)
Here is my code
<form action="<?php echo (htmlspecialchars($_SERVER["PHP_SELF"])) ?>" method="post">
<input name="delete" type="submit" id="delete" value="Delete">
<input type="hidden" name="confirm_delete" id="confirm_delete" value="0" >
<script>
$(document).ready(function() {
$("#delete").click(function(){
var answer = confirm("Are you sure you want to delete?");
if (answer){
return true;
document.getElementById('confirm_delete').value = 1;
} else {
return false;
document.getElementById('confirm_delete').value = 0;
}
});
});
</script>
Then print_r($_POST['confirm_delete']);
but value always is 0. That means that document.getElementById('confirm_delete').value = 1;
does not work.
Please, advice what need to correct
Share Improve this question asked Oct 17, 2013 at 7:29 user2465936user2465936 1,0404 gold badges17 silver badges32 bronze badges 1-
1
place your return statements after the
document.getElementById()
mands – Patrick Manser Commented Oct 17, 2013 at 7:32
1 Answer
Reset to default 4Replace your javascript code by following:
<script>
$(document).ready(function() {
$("#delete").click(function(){
var answer = confirm("Are you sure you want to delete?");
if (answer){
document.getElementById('confirm_delete').value = 1;
return true;
} else {
document.getElementById('confirm_delete').value = 0;
return false;
}
});
});
</script>
You are using the return true and false
first after that you are assigning the value. so return terminates from the function before assign the value. so first assign the value and then use the return.