简体   繁体   English

使用 jQuery 和 AJAX 将表单的文件和文本数据上传到 PHP

[英]upload a form's file and text data to PHP using jQuery and AJAX

Good morning.早上好。 I'm trying to make the form submission of a message more fluid avoiding the reload of the page for the sending of it.我正在尝试使消息的表单提交更加流畅,避免重新加载页面以发送它。 Since the message may be text or image, I need to send both of them to a PHP page for upload.由于消息可能是文本或图像,我需要将它们都发送到 PHP 页面进行上传。 I'm using this code in the html page:我在 html 页面中使用此代码:

<form id="newmessage" enctype="multipart/form-data">

<textarea form="newmessage" id="messagetext" name="messagetext" ></textarea>
         
<input type="submit" name="submit" value="send" onclick="return newMessage();">
         
<input type="file" accept="image/*" id="image" name="image">

</form>
      
<script>
function newMessage(){
var messagetext = document.getElementById("messagetext").value;
var image = document.getElementById("image").value;

      $.ajax({
           type:"post",
           url:"new_message.php",
           data: 
           {  
            "messagetext" :messagetext,
            "image" :image,
           },
           cache:false,
           success: function(html) {
               document.getElementById("messagetext").value = "";
                 }
             });
             
             return false;
          }
      </script>

As you can see, I'm allowing users to type in the textarea or upload a file.如您所见,我允许用户输入文本区域或上传文件。 When they submit the form, the newMessage() method is invoked and sends image and messagetext to new_message.php, which process them:当他们提交表单时,调用 newMessage() 方法并将图像和消息文本发送到new_message.php ,后者会处理它们:

   // new_message.php
   $messagetext = $_POST["messagetext"];
   $image = $_FILES["image"]["tmp_name"]; 
   
   if((!empty($messagetext) || isset($image))) {
   
       if(!empty($messagetext)) {
           // create text message
       } else if(isset($image)) {
           // create image message
       }
   }

When I write a text message it works perfectly, but it doesn't send anything if it's image.当我写一条短信时,它工作得很好,但如果它是图像,它就不会发送任何东西。 Maybe the image variable in AJAX is not taking the file properly.也许 AJAX 中的图像变量没有正确获取文件。 I excuse if this question is unclear, but I'm a beginner in StackOverlow and I'm open to edits.如果这个问题不清楚,我很抱歉,但我是 StackOverlow 的初学者,我愿意编辑。 Thanks for all replies.感谢所有回复。

can you try this.你能试试这个吗? you don't need to worry about the file and message in textarea.您无需担心 textarea 中的文件和消息。 Make sure you have added jQuery.确保您已添加 jQuery。

$("#newmessage").on("submit", function(ev) {
  ev.preventDefault(); // Prevent browser default submit.

  var formData = new FormData(this);
    
  $.ajax({
    url: "new_message.php",
    type: "POST",
    data: formData,
    success: function (msg) {
      document.getElementById("messagetext").value = "";
    },
    cache: false,
    contentType: false,
    processData: false
  });
  return false;
});

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

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