-1

I am trying to submit a form using jQuery Ajax in PHP Codeigniter. But it seems it doesn't work. What I did wrong...

Here is the HTML Markup.

<form action="" method="post" enctype="multipart/form-data" class="form-horizontal">
    <!-- other form inputs ommitted -->
    <div class="col-md-3">
        <div class="box box-danger box-solid">
            <div class="box-header"> <label>User Photo </label> </div>
            <div class="box-body box-profile">
                <center>
                    <img id="user_photo_change" class="img-responsive" src="//placehold.it/400x400" alt="Profile Picture" style="max-width: 120px;">
                    <br>
                    <input type="file" name="photo" onchange="readPicture(this);">
                </center>
            </div>
        </div>
    </div>
    <div class="col-md-12">
        <center>
            <button type="reset" class="btn btn-sm bg-red">Reset</button>
            <button type="submit" id="submit" class="btn btn-sm bg-green">Save</button>
        </center>
    </div>
</form>

And Here Is the Ajax Call

<script>
    $(function() {

        $('form').on('submit', function (e) {
          e.preventDefault();

          $.ajax({
            type: 'post',
            url: '<?php echo base_url('admin/save'); ?>',
            data: $('form').serialize(),
            success: function (data) {

                $('#message').html(data);
            }
          });

        });

    });
</script>
Muhammad Aftab
  • 1,098
  • 8
  • 19
  • 1
    Possible duplicate of [Prevent Default on Form Submit jQuery](https://stackoverflow.com/questions/6462143/prevent-default-on-form-submit-jquery) – Don't Panic Oct 29 '19 at 07:06
  • *But it seems it doesn't work*: what is not working? What is your controller/model code? – Vickel Oct 29 '19 at 17:18

2 Answers2

0

you can change the submit button to a normal button. and listen it's click. hope help you.

PeakFish
  • 45
  • 6
0

Try:

$('body').on('submit','form',function(e) {
e.preventDefault();
$.ajax({ type: 'post',
 url: '',
 data: $('form').serialize(),
 success: function (data) {
   $('#message').html(data); 
   alert(data);
 }
});
});
Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30