Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Laravel通过modal上传图像不工作_Javascript_Php_Jquery_Ajax_Laravel - Fatal编程技术网

Javascript Laravel通过modal上传图像不工作

Javascript Laravel通过modal上传图像不工作,javascript,php,jquery,ajax,laravel,Javascript,Php,Jquery,Ajax,Laravel,我在拉威尔做一个库存系统,我想上传一张我所有产品的图片。我的问题是我不能上传图像。我在上传照片时尝试了我的旧代码,但它在我当前的项目中不起作用。您在控制器中看到的上载代码是一种laravel集合形式。但是现在,我使用一个模态和一个ajax请求将输入保存到数据库中。有人知道我该怎么办吗?顺便说一句,我的模态没有表单标签,因为我认为表单在模态中不起作用。提前谢谢 模态创建 <div class="modal fade" id="exampleModalCenter" tabindex

我在拉威尔做一个库存系统,我想上传一张我所有产品的图片。我的问题是我不能上传图像。我在上传照片时尝试了我的旧代码,但它在我当前的项目中不起作用。您在控制器中看到的上载代码是一种laravel集合形式。但是现在,我使用一个模态和一个ajax请求将输入保存到数据库中。有人知道我该怎么办吗?顺便说一句,我的模态没有表单标签,因为我认为表单在模态中不起作用。提前谢谢

模态创建

     <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title" id="exampleModalCenterTitle">Register New Product</h5>
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                          </button>
                        </div>
                        <div class="modal-body">
                        <p style="font-weight: bold;">Name </p>
                          <input   type="text" class="form-control" id="product_name"/>
                          <p style="font-weight: bold;">Description </p>
                          <input   type="text" class="form-control" id="description"/>

                          <p style="font-weight: bold;">Price </p>
                          <input   type="text" class="form-control" id="currentprice"/>
                          {{-- <input style="text-transform:uppercase"   type="text" class="form-control" id="supplier_id"/> --}}
                          <p style="font-weight: bold;">Supplier </p>
                          <select class="form-control"  id="supplier_id"  >
                              @foreach ($suppliers as $supplier)
                          <option value="{{$supplier->id}}">{{$supplier->name}}</option>
                              @endforeach
                              </select>
                         <p style="font-weight: bold;">Picture </p>
                          <input  type="file" class="form-control" id="picture"/>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                          <button type="button" class="btn btn-primary" id="add_product">Add</button>
                        </div>
                      </div>
                    </div>
                  </div>
阿贾克斯

模型


我不能添加任何评论,这是愚蠢的

您可能还需要在json中对图像数据进行编码


(固定措辞)

我不能添加评论,那是愚蠢的

您可能还需要在json中对图像数据进行编码


(固定措辞)

在Ajax代码中,定义了
image
变量。凯先生,我想我忘记定义
image
变量了。我会在几分钟内告诉你结果没有错误,先生,当我添加此代码时,
var image=$('#picture').val()当我查看数据库时,图像名称如下所示:“C:\fakepath\59838793_48282515589519_4600609237641461760_n.jpg”。我的
public/img
目录中没有图像这可能对定义
image
变量的Ajax代码有所帮助。好的,先生,我想我忘记定义
image
变量了。我会在几分钟内告诉你结果没有错误,先生,当我添加此代码时,
var image=$('#picture').val()当我查看数据库时,图像名称如下所示:“C:\fakepath\59838793_48282515589519_4600609237641461760_n.jpg”。my
public/img
目录中没有图像这可能有助于您尝试将这些图像添加到您的Ajax、enctype:“多部分/表单数据”、processData:false、contentType:false第一个通知服务器您提交了混合数据。processData告诉query不要“处理”正在发送的数据,contentType告诉jquery不要更改类型。我输入了你想要的代码,现在它返回了一个错误。我无法再插入任何错误,因为在我的控制器
store
方法中存在
未定义的索引:product\u name
。我觉得没什么问题,没问题。我相信您会明白的。您可以尝试将这些添加到您的Ajax、enctype:“multipart/form data”、processData:false、contentType:false第一个通知服务器您提交了一个混合数据。processData告诉query不要“处理”正在发送的数据,contentType告诉jquery不要更改类型。我输入了你想要的代码,现在它返回了一个错误。我无法再插入任何错误,因为在我的控制器
store
方法中存在
未定义的索引:product\u name
。我觉得没什么问题,没问题。我相信你会明白的。
    public function store(Request $request)
    {
        $data = $request->all();
        $data['product_name'] = ($data['product_name']);
        $data['description'] = ($data['description']);
        $data['supplier_id'] = ($data['supplier_id']);
        $data['price'] = ($data['price']);



        if ($request->hasFile('image')){
            //Add new photo
                $image = $request->file('image');
                $filename = time() . '.' . $image->getClientOriginalExtension();
                $location = public_path('img/' . $filename);
                Image::make($image)->resize(300,300)->save($location);
                $oldFilename = $products->image;
            //Update DB
                $products->image = $filename;
             //Delete the old photo
                // Storage::delete($oldFilename);
            }

        Product::create($data);
        return response()->json($data);
    }
   $(document).ready(function(){
                //add
                $('#add_product').click(function(e){
                    e.preventDefault();
                    var name = $('#product_name').val();
                    var description = $('#description').val();
                    var price = $('#currentprice').val();
                    var supplier_id = $('#supplier_id').val();
                    var image = $('#picture').val();

                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });

                    $.ajax({
                        url: "{{    url('/product')     }}",
                        method: 'post',
                        data:{
                            product_name: name,
                            description: description,
                            price: price,
                            supplier_id: supplier_id,
                            image: image,
                        },
                        success: function (res){
                            console.log(res);
                            window.location.href = '{{route("products")}}'
                        }
                    });

                });
                //add
class Product extends Model
{
    use SoftDeletes;
    protected $fillable = [
        'product_name', 'quantity', 'description' ,'supplier_id', 'price', 'image',
    ];

    public function supplier()
    {
        return $this->hasOne('App\Supplier');
    }
}