Ari Patwary
Ari Patwary

Reputation: 80

AJAX POST method isn't working in android

My form should post with AJAX and every other device I have is able to post besides android. I've tried changing the method to get. I even have a conditional to find if the device is an android but i'd like to be able to offer the AJAX to android users. If i need to offer more information i'm more than happy too if anyone has dealt with anything similar.

html:

      <form method='post' action='insert.php' id="requestForm">
           <input class="ifields" type="text" id="song_name" name="song_title" placeholder="Song">
           <input class="ifields" type="text" id="song_author" name="song_artist" placeholder="Artist">
           <input class="ifields" type="text" id="song_singer" name="users_name" placeholder="Your Name">
           <br><br><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success isubmit">Submit</button>
      </form>

Jquery:

                 $(document).on('click', '#btn_add', function(e){  
                      var song_name = $('#song_name').val();  
                      var artist_name = $('#song_author').val();  
                       var user_input_name = $('#song_singer').val();
                      if(song_name == '')  
                      {  
                           alert("Enter song Name");  
                           return false;  
                      }  
                      if(artist_name == '')  
                      {  
                           alert("Enter artist Name");  
                           return false;  
                      } 
                      if(user_input_name == '')  
                      {  
                           alert("Enter your name Name");  
                           return false;  
                      }   
                     var ua = navigator.userAgent.toLowerCase();
                     var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
                     if(isAndroid) {
                          $.ajax({  
                               url:"insert_admin.php",  
                               method:"POST",  
                               data:{song_name:song_name, artist_name:artist_name, user_input_name:user_input_name},  
                               dataType:"text",  
                               success:function(data)  
                               {  
                                    alert("your song was added");  
                                    fetch_data();
                                    $("form")[0].reset();  
                               }  
                          })  
                     }
                     else{
                          e.preventDefault();
                          $.ajax({  
                               url:"insert_admin.php",  
                               method:"POST",  
                               data:{song_name:song_name, artist_name:artist_name, user_input_name:user_input_name},  
                               dataType:"text",  
                               success:function(data)  
                               {  
                                    alert("your song was added");  
                                    fetch_data();
                                    $("form")[0].reset();  
                               }  
                          })  
                     }   
                 }); 

Upvotes: 0

Views: 430

Answers (1)

Mohammad C
Mohammad C

Reputation: 1341

$("btn_add").click(function(e){  
    var song_name = $('#song_name').val();  
    var artist_name = $('#song_author').val();  
    var user_input_name = $('#song_singer').val();
    if(song_name == '')  
    {  
        alert("Enter song Name");  
        return false;  
    }  
    if(artist_name == '')  
    {  
        alert("Enter artist Name");  
        return false;  
    } 
    if(user_input_name == '')  
    {  
        alert("Enter your name Name");  
        return false;  
    }   
    $.ajax({  
        url:"insert_admin.php",  
        method:"POST",  
        data:{"song_name":song_name, "artist_name":artist_name, "user_input_name":user_input_name},  
        dataType:"text",  
        success:function(data)  
        {  
            alert("your song was added");  
            fetch_data();
            $("#requestForm").reset();  
        }  
    });
}); 

Hey, Man. try this, let me know if it works. This should hopefully create a click event listener on the button only. Try on pc first and check console log for any errors. If there are no errors. Then proceed to trying it on android phone. Fingers crossed, there are no issues.

Upvotes: 1

Related Questions