简体   繁体   English

通过 ajax 和 php 上传图片作为附件

[英]upload an image as attachment via ajax and php

So i have a work HTML form that sends the entered content to email.所以我有一个工作 HTML 表格,将输入的内容发送到 email。 This is use jquery to submit the form data via ajax to a php file to send email.这是使用 jquery 通过 ajax 将表单数据提交到 php 文件以发送 Z0C83ZF57C786A7EB4 This is working fine no problems.这工作正常没有问题。 I now want to add max 4 document file uploads to the form and have these sent with the form data as attachments.我现在想向表单添加最多 4 个文档文件上传,并将这些文件与表单数据一起作为附件发送。 So i believe that i understand the HTML Code form this, i also believe i understand how to the js checking bit and most of the php side.所以我相信我了解 HTML 代码形式,我也相信我了解如何检查 js 位和大部分 php 方面。 So whats the problem i here you ask, well i just cant get my head around the ajax data: string.那么我在这里问的问题是什么,我只是无法理解 ajax 数据:字符串。

$.ajax({
  type: "POST",
  url: "assets/php/quick_form.php",
  data:
    "name=" +
    name +
    "&reg=" +
    reg +
    "&phone=" +
    phone +
    "&vehicleType=" +
    vehicleType +
    "&postCode=" +
    postCode +
    "&message=" +
    message +
    "&phoneCon=" +
    phoneCon +
    "&textCon=" +
    textCon +
    "&whatsApp=" +
    whatsApp,
  success: function (text) {
    if (text == "success") {
      formSuccess();
    } else {
      formError();
      submitMSG(false, text);
    }
  },
});

Every help or example i see sets data: as formData variable and i'm just unsure what i need to do with js var's to pass file data as this format from my form so a can then process it in php.我看到的每个帮助或示例都设置数据:作为 formData 变量,我只是不确定我需要用 js var 做什么才能从我的表单中以这种格式传递文件数据,以便可以在 php 中处理它。

If some one could point me to an example of how to do this i would be much appreciative.如果有人能指出我如何做到这一点的例子,我将不胜感激。 I have spent all night doing this and i just cant seem to get my head around this我整晚都在做这件事,我似乎无法理解这个

Thanks in advance for any help提前感谢您的帮助

In jquery ajax method the data property accept a object, string or array, you could collect all form entries as formData在 jquery ajax 方法中,数据属性接受 object、字符串或数组,您可以将所有表单条目收集为 formData

const formData = new FormData(form);

And then tranform formData to an object然后将 formData 转换为 object

const data = Object.fromEntries(formData)

To use it in the ajax method在 ajax 方法中使用它

const form = document.form.formID

// collect form data
const formData = new FormData(form);

// tranform formData to an object
const data = Object.fromEntries(formData);

$.ajax({
  type: "POST",
  url: "assets/php/quick_form.php",
  data: data,
  success: function (text) {
    if (text == "success") {
      formSuccess();
    } else {
      formError();
      submitMSG(false, text);
    }
  },
});

Update 0更新 0

Because Object.fromEntries() has support since firefox 63, chrome 73 that is to say recent versions of browsers, you can use the following logic to get the object of an array without the need of Object.fromEntries Because Object.fromEntries() has support since firefox 63, chrome 73 that is to say recent versions of browsers, you can use the following logic to get the object of an array without the need of Object.fromEntries

 const formData = new FormData(); formData.append("name", "ada"); formData.append("email", "ada@example.com"); const data = Array.from(formData).map(([name, value]) => ({ name, value, })).reduce((previousValue, currentValue) => { const [name, value] = Object.values(currentValue); previousValue[name] = value; return previousValue; }, {}); console.log(data);

See

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM