最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Click button. Javascript alert box. If click OK, page reloads and need to create php variable with html input form value - Stack

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

Replace 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论