[英]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.