$_POST is not working in ajax form submit?

后端 未结 11 1564
遥遥无期
遥遥无期 2021-01-17 12:57

When I try to check user input name is already exist by ajax form submit !But it only get Undefined index: username in sessions.php ,what is missin

相关标签:
11条回答
  • 2021-01-17 13:48

    Your coding is correct.Remove processData and contentType from Ajax it will work

    processData : false,
    contentType : false,  
    
    0 讨论(0)
  • 2021-01-17 13:50

    You are setting contentType to false, that is why PHP can not parse your post body

    0 讨论(0)
  • 2021-01-17 13:50

    You need to change your script:

    Try using new FormData instead of .serialize().

    <script type="text/javascript">
        $('#confirm').click(function(e){
            e.preventDefault();
            var formData = new FormData($("#saveuser")[0]);
          $.ajax({
                type:'POST',
                url :"tt.php",
                data:formData,
                contentType : false,
                processData: false,            
                success: function(d){
                    console.log(d);//[error] :Undefined index: username 
                }
            });
        });
    </script>
    

    Note : You are used contentType to false that mean jQuery not to add a Content-Type header. You are using jQuery's .serialize() method which creates a text string in standard URL-encoded notation. You need to pass un-encoded data when using "contentType: false".

    0 讨论(0)
  • 2021-01-17 13:56

    Remove that method,action: post and blank from your form tag as you need to give all details in ajax method only.

    or you can delete the form tag itself as ajax method will take care of the post call.

    this will solve hopefully

        <form id="saveuser" enctype="multipart/form-data">
    
    0 讨论(0)
  • 2021-01-17 13:56

    Use the following code in your html code and remove contentType : false, processData: false

    <form action="" method="POST" id="saveuser" enctype="multipart/form-data">
        <input type="text" name="username"><br>
        <input type="password" name="pass"><br>
        <input type="file" name="fileupload"><br>
        <input type="submit" name="submit" value="Confirm" id="confirm">
    </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
    <script type="text/javascript">
        $('#confirm').click(function(e){
            e.preventDefault();
            $.ajax({
                type:'POST',
                url :"sessions.php",
                data: $('#saveuser').serialize(),
                success: function(d){
                    console.log(d);//[error] :Undefined index: username
                }
            });
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题