Php 提交图像时出现Ajax表单错误

Php 提交图像时出现Ajax表单错误,php,ajax,ajaxform,forms,http-post,Php,Ajax,Ajaxform,Forms,Http Post,当表单在同一页面上提交并显示更改时,我很难尝试进行AJAX编辑更改,但是图像抛出错误:Undefined index:image in update.php,第22行和第24行。它拒绝传递值 表单(editForm.php): <div class="modal-content editDisplay"> <div class="modal-header"> <button type="button" class="close" data-dismiss="m

当表单在同一页面上提交并显示更改时,我很难尝试进行AJAX编辑更改,但是图像抛出错误:
Undefined index:image in update.php,第22行和第24行
。它拒绝传递值

表单(editForm.php):

<div class="modal-content editDisplay">
 <div class="modal-header">
  <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
  <h4 class="modal-title" id="editModalLabel">Edit Item</h4>
</div>
<form class="editForm" method="post" enctype="multipart/form-data">
  <div class="modal-body">
        <div class="form-group">
            <label for="inputName">Name</label>
            <input type="text" class="form-control" id="inputName" name="Product_Name" placeholder="Name" value="<?php echo $product ?>">
            <input type="hidden" name="oldProduct" value="<?php echo $oldProduct ?>">
        </div>
        <div class="form-group">
            <label for="inputDescription">Description</label>
            <textarea class="form-control" id="inputDescription" name="Description" placeholder="Description"><?php echo $description ?></textarea>
        </div>
        <div class="form-group">
            <label for="inputPrice">Price</label>
            <input type="text" class="form-control" id="inputPrice" name="Price" placeholder="Price" value="<?php echo $price ?>">
        </div>
        <div class="form-group">
            <label for="inputQuantity">Quantity</label>
            <input type="number" class="form-control" id="inputQuantity" name="Quantity" placeholder="Quantity" value="<?php echo $quantity ?>">
        </div>
        <div class="form-group">
            <label for="inputSalePrice">Sale Price</label>
            <input type="text" class="form-control" id="inputSalePrice" name="Sale_Price" placeholder="Sale Price" value="<?php echo $salePrice ?>">
        </div>
        <div class="form-group">
            <label for="inputImage">Image Upload</label><br>
            <fieldset class="file-fieldset">
                <span class="btn btn-default btn-file">
                    <span class="glyphicon glyphicon-upload"></span>&nbsp;&nbsp;Browse Browse <input name="image" type="file" id="inputImage"/><br>
                </span>
                <input type="hidden" name="prevPicture" value="<?php $image ?>"/>
                <span style="margin-left:8px;" value=""><?php echo $image ?></span>

            </fieldset>
        </div>
  </div>
  <div class="modal-footer">
    <button type="reset" class="btn btn-default">Reset</button>
    <button type="submit" class="btn btn-primary" id="saveButton" name="update">Save Changes</button>
  </div>
 </form>
</div>
<?php

include('connection.php');
include('LIB_project1.php');

$productName = $_POST['Product_Name'];
$productDescription = $_POST['Description'];
$price = $_POST['Price'];
$quantity = $_POST['Quantity'];
$salePrice = $_POST['Sale_Price'];
$oldImage = $_POST['prevPicture'];
$oldProduct = $_POST['oldProduct'];


//$productName = 'Jaime';
//$productDescription = 'This is crazy';
//$price = '0';
//$quantity = '12234';
//$salePrice = '0';
//$oldImage = $_POST['prevPicture'];
//$oldProduct = $_POST['oldProduct'];
$imageName= $_FILES['image']['name']; //TODO line 22
echo ' The image is '.$imageName;
$image_temp = $_FILES['image']['tmp_name']; // line 24
echo 'Product name is: '.$productName;

//$productName = 'Dodo Square';
//$productDescription = 'Flower on a Bee. Such Beauty!';
//$price = 9;
//$quantity = 8;
//$salePrice = 230;
//$newImage = '038.jpg';
//$oldProduct = 'Times Square';

