简体   繁体   English

如何在不刷新页面的情况下在同一{div}上显示表单成功消息?

[英]How can i display form success message on same { div } without refreshing the page?

How can I submit a form on same without refreshing page? 如何在不刷新页面的情况下在同一表单上提交表单? Here I'm submitting a pdf to drag and drop container and when I submit the form it's redirecting to upload.php. 在这里,我要提交一个pdf来拖放容器,当我提交表单时,它会重定向到upload.php。 I need to display the successful message on the same container . 我需要在同一容器上显示成功消息。 I have not enough knowledge about the ajax. 我对ajax的了解不足。 Kindly please help me to solve the issue 请帮助我解决问题

Here is the drag and drop container below: 这是下面的拖放容器:

在此处输入图片说明

Here is the result page (upload.php) below: 这是下面的结果页面(upload.php)

在此处输入图片说明

HTML Form: HTML表单:

<form method="POST" action="upload.php" enctype="multipart/form-data">
  <input type="file" multiple name="file[]" accept="application/pdf">
      <input class="button-primary" type="submit" value="Submit">
</form>

Upload.php file: Upload.php文件:

<?php 
//echo 'done';
$output = '';

    if(isset($_FILES['file']['name'][0])){
        //echo 'ok';
        foreach($_FILES['file']['name'] as $keys => $values) {

            if(move_uploaded_file($_FILES['file']['tmp_name'][$keys], 'upload/' .$values)) {

                $output .= 'Form submited succesfully';
            }
        }
    }
echo $output;
?>

You can prevent the default behaviour of the form on submit, which redirects it. 您可以防止表单在提交时的默认行为,该行为将其重定向。 Try using this: 尝试使用此:

<form id="form" method="POST" enctype="multipart/form-data">
  <input id='file' type="file" multiple name="file[]" accept="application/pdf">
  <input class="button-primary" type="submit" value="Submit">
</form>
<p id="response-text"></p>
    <script>
        $("#form").submit(function (e) {
            e.preventDefault();
            var xhttp = new XMLHttpRequest();
            var data = new FormData();
            data.append("file", document.getElementById("file").files[0]);
            xhttp.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("response-text").innerHTML = 'Form Successfully Submitted';
                }
                else {
                    document.getElementById("response-text").innerHTML = 'Form could not be submitted';
                }
            };
            xhttp.open("post", "/upload.php", true);
            xhttp.send(data);
        });

    </script>

Now you can display the desired message wherever you want to display. 现在,您可以在想要显示的任何地方显示所需的消息。 Here I have displayed on <p> of id response-text 我在这里显示了id response-text <p>

Use the following code 使用以下代码

<form method="POST" enctype="multipart/form-data">
            <input id='file' type="file" multiple name="file[]" accept="application/pdf">
            <input id='submit'class="button-primary" type="submit" value="Submit">
        </form>
 $("#submit").submit(function(e){
    e.preventDefault();
 var xhr = new XMLHttpRequest();
          var data = new FormData();

          data.append("file",document.getElementById("file").files[0]);
          xhr.open("post","/upload.php",true);

          xhr.send(data);


            });

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

相关问题 弹出div中的成功消息,无需刷新页面 - success message in popup div without refreshing page 用户提交表单后如何显示成功消息 - How can I display a success message after the user submit the form 如何在 Laravel 不刷新页面的情况下重新加载 div - How can i reload a div without refreshing page in Laravel 提交表单后如何在同一页面中获取成功消息? - How to get the success message in the same page after submitting the form? 如何使联系表单成功消息显示在同一页面中 - How do I make contact form success message appear in same page 如何使用malsup表单jquery成功发送邮件后在表单提交中显示成功消息而无需刷新页面 - How to display success message in a form submit after sending a mail successfully using malsup form jquery without page refresh 如何在不刷新的情况下在同一页面上显示上传的文件内容? - how to display the uploaded file contents on the same page without refreshing? 在同一页面上提交表格和成功消息? - Submitting form and success message on same page? 与表单在同一页面上的成功或错误消息 - Success or error message on same page as form 如何在不重定向的情况下在同一页面中显示成功或错误警报 - how to display success or error alert in same page without redirecting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM