Scott O'connor
Scott O'connor

Reputation: 133

When using ajax POST for form in codeigniter, not passing through data

I am trying to post data through my website via ajax, however for some reason it seems to echo the success message but does not get the mobile field data. it's just blank. When i inspect with firebug, it gives the response, but the POST tab is empty.

My controller contains the following :

function submit()
 {
     //set validation rule

         // get post data
         $emailid = $this->input->post('mobile');

         // write your database insert code here


         echo "<div class='alert'>Thanks for Subscribing! Please stay tuned to get awesome tips...</div> here is $emailid";

 }

And my view contains :

<label>Mobile</label>

<input type="number" name="mobile" id="mobile" class=" form-control"  />

<button type ="submit" id="submit" name="submit"  class="btn btn-info btn-block" /> NEXT </button>

The JS

                <script type="text/javascript">
                     $("#submit").click(function(e) {
                         e.preventDefault();
                         var mobile = $("mobile").val();
                         $.ajax({
                             url: "https://cheddarplatform.com/complete/submit",
                             method: "POST",
                             data: {mobile: mobile},
                             success: function(data) {
                                 $("#message").html(data);
                             },
                             error: function() {
                                 alert("Please enter valid email id!");
                             }
                         });
                     });
                     </script>

Upvotes: 0

Views: 93

Answers (1)

Nico Haase
Nico Haase

Reputation: 12095

$("mobile") is not a proper selector, as you do not have a HTML element of that type anywhere. Probably you want to use $("#mobile") and have a look at your browser's network tab to find this error easier the next time

Upvotes: 1

Related Questions