Note that there are some explanatory texts on larger screens.

plurals
  1. POPost to CI Controller using JQueryAjax and Modal Bootstrap
    primarykey
    data
    text
    <p>I am using Bootstrap Modal to take the user's input and submit it to a CI Controller for validation. I want to be able to return the results to the same modal (JSON) before the input is save to a database table. So far from the code, Firebug is telling me I have a TypeError : result is null. I'd like to get it to work. I am new to JQuery/Ajax Thank you for your help.</p> <p><strong>Script</strong></p> <pre><code>$(document).ready(function() { $('#createCardId').click('click', function(e) { e.preventDefault(); //we'll want to move to page specific files later var email = $('input#email').val(); //from hidden field var location = $('input#location').val(); //from hidden field var card = $('#cardId').val(); //user's input $.ajax({ url : "../edituser/addCardId", dataType : "json", type : 'POST', data : "email=" + email + "&amp;location=" + location + "&amp;CardId=" + card, success : function(result) { if (result.error) { $(".alert").fadeIn('slow'); $("#error_message").html(result.message); } else { $(".alert").fadeIn('slow'); $("#error_message").html(result.message); $('#new_item').modal('hide'); } }, }); }); }); </code></pre> <p><strong>View-Modal Bootstrap</strong></p> <pre><code> &lt;div id="myModal" class="modal hide fade modal-admin" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt; &lt;div class="modal-header"&gt; &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;×&lt;/button&gt; &lt;h3 id="myModalLabel"&gt;Add Card ID&lt;/h3&gt; &lt;/div&gt; &lt;div class="modal-body"&gt; &lt;!--############--&gt; &lt;!-- column left --&gt; &lt;div class="span6"&gt; &lt;p&gt; &lt;?php $location = $this-&gt;session-&gt;userdata('location'); $email = $account-&gt;email; echo $location. '&lt;br&gt;'; echo $email; if(validation_errors()) { echo '&lt;div class="alert alert-error"&gt;'.validation_errors().'&lt;/div&gt;'; } ?&gt; &lt;!--CARD ID --&gt; &lt;div id="error_message"&gt;&lt;/div&gt; &lt;div class="control-group"&gt; &lt;input id="email" type="hidden" name="email" value="&lt;?php echo $email; ?&gt;"&gt; &lt;input id="location" type="hidden" name="location" value="&lt;?php echo $location; ?&gt;"&gt; &lt;label class="control-label"&gt;Card ID: &lt;span style="color: red;"&gt;*&lt;/span&gt;&lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="text" id="cardId" name="cardId" value=""/&gt; &lt;/div&gt; &lt;/div&gt; &lt;!--END OF CARD ID--&gt; &lt;/p&gt; &lt;/div&gt; &lt;!--############ column left ends--&gt; &lt;!--############--&gt; &lt;!--column right--&gt; &lt;div class="span6"&gt; &lt;p&gt;Hello &lt;?php echo $location; ?&gt;&lt;/p&gt; &lt;/div&gt; &lt;!--############ column right ends--&gt; &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button class="btn" data-dismiss="modal" aria-hidden="true"&gt;Cancel&lt;/button&gt; &lt;button class="btn btn-primary" id="createCardId" "data-dismiss=modal"&gt;Save changes&lt;/button&gt; &lt;!--&lt;input name="submit" type="submit" id="submit" value="Save Changes" class="btn btn-primary"&gt;--&gt; &lt;/div&gt; &lt;!--############--&gt; &lt;/div&gt; </code></pre> <p><strong>CI Controler</strong></p> <pre><code>public function addCardId() { $this-&gt;form_validation-&gt;set_rules('CardId', 'Card Id', 'required|trim|xss_clean|max_length[6]|is_unique[accounts.CardId]'); $email = $this-&gt;input-&gt;post('email'); $location = $this-&gt;input-&gt;post('location'); $result = array(); if ($this-&gt;input-&gt;is_ajax_request()) { if ($this-&gt;form_validation-&gt;run() == FALSE) { $result['error'] = true; $result['message'] = validation_errors(); } else { $result['error'] = TRUE; $result['message'] = 'The Card ID has been saved'; //Model will load here to add Card ID to the database. redirect('admin/editaccount/search_account', 'refresh'); } $json = json_encode($result); die($json); } else { redirect('../searchaccount/showresults', 'refresh'); } } </code></pre> <p><strong>Thank you for your help!!</strong> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload