Ajax不会将数据传递给我的控制器

时间:2017-06-21 14:30:18

标签: php jquery

我已经为此工作了好几个小时。我有一个基本表单,为我建立的商店提交数据和特征图像。

我使用ajax从视图向控制器发送数据,但控制器中的$_POST数组始终为空。

查看:

<form method="POST" id="newproducts" enctype="multipart/form-data">
<div class="form-group">
    <label class="control-label">Product Name</label>
    <input type="text" name="product" class="form-control" placeholder="Enter product name">                                
</div>
<div class="form-group">
    <label class="control-label">Prodct Category</label>
    <select name="category" class="form-control" id="mycat">
        <option value=""></option>
        <option value="1">Dummy Category 1</option>
        <option value="2">Dummy Category 2</option>
    </select>
</div>
<div class="form-group">
    <label class="control-label">Available Stock</label>
    <input type="number" name="stock" class="form-control" placeholder="Number of Available units">
</div>
<div class="form-group">
    <label class="control-label">Product Price</label>
    <input type="number" class="form-control" name="price" placeholder="Price without the (R)">
</div>
<div class="form-group">
    <label class="control-label">Product Description</label>
    <textarea name="description" placeholder="Enter Prodct Description" class="form-control"></textarea>
</div>
<div class="form-group">
    <label class="control-label">Feature Image</label>
    <input type="file" name="feature" id="feature">
    <div class="error"></div>
    <div class="clearfix"></div>
    <p>&nbsp;</p>
    <div id="preview"></div>
</div>
<div class="form-group">
    <button type="submit" class="btn btn-success" name="add">Add Product</button>
</div>
<div class="form-group">
    <input type="image" src="view/assets/images/ajax-loader.gif" style="display: none;" name="loader">
</div>

product.js

我遗漏了验证只是为了显示相关代码

var formData = new FormData($('#newproducts')[0]);

            $.ajax({
                type : "POST",
                url  : "controller/products.php",
                // data : {product :product, category:category,stock:stock,price:price,description:description,feature:feature},
                data :  formData,
                cache : false,
                contentType: true,
                processData:false,
                async : false,
                traditional: true,
                beforeSend : function(){

                    $('button[type="submit"][name="add"]').html("Please Wait...");
                    $('input[type="image"][name="loader"]').css('display','block');
                },
                success : function(results){
                    $('button[type="submit"][name="add"]').html("Add Product");
                    $('input[type="image"][name="loader"]').css('display','none');

                    if(results == "ok"){
                        $('#results').removeClass('error');
                        $('#results').addClass('success');
                        $('#results').html('Product added successfully...');
                    }else{

                        $('#results').removeClass('success');
                        $('#results').addClass('error');
                        $('#results').html(results);
                    }
                }
            });
        return false;   
    }

当我发送这样的数据时:data : {product :product, category:category,stock:stock,price:price,description:description,feature:feature},一切正常,但我唯一的挑战就是发送图像数据进行上传。

我的控制器:

我刚刚做了一个简单的var_dump来查看它是否获取数据

var_dump($_POST)

给出:

array(0) { }

2 个答案:

答案 0 :(得分:2)

尝试更改这部分ajax,使用这些设置,我让它工作。 Using this post and one of my own I did a while back for reference,这是我寻找&#34;复习&#34;在FormData

以下是我更改的部分:

// Pass event, use this instead of false at the end,
// this is just how I stop the default action, it's optional
e.preventDefault();
// Set the form into the formdata
var formData    =   new FormData($("#newproducts")[0]);    
// Append the file to the formdata
formData.append('file',$( '#feature' )[0].files[0]);
// Send normally
$.ajax({
    // Same
    type : "POST",
    url  : "controller/products.php",
    data :  formData,
    processData: false,
    contentType: false,
    // I removed "traditional", but I don't know if it still works with it,
    // I never tried it. Everything after is the same...

答案 1 :(得分:-1)

检查你的php.ini的大小限制,并尝试使用比你更大的大小(如20MB):

; Maximum allowed size for uploaded files.
upload_max_filesize = 20M

; Must be greater than or equal to upload_max_filesize
post_max_size = 20M

几年前我遇到了同样的问题。当POST,GET和FILES为空时,问题始终是输入的大小,因此如果您尝试增加限制,则上传应该可以正常工作。 您还可以尝试上传比实际配置更小的文件。