无法从ajax获取数据到php页面

我设计了一个简单的页面,用户可以使用 html 输入名称、密码和图像。我尝试使用 ajax 将这些数据发送到特定的 php 页面,但我无法实现这一点。我是怎么做这件事的


html代码


<?php include('connection.php'); ?>         

    <form id="form" enctype="multipart/form-data">

        <table>

            <tr>

                <td>Name:</td>

                <td><input type="name" id="name"></td>

            </tr>

            <tr>

                <td>Password:</td>

                <td><input type="password" id="pass"></td>

            </tr>

            <tr>

                <td>Photos:</td>

                <td><input type="file" id="photos"></td>

            </tr>

            <tr>

                <td><input type="submit" id="go"></td>

            </tr>

        </table>

    </form>

jQuery 和 ajax


<script>

    $(document).ready(function(){

        $('#go').click(function(){      

            var img_name = $('#img_name').val();

            var name = $('#name').val();

            var pass = $('#pass').val();  

            $.ajax({   

                type : "POST",

                url : "singup_submit.php",

                data : { img_name:img_name, name:name, pass:pass},

                success : function(done){

                    alert(done);

                }    

            });    

        });

    });

</script>

php代码


<?php

    include('connection.php');    

    if(isset($_POST)){   

        $name = $_POST['name'];

        $pass = $_POST['pass'];

        $img_name=$_FILES['img_name']['name'];


        $qr="INSERT INTO data (name,pass,img_name) VALUES ('$name','$pass','$img_name')";

        $ex=mysqli_query($con,$qr) or die(mysqli_error($con));    

        if($ex==1)

            echo 'done';

        else            

            echo 'not done';

}


慕田峪4524236
浏览 120回答 2
2回答

慕尼黑的夜晚无繁华

按照此代码..它可能会帮助你<script>&nbsp; &nbsp; $(document).ready(function(){&nbsp; &nbsp; &nbsp; &nbsp; $("#go").click(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var name&nbsp; &nbsp; = $("#name").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var pasword = $("#password").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var photos&nbsp; = $("#photos").val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(name==''||pass==''||photos==''){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Please Fill All Fields");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type&nbsp; &nbsp;: "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url&nbsp; &nbsp; : "singup_submit.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data&nbsp; &nbsp;: formData,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache&nbsp; : false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(result){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; });&nbsp; </script>

慕姐8265434

您没有在 ajax 请求中发送任何文件。$(document).ready(function(){$("#go").on('submit',function(e){&nbsp; &nbsp;e.preventDefault()&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: 'singup_submit.php',&nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; data: new FormData(this),&nbsp; &nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; &nbsp; success: function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(done);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("error");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; });&nbsp; });然后像你现在所做的那样从 php 中的全局变量中获取数据。并请像这样为您的表单字段指定名称&nbsp;<form id="form" enctype="multipart/form-data">&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Name:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="name" name="name" id="name"></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Password:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="password" name="password" id="pass"></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Photos:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="file" name="img" id="photos"></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="submit" name="submit" id="go"></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table></form>它应该工作。
打开App,查看更多内容
随时随地看视频慕课网APP