简体   繁体   中英

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>

$("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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM