Gilbert
Gilbert

Reputation: 64

Form_Open always submits - Submitting Via Ajax - CodeIgniter

I want to submit my form via Ajax. I have done it before but for some reason whenever I click submit it will always submit the form and refresh the page. I made a test function just to console log once submit button is pressed but that doesn't even work.

Once I can get the console log working without page refreshing then I can do the Ajax myself since I know how.

Here is my form (keep in mind Im only going to post one input here just for an example).

function submit_form(e) {
    e.preventDefault();
    console.log('working');
    return false;
};


   {!! form_open('myaccount/product_coupon/add',['id' => 'product_coupon_form', 'onsubmit'=> 'submit_form']) !!}

 Username <input type="text" name="username" class="form-control">

 <input type="submit" class="btn btn-primary" id="submit">

 </form>

I have also tried in javascript calling it by $('#submit').click(function()... but that still did nothing

Upvotes: 1

Views: 297

Answers (1)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

1st: be sure you include jquery

2nd: wrap your code in $(document).ready(function(){ //code here })

3rd: you can use form submit instead of submit button click

$(document).ready(function(){
     $('#product_coupon_form').on('submit' , function(e){
          submit_form(e);
     })
});

Upvotes: 1

Related Questions