0

Ajax form submission not happening.
view:

<form method="post" name="addwishlists" id="addwishlists" enctype="multipart/form-data" >
 <input type="hidden" name="deal_id" value="<?php echo $deal_id; ?>" >
<input type="hidden" name="cust_id" value="<?php echo $cust_id; ?>">

                                    <input type="submit" value="Add Wishlist" id="addwishlist" class="btn btn-primary" />
                                    </form>

My code is:

<script>   //no need to specify the language
$(document).ready(function(){
  $("#addwishlist").click(function(e){  // passing down the event 

    $.ajax({
       url:'<?php echo base_url();?>add-wishlist',
       type: 'POST',
       data: $("#addwishlists").serialize(),
       success: function(){
           alert("wishlist added");
           $('#deal_id').val('');
           $('#cust_id').val('');
       },
       error: function(){
           alert("Fail")
       }
   });
   e.preventDefault(); // could also use: return false;
   //return false;
 });
});
</script>

Edited by praba_web: mistake

2
Contributors
1
Reply
24
Views
1 Year
Discussion Span
Last Post by gabrielcastillo
1

Do you get any errors in you console.log? I usually add my e.preventDefault() at the top of my function, before the ajax.

try:

$('#addwishlist').on('click', function(e){
    e.preventDefault();

    var test = 'Test_post';

    $.ajax({

        url: '/addwishlist/test_ajax',
        type: 'POST',
        data: {post: test},
        dataType: 'JSON',
        cache: false,
        success: function(json){
            alert(json.message);
            console.log(json.post);
        }

    });

});

In your controller add this

public function test_ajax()
{

    $data = array('message' => 'Ajax Form Success', 'post' => $this->input->post(NULL, TRUE));


    $this->output->set_content_type('application/json')->set_output(json_encode($data));
}

You are sending your form data to a test function in you controller and then outputing a JSON string, and catching it in your success callback function.

Edited by gabrielcastillo: rename variable

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.