weixin_33749131 2017-04-14 14:36 采纳率: 0%
浏览 15

使用ajax形式发布数据ID

I've a question about ajax form. So, You can see my following ajax code have data-id . I'm getting this id in .pmsc div. But When I press #upmsc for uploading image then I am not getting any error, just getting this result from chrome developer console, no upload, no inserting the image name in database. What I'm doing wrong here.

enter image description here

Then ajax submit form is this

$('body').on('change', '#upmsc', function(e) {
   e.preventDefault();
   var id = $(".pmsc").attr("data-id");
   var data = 'id=' + id;
   $("#musicform").ajaxForm({
      type: "POST",
      data: data,
      cache: false,
      target: '.appended',
      beforeSubmit: function() {
         // Do Something
      },
      success: function(response) {
         // Do Something
         $(".showecho").html(response);
      },
      error: function() {}
   }).submit();
});

My html form is this:

    <form id="cfrm" class="options-form" method="post" enctype="multipart/form-data" action="'.$base_url.'upload.php">
       <label class="mcup" for="upcmsc"></label>
<input type="file" name="musicCover" id="upcmsc" />
    </form>
    <div class="pmsc" id="56">
    </div>

and PHP code is this

<?php
include_once 'inc/inc.php';
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['id'])) {
    $id = mysqli_real_escape_string($db, $_POST['id']);
    $name = $_FILES['musicCover']['name'];
    $size = $_FILES['musicCover']['size'];
    if(strlen($name)) {
        $ext = getExtension($name);
        if(in_array($ext,$valid_formats)) {
            if($size<(50024*50024)) { 
                $actual_image_name = time().$uid.".".$ext;
                $tmp = $_FILES['musicCover']['tmp_name'];
                if(move_uploaded_file($tmp, $mcoverpath.$actual_image_name)) {
                    $data=$HuHu->Music_Cover_Image_Upload($id,$actual_image_name);
                    if($data){
                        echo '<img class="cover covermsc" src="'.$base_url.$mcoverpath.$actual_image_name.'">';
                        }
                  } else {
                      echo "Fail upload folder with read access.";
                }
                } else
                  echo "Image file size max 1 MB";                  
       } else
          echo "Invalid file format.";
     } else
         echo "Please select image..!";
        exit;
 }


?>
  • 写回答

1条回答 默认 最新

  • weixin_33716941 2017-04-14 14:49
    关注
    var data = 'id=' + id;
    $("#musicform").ajaxForm({
      type: "POST",
      data: data,
    

    You set the additional data to a string while it should be an object. That then fails your checks in the PHP code. So try this:

    var data = {id: id};
    

    Found some more problems with the PHP code braces, try this:

    <?php
    include_once 'inc/inc.php';
    $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['id'])) {
        $id = mysqli_real_escape_string($db, $_POST['id']);
        $name = $_FILES['musicCover']['name'];
        $size = $_FILES['musicCover']['size'];
    
        if(strlen($name)) {
            $ext = getExtension($name);
            if(in_array($ext,$valid_formats)) {
                if($size<(50024*50024)) { 
                    $actual_image_name = time().$uid.".".$ext;
                    $tmp = $_FILES['musicCover']['tmp_name'];
    
                    if(move_uploaded_file($tmp, $mcoverpath.$actual_image_name)) {
                        $data=$HuHu->Music_Cover_Image_Upload($id,$actual_image_name);
                        if($newdata){
                            echo '<img class="cover covermsc" src="'.$base_url.$mcoverpath.$actual_image_name.'">';
                        }
                    } else {
                        echo "Fail upload folder with read access.";
                    }
                } else {
                    echo "Image file size max 1 MB";        
                }
            } else {
                echo "Invalid file format.";
            }
        } else {
            echo "Please select image..!";
            exit;
        }
    } else {
        echo '1';   
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