CodeIgniter and AJAX form submit

Pinterest LinkedIn Tumblr

Html part in view

<form id="comment" method="post">
    <h2>Enter Your Details</h2>
    <center><div id="result"></div></center>

    <div class="form_fld">
        <input type="text" placeholder="Enter Your Full Name" name="name" required=""> 
    <div class="form_fld">
        <label>Email ID</label>
        <input type="text" placeholder="Enter Email ID" name="email" required="">
    <div class="form_fld">
        <label>Contact Number</label>
        <input type="text" placeholder="Enter Contact Number" name="contact" required="">
    <div class="form_fld">
        <select name="developer">
    <div class="form_fld">
        <button type="submit" id="send">Submit</button>

After Html Part Just put ajax request

<script type="text/javascript" src="<?php echo base_url('assets/'); ?>js/jquery.js"></script>
        dataString = $("#comment").serialize();

            type: "POST",
            url: "home/contact",
            data: dataString,
            success: function(data){
                // alert('Successful!');
                $("#result").html('Successfully updated record!'); 
                $("#result").addClass("alert alert-success");


        return false;  //stop the actual form post !important!

Within Controller

public function contact()
    $ip = $_SERVER['REMOTE_ADDR'];
    $data = array('name' => $this->input->post('name'),
                  'email' => $this->input->post('email'),
                  'number' => $this->input->post('contact'),
                  'developer' => $this->input->post('developer'),
                  'ip' => $ip,
                  'date' =>  date("d/m/Y"));
    $result = $this->User_model->contact($data);

Write A Comment