//working under the assumption that the image already exist in the database
$targetDirectory = 'productImages'; 
$files = scandir($targetDirectory,1);
//code passed
for($i=0; $i<sizeof($files); $i++)
{
    if($oldImage==$files[$i])
    {
        unlink('productImages/'.$oldImage);
    }
}

$target = "productImages";

//add the image to the directory
$target = $target.'/'.$imageName;
move_uploaded_file($image_temp,$target);  

updateProduct($conn,'product',':productName', ':productDescription', ':price', ':quantity', ':imageName', ':salePrice',                          'Product_Name', 'Description', 'Price', 'Quantity', 'Image_Name', 'Sale_Price', $productName, $productDescription, $price, $quantity,$imageName, $salePrice, $oldProduct, ':oldProduct');
//header('location:admin.php');

?>
AJAX(显示编辑的更改)问题:需要提交这些编辑的更改

 $(document).ready(function() 
 {
           //the user click save edit
   $(".edit").on("submit",function(e)
   { 
       e.preventDefault();
       $.ajax({
            type:"POST",
            url:'update.php', //I will put project id here as well
            data:$(".editForm").serialize(),
            success:function(smsg)
            {
                alert(smsg);
               //update the number of items the user has in their shopping cart
                $.get('admin.php',function(data){
                $('#refresh').load("admin.php #refresh");
                    //alert('success');
                });
            }
        });


   });


   });

Change
将一系列测试添加到PHP中,您将很快找到答案

您已经在提醒PHP ajax处理器文件发送的响应:

alert(smsg);
因此,用它来故障排除/诊断哪里出了问题

第一个测试可以是在PHP文件的顶部放置一条“I got here”消息——至少这样您就知道ajax本身正在工作。因此,将
update.php
顶部修改为:

<?php
echo "Got to here";
die();
我相信你会很快发现错误——肯定比写一个详细的SO问题并等待答案要快

祝你好运

var inputImage = $("#inputImage");
var fd = new FormData(document.getElementById("editform")); 

fd.append("image", inputImage);

$.ajax({
    url: "",
    type: "POST",
    data: fd,
    processData: false, 
    contentType: false, 
    success: function(response) {

    }
});

默认情况下,在发布期间不会将图像添加到表单中,您需要获取整个表单,并在发送之前将图像附加到表单中。我这样做是为了asp.net,但它也应该适用于php。

可能是因为图像从未通过ajax发送到服务器,所以
$\u文件
'image'
索引下没有任何内容。 看看


或考虑使用Objult.< /P>您的输入标签被破坏<代码> Plus,这是什么“代码> UPDATE产品?($conn、'product'、':productName'、':productName'、':price'、':QUOTE'、':imageName'、':salePrice'、':productName'、':productName'、':productName'、':price'、':QUOTE'、':imageName'、':salePrice'、'product'、'price'、'QUOTE'、'productName'、'SALEPRIC;

?您的绑定周围也有引号。如果使用PDO,请删除绑定的引号。但是,很难说您的查询是什么或应该是什么。感谢您的修复,但我仍然得到了错误。
updateProduct()
是一个更新产品时可以重用的函数。不客气。你有
var url='editForm.php?;
不应该是
var url='update.php?;
?修复了,但仍然是同一个问题。更新了帖子。是的,我可以帮助你:)Harutyun Abgaryan,更新函数工作得很好。通过给每个变量赋值,我对其进行了独立测试,并将它们正确地发布在数据库中。
$productName = $_POST['Product_Name'];
$productDescription = $_POST['Description'];
$price = $_POST['Price'];
$quantity = $_POST['Quantity'];
$salePrice = $_POST['Sale_Price'];
$oldImage = $_POST['prevPicture'];
$oldProduct = $_POST['oldProduct'];

echo "$productName: " .$productName. " -- $productDescription: " .$productDescription. " - etc etc etc";
die();
var inputImage = $("#inputImage");
var fd = new FormData(document.getElementById("editform")); 

fd.append("image", inputImage);

$.ajax({
    url: "",
    type: "POST",
    data: fd,
    processData: false, 
    contentType: false, 
    success: function(response) {

    }
});