Codeigniter insert to database via ajax from model
<p>I currently have a page with a single form element. Upon entering a value into the text box, a modal window pops up showing the value you have entered.Upon clicking confirm the value is then entered into the database. This is working fine but I'm looking to have another modal window popup when the insert is successful but I am struggling with it. </p>

<p>I'm thinking of going down the route of submitting the form via ajax and then showing the confirmation modal but I can't seem to get it working. I'm not sure if its the fact that I'm using two modal windows or what it is really.</p>

<pre><code>&lt;form action="&lt;?php echo base_url()."index.php/create/createHospital"; ?&gt;" method = "post" name = "form1" id ="form1" &gt;
    &lt;div class="hospital_container"&gt;
        &lt;h3 class = "hospital_header"&gt;&lt;b&gt;Enter Hospital Information&lt;/b&gt;&lt;/h3&gt;

    &lt;label for="hospitalName" class = "labelForm"&gt;Name:&lt;/label&gt;
    &lt;input type="text" class = "input2" id="hospitalName" name="hospitalName" class = "newUserForm"&gt;

        &lt;button type="button"  id = "hospital_submit_button" class = "hospital_submit_button" data-toggle="modal" data-target="#userModal"&gt;Add New Hospital&lt;/button&gt;

    &lt;/div&gt;
&lt;/form&gt;

&lt;!--   MODAL   --&gt;

&lt;div id="userModal" class="modal fade" role="dialog"&gt;
    &lt;h3 class="modal-title_user"&gt;Create New Hospital?&lt;/h3&gt;
    &lt;div class="modal-body"&gt;

            &lt;h4 id = "h4modal_user_hosp"&gt; Hospital Information&lt;/h4&gt;
            &lt;div id ="modal_hosp_container"&gt;
                &lt;br&gt; &lt;label  class = "modallabeluser"&gt; Hospital:&lt;/label&gt; &lt;p class = "modaluser"  id = "hospital1" name = "hospital1"&gt;&lt;/p&gt;
            &lt;/div&gt;

        &lt;button type="submit"  id = "overlaysubclass2"&gt; Yes,Complete&lt;/button&gt;
        &lt;button id = "editoverlay" data-dismiss="modal"&gt; No,Edit Info&lt;/button&gt;

    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>When confirming the value in the modal window I am using:</p>

<pre><code> $('#hospital_submit_button').click(function() {

            var hospitalName = $('#hospitalName').val();

            $('#hospital1').text(hospitalName);



        });

    $('#overlaysubclass2').click(function(){
        document.form1.submit();

    });
</code></pre>

<p>This was the ajax call I have at the moment but I'm unsure how to tie it in with the rest of the code?</p>

<pre><code>$(function() {
   $("#form1").on("submit", function(event) {
       event.preventDefault();
       $.ajax({
           url: "&lt;?php echo base_url()."index.php/create/createHospital"; ?&gt;",
           type: "post",
           data: $(this).serialize(),
           success: function(d) {
               alert(d);
           }
       });
   });
</code></pre>

<p>});</p>

<p>Alternatively, does anyone have an idea of how to achieve the modal another way?
Thanks in advance</p>

<p>Model</p>

<pre><code>    function create_hospital($data){ 

        $theData = array(
            'hospitalName' =&gt; $this-&gt;input-&gt;post('hospitalName'),

        );

        $insert = $this-&gt;db-&gt;insert('hospitals', $theData);
        return $insert;

    }
</code></pre>

<p>Controller</p>

<pre><code>  function createHospital()
{


      //  $hospital=$this-&gt;input-&gt;post('hospitalName');
      //  echo $hospital;

        $theData = array(

            'hospitalName' =&gt; $this-&gt;input-&gt;post('hospitalName'),
        );

        $this-&gt;user_model-&gt;create_hospital($theData);
        //$data['message'] = 'Data Inserted Successfully';

      redirect('Admin/add_hospitals');
    }
</code></pre>

<p>MODAL I WANT TO POPUP ON SUCCESSFULL INSERT TO DB </p>

<p></p>

<pre><code>    &lt;h3 class="modal-title_user"&gt;Confirm?&lt;/h3&gt;
    &lt;div class="modal-body"&gt;

            &lt;h4 id = "h4modal_user_hosp"&gt; Hospital Information&lt;/h4&gt;
            &lt;div id ="modal_hosp_container"&gt;

              &lt;p&gt; confirmed&lt;/p&gt;

            &lt;/div&gt;


            &lt;input type = "submit" name="submit" value = "Yes,Confirm" id = "confirmbutton"&gt;
            &lt;input type = "submit" name="submit" value = "No,Edit Info" id = "editoverlay" data-dismiss="modal"&gt;

    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>Would it not be a case of using:</p>

<pre><code> success: function(data) {
                       // alert("success");
                        $("#confirmModal").modal("show");

                    }
</code></pre>