为什么表单数据不通过?

时间:2017-11-10 11:37:19

标签: javascript php ajax

我试图在php中为上传图片制作文档,但文档getiamge.php不接受表单数据并发出警告。

的index.php

<html>
<head>
<meta charset="utf-8">
    <title>Image Upload</title>
    <script>

        function Insert() 
    {
        var image= document.getElementById("image").value;

        var name= document.getElementById("name").value;





    if (image == ""  && name == "") 
    {
        document.getElementById("message").innerHTML="Please Fill The Fields";
        return;
    }
        else if (image == "" || name == "") 
            {
                document.getElementById("message").innerHTML="Please Fill The Form Correctly.";
                return;
            }
        else
        { 
        if (window.XMLHttpRequest)
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else 
        {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() 
        {
            if (this.readyState == 4 && this.status == 200) 
            {
                document.getElementById("message").innerHTML = this.responseText;
            }
        };


        xmlhttp.open("POST","getimage.php?name="+name+"&image="+image,true);
        xmlhttp.setRequestHeader("Content-type", "multipart/form-data");    
        xmlhttp.send();
        }
    }



    </script>
</head>

<body>
<form enctype="multipart/form-data">
    <input type="file" id="image">
    <input type="text" id="name">
    <input type="button" value="Upload" onClick="Insert()">
    <h6 id="message">yyys</h6>
    </form>

</body>
</html>

getimage.php

<?php
include("2nd.php");

$a=$_POST['image'];
$name=$_POST['name'];

$path="images/".basename($_FILES['image']['name']); 

$image=base64_encode($a);
$query="INSERT INTO images(image,name) VALUES ('".$image."','".$name."')";

$insert=mysqli_query($conn,$query);

if($insert)
{
  move_uploaded_file($_FILES['image']['name']);
    if(move_uploaded_file($_FILES['image']['name']))
    {

    echo "Uploaded Successfully";
    }
    else
    {
        echo "Not Uploaded successfully";
    }
}
else
{
    echo "Not Inserted";
}
?>

警告就是这些

警告:第0行的Unknown中的multipart / form-data POST数据中缺少边界

注意:未定义的索引:第4行的D:\ xampp \ htdocs \ image \ getimage.php中的图像

注意:未定义的索引:第5行的D:\ xampp \ htdocs \ image \ getimage.php中的名称

注意:未定义的索引:第7行的D:\ xampp \ htdocs \ image \ getimage.php中的图像

注意:未定义的索引:第16行的D:\ xampp \ htdocs \ image \ getimage.php中的图像

注意:未定义的索引:第17行的D:\ xampp \ htdocs \ image \ getimage.php中的图像 未成功上传

1 个答案:

答案 0 :(得分:0)

Notice: Undefined index: image错误即将发生,因为您要在POST数据中发送图片,因此它会$_POST['image']进入$_FILES['image']['name'],您必须使用FormData(),如下所示:

&#39;

    function Insert() 
{
    var image= document.getElementById("image").files[0];

    var name= document.getElementById("name").value;

if (image == ""  && name == "") 
{
    document.getElementById("message").innerHTML="Please Fill The Fields";
    return;
}
    else if (image == "" || name == "") 
        {
            document.getElementById("message").innerHTML="Please Fill The Form Correctly.";
            return;
        }
    else
    { 
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else 
    {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() 
    {
        if (this.readyState == 4 && this.status == 200) 
        {
            document.getElementById("message").innerHTML = this.responseText;
        }
    };


     xmlhttp.open("POST","getimage.php",true);
     xmlhttp.setRequestHeader("Content-type", "multipart/form-data");  
     var formData = new FormData();
     formData.append("image", image);
     formData.append("name", name);
     xmlhttp.send(formData);
    }
}
</script>'