Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

javascript - Confirmation Message not working Using Ajax Codeigniter

I am fetching data from database using ajax in codeigniter and now i am trying to put "delete confirmation box" in controller but that button is not working,

Here is controller file/code

$user['result']=$this->crud->AddMember($data);
foreach($user['result'] as $row) {
    echo "<td>";
    <input type="hidden" name="id" value="'.$id.'"> 
    <input type="submit" class="btn btn-danger btn-sm" value="Delete" name="delete" onclick="return confirm('Are you sure you want to delete this item')">
    </form>';
    echo "</td>";
}

Here is view file

<form method="post" name="myForm" class="form-horizontal" id="user_form" enctype="multipart/form-data">
    <input type="text" class="form-control" id="FlatNumber" name="FlatNumber" placeholder="">
    <button type="submit" class="btn btn-primary btn-lg" id="butsave">Add Details</button>
</form>

<script>
$(document).ready(function() {
    $('#butsave').on('click', function() {

        event.preventDefault();  
        var FlatNumber = $('#FlatNumber').val();
        if(FlatNumber == '') {  
            alert("Please enter Flat Number");
        }else{
            $.ajax({
                    url:"<?php echo base_url() . 'index.php/Member/AddRecord'?>",
                    type: "POST",
                    data: {FlatNumber : FlatNumber},
                    dataType: "html",
                    success: function(msg){
                        alert(msg);
                        if (msg == 'exist') {
                            $("#successs").hide();
                        }else{
                            $("#errorr").hide();
                        }
                    }
            });
        }  
    }); 
});
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is because onclick works only for elements who already on this page. after ajax response it will not work so you need to use code like this.

$(document).on("click",".btn-danger", function(){
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...