在模式内部上传图像不会上传并且不会保存到数据库

时间:2018-09-05 18:41:28

标签: javascript php jquery html bootstrap-modal

我有这个带有文件上传的引导模式形式,我将其用于图像,但是当我点击保存时,除了图像,我的所有数据都插入到数据库中,并且即使文件夹中的上传也不起作用,我也没有。不知道为什么。感谢任何帮助,因为我是编码新手。

谢谢

HTML:

// Button that trigger modal
<button class="float-right col-3 btn btn-primary btn-block" data-toggle="modal" data-target="#adauga-gr"><i class="icon-plus"></i> ADD INFOS</button>
// Modal window
<div class="modal fade" id="adauga-gr" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel-4" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                          <div class="modal-content">
                            <div class="modal-header">
                              <h5 class="modal-title" id="exampleModalLabel-4">Adauga Grupe si Instructori</h5>
                              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                              </button>
                            </div>
                            <div class="modal-body">
                              <form method="post" action="#" class="grupe" id="addgrupe_instructori" enctype="multipart/form-data">
                                <div class="form-group">
                                  <label for="poza" class="col-form-label">**Add Picture**</label>
                                   <input type="file" name='photo' class="dropify" />

                                 </div>
                                <div class="form-group">
                                  <label for="instructor" class="col-form-label">Instructor:</label>
                                  <input type="text" name="instructor" class="form-control" id="instructor" >
                                </div>
                                <div class="form-group">
                                  <label for="grupa1" class="col-form-label">Grupa 1:</label>
                                  <input type="text" name="grupa1" class="form-control" id="grupa1">
                                </div>
                                <div class="form-group">
                                  <label for="grupa2" class="col-form-label">Grupa 2:</label>
                                  <input type="text" name="grupa2" class="form-control" id="grupa2">
                                </div>
                                <div class="form-group">
                                  <label for="grupa3" class="col-form-label">Grupa 3:</label>
                                  <input type="text" name="grupa3" class="form-control" id="grupa3">
                                </div>
                              </form>
                            </div>
                            <div class="modal-footer">
                              <button type="button" class="btn btn-success add-gr">Adauga in tabel</button>
                              <button type="button" class="btn btn-light" data-dismiss="modal">Inchide</button>
                            </div>
                          </div>
                        </div>
                      </div>

PHP:

<?php

    $db = mysqli_connect("localhost", "root", "pass", "db");


        //Getting actual file name
        $name = $_FILES['photo']['name'];

        //Getting temporary file name stored in php tmp folder
        $tmp_name = $_FILES['photo']['tmp_name'];

        //Path to store files on server
        $path = 'images/instructori/';
        $fileTarget = 'images/instructori/'.$name.
        //checking file available or not

            //Moving file to temporary location to upload path
            move_uploaded_file($tmp_name,$path.$name);


    $instructor = $_POST['instructor'];
    $grupa1 = $_POST['grupa1'];
    $grupa2 = $_POST['grupa2'];
    $grupa3 = $_POST['grupa3'];

    $sql = mysqli_query($db,"INSERT INTO `instructori_grupe` SET poza='$fileTarget',instructor='$instructor',grupa1='$grupa1',grupa2='$grupa2',grupa3='$grupa3'");

?>

Javascript:

$('.add-gr').on("click", function(e) {
   e.preventDefault();
    var form = $("#addgrupe_instructori");
     $.ajax({
            type: 'post',
            url: 'sql/addinstructorgrupe.php',
            data: $(form).serialize(),
            success: function (data) {
              console.log(data);
           $(".close").click();
             }
          });
 });

0 个答案:

没有答案