jQuery AJAX form data serialize using PHP

前端 未结 7 1462
轻奢々
轻奢々 2020-12-05 08:07

I am stuck in my code, I need to send data from the form to the check.php page and then process it.

This is my code:

The AJAX part:



        
相关标签:
7条回答
  • 2020-12-05 08:49

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        var form=$("#myForm");
        $("#smt").click(function(){
            $.ajax({
                type:"POST",
                url:form.attr("action"),
                data:form.serialize(),
                success: function(response){
                    console.log(response);  
                }
            });
        });
    });
    </script>

    This is perfect code , there is no problem.. You have to check that in php script.

    0 讨论(0)
  • 2020-12-05 08:56

    Change your code as follows -

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    var form=$("#myForm");
    $("#smt").click(function(){
    $.ajax({
            type:"POST",
            url:form.attr("action"),
            data:form.serialize(),
    
            success: function(response){
            if(response == 1){
                  $("#err").html("Hi Tony");//updated
            }  else {
                $("#err").html("I dont know you.");//updated
            }
            }
        });
    });
    });
    </script>
    

    PHP -

    <?php
    $user=$_POST['user'];
    $pass=$_POST['pass'];
    
    if($user=="tony")
    {
        echo 1;   
    }
    else
    {
        echo 0;    
    }
    ?>
    
    0 讨论(0)
  • 2020-12-05 09:05

    I just had the same problem: You have to unserialize the data on the php side.

    Add to the beginning of your php file (Attention this short version would replace all other post variables):

    parse_str($_POST["data"], $_POST);
    
    0 讨论(0)
  • Your problem is in your php file. When you use jquery serialize() method you are sending a string, so you can not treat it like an array. Make a var_dump($_post) and you will see what I am talking about.

    0 讨论(0)
  • 2020-12-05 09:07
    Try this its working..
    
        <script>
          $(function () {
              $('form').on('submit', function (e) {
                  e.preventDefault();
                  $.ajax({
                      type: 'post',
                      url: '<?php echo base_url();?>student_ajax/insert',
                      data: $('form').serialize(),
                      success: function (response) {
                          alert('form was submitted');
                      }
                      error:function() {
                          alert('fail');
                      }
                  });
              });
          });
        </script>
    
    0 讨论(0)
  • 2020-12-05 09:09

    Try this

     $(document).ready(function(){
        var form=$("#myForm");
        $("#smt").click(function(){
        $.ajax({
                type:"POST",
                url:form.attr("action"),
                data:$("#myForm input").serialize(),//only input
                success: function(response){
                    console.log(response);  
                }
            });
        });
        });
    
    0 讨论(0)
提交回复
热议问题