【问题标题】:AJAX Maile sender + Multiple Attachment + Bootstrap pluginAJAX Maile 发件人 + 多个附件 + Bootstrap 插件
【发布时间】:2016-12-20 20:46:48
【问题描述】:

我刚刚编写了用于发送电子邮件的脚本。它采用随机电子邮件并使用随机值发送 - 一切正常。现在我只想添加发送多张图片的选项 - 选择随机一张并通过电子邮件发送。

我使用引导程序,我发现了这个插件:http://plugins.krajee.com/file-input

但是当我尝试上传图片时,我收到 405 错误。 Meaby你知道用ajax发送附件的任何其他方式吗?

这是我的代码:

$("#dodawarka").submit(function(e){
      e.preventDefault();


      var tytul = $("#tytul").val();
      var kontos = $("#lkonta").val();
      var tresc = $("#tresc").val();
      var jemail = $("#kemaile").val();
      var jemail = $("#kemaile").val();


      var dataString = 'listakont=' + kontos + '&tytul=' + tytul + '&tresc=' + tresc + '&emailjaki=' + jemail;

        $.ajax({
        type: "POST",
        url: "dodaj.php",
        data: dataString,
            beforeSend: function(){
              $('#tytul, #tresc, #przyciskx').prop( "disabled", true );
              $('.wczytywanie').show();
            },
            success: function(){
                $('#ddodaneok').show();
                $('#dodawarka').slideUp();
                $('#linkizwrotne').show();
            },
             error: function(){
                $('#kurwasabledy').show();
             }
    });

        return false;
});

还有:

    <form role="form" enctype="multipart/form-data" id="dodawarka" method="post">
  <div class="form-group" id="listakont" style="visibility:hidden; position: absolute;">
    <label for="listakont">Lista kont na które zostaną wysłane wpisy</label>
    <textarea class="form-control" rows="10" id="lkonta" disabled></textarea>
  </div>
  <div class="form-group" id="ssssemail" style="visibility:hidden; position: absolute;">
    <label for="ssssemail">Emaile z których będzie można wysyłam wpisy podaj w formacie</label>
    <textarea class="form-control" rows="10" id="kemaile"></textarea>
  </div>
  <div class="form-group">
    <label for="tytul">Podaj tytuł wpisu <em>+spintax</em></label>
    <input type="tytul" class="form-control" id="tytul" required>
  </div>
  <div class="form-group">
    <label for="tresc">Treść wpisu <em>+spintax</em></label>
    <textarea class="form-control" rows="25" id="tresc" required></textarea>
  </div>
  <div class="form-group">
    <label for="obrazki">Dodaj obrazki</label>
    <input id="input-id" type="file" class="file" data-preview-file-type="text" multiple=true data-min-file-count="1">
  </div>
  <button type="submit" class="btn btn-default" id="przyciskx"><img src="img/infinity.gif" class="wczytywanie" /> Wyślij na zaplecza</button>
</form>

【问题讨论】:

    标签: php jquery ajax forms email


    【解决方案1】:

    试试:

    &lt;input id="input-id" type="file" class="file" data-preview-file-type="text" multiple name="files[]" data-min-file-count="1"&gt;

    已添加:name="files[]",仅multiple 而非multiple=true

    在 php 中做:

    $output = '';
    
    if(is_array($_FILES)){
    
        foreach($_FILES['files']['name'] as $name => $value)
        {
            $file_name = explode(".", $_FILES['files']['name'][$name]);
            $allowed_ext = array("jpg", "jpeg", "png", "gif");
    
            if(in_array($file_name[1], $allowed_ext))
               {$new_name = $userid.md5(rand()).".".$file_name[1];
    
                $sourcePath = $_FILES['files']['tmp_name'][$name];
    
                $targetPath = "uploadedimages/".$new_name;
    
                include_once("connection.php"); // your connection to DB and select DB
    
                $sql = "INSERT into images(userid, imgpath) VALUES ('$userid', '$targetPath')";
    
                $query = $conn->query($sql);
    
    
                if($query && move_uploaded_file($sourcePath, $targetPath))
                {
                    $output .= '<div class="col-md-3"><img src="'.$targetPath.'" class="img-thumbnail" width="150px" height="180px"/></div>';
                }
            }
    
        }
    }
    echo $output;
    

    【讨论】:

    • 谢谢,但还需要使用 ajax 发送的脚本 :)
    • 您将它与仅 AJAX 的朋友一起发送 :) 您可以使用已有的 AJAX。
    猜你喜欢
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多