防止邮寄后重定向 [英] Prevent redirecting after post

查看:131
本文介绍了防止邮寄后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提出一个已经有多个答案和多次的问题可能是一个坏主意,但我仍然应该问这个问题。我尝试了几乎所有我在那里找到的在提交表单后阻止重定向,但没有任何帮我。
有一些小细节,我没有看到。我对jQuery和AJAX不是很熟悉。特别是与前者。
所以,代码:

 < form id =formaction =uploadfile.phpmethod = postenctype =multipart / form-data><! -  action =uploadfile.phponsubmit =return false; - > 
< label>名称< / label>
< input id =usernamename =usernametype =textonblur =checkUsername(this.value)onkeypress =clearError('nameerror')oninput =clearError('nameerror' )/>< br>
< label id =nameerror>< / label>< br>
< label>电子邮件< / label>
< input id =emailname =emailtype =textonblur =validateEmail(this.value)onkeypress =clearError('emailerror')/>< br>
< label id =emailerror>< / label>< br>
选择一个档案< br />
< label id =draganddroperror>< / label>< br>
< input name =fileToUpload []id =fileToUploadtype =fileonchange =onChange(event)multiple />< br />>
< button id =btnSubmitonclick =sendData()style =background-color:gray; color:#ffffff; />Отправить< /按钮>
< / form>

我的JS

  function sendData(){
var file_data = $(#fileToUpload)。prop(files);
console.log(file_data);
if($(#file_data)。val()!=){
var form_data = new FormData();
//form_data.append('file',file_data);
//console.log(文件);
form_data.append('file',file_data);
console.log(form_data);
$ .ajax({
url:'uploadfile.php',//指向服务器端PHP脚本
dataType:'text',// PHP脚本的期望值,如果有的话
cache:false,
contentType:false,
processData:false,
data:form_data,
类型:'post',
成功:function(data){
// get server responce here
// alert(data);
//清除文件字段
// $(#your-files ).val();
return false;
}
});
返回false; //event.preventDefault();
} else {
alert(请选择文件!);




$ b $ p
$ b $ p所以,这是有问题的代码。所有作品完美无瑕,除重定向外。另一个问题包含提交,但我没有提交输入。我试图从post方法(第1行)脱离表单,但是我得到了服务器错误。到处都返回false。
我在这个问题上花了无数个小时,几乎所有的夜晚都花了几天时间。我将不胜感激任何帮助,谢谢。

解决方案

防止表单提交的技巧是 return false onsmit提交如下:

 < form id =formonsubmit =return sendData()method =postenctype =multipart /格式数据> 
<! - action =uploadfile.phponsubmit =返回false; - >
< label>名称< / label>
< input id =usernamename =usernametype =textonblur =checkUsername(this.value)onkeypress =clearError('nameerror')oninput =clearError('nameerror' )/>
< br>
< label id =nameerror>< / label>
< br>
< label>电子邮件< / label>
< input id =emailname =emailtype =textonblur =validateEmail(this.value)onkeypress =clearError('emailerror')/>
< br>
< label id =emailerror>< / label>
< br>选择一个文件
< br />
< label id =draganddroperror>< / label>
< br>
< input name =fileToUpload []id =fileToUploadtype =fileonchange =onChange(event)multiple />
< br />
< button type =submitid =btnSubmitstyle =background-color:grey; color:#ffffff;>上传< / button>
< / form>

请注意,我写过 onsubmit = return sendData()。当 sendData()将返回 true 时,表单将被提交,否则它将不会被提交。为此, sendData()中的最后一条语句是 return false; 。这样,窗体永远不会在当前窗口中提交,而只能通过Ajax提交。

 函数sendData(){
var file_data = $(#fileToUpload)。prop(files);
console.log(file_data);
if($(#file_data)。val()){
var form_data = new FormData();
//form_data.append('file',file_data);
//console.log(文件);
form_data.append('file',file_data);
console.log(form_data);
$ .ajax({
url:'uploadfile.php',//指向服务器端PHP脚本
dataType:'text',// PHP脚本的期望值,如果有的话
cache:false,
contentType:false,
processData:false,
data:form_data,
类型:'post',
成功:function(data){
// get server responce here
// alert(data);
//清除文件字段
// $(#your-files ).val();
}
});
} else {
alert(请选择文件!);
}
返回false;
}

我希望这可以给你清楚的理解。


It's probably a bad idea to ask a question, which already have multiple answers and multiple times, but I should ask it anyway. I tried pretty much everything I find there Prevent redirect after form is submitted but nothing helps me. There is a some minor detail, which I don't see. I'm not very familiar with jQuery and AJAX. Especially with the former. So, the code:

<form id="form" action="uploadfile.php" method="post" enctype="multipart/form-data"  ><!--action="uploadfile.php" onsubmit="return false;" -->
    <label>Name</label>
    <input id="username" name="username" type="text" onblur="checkUsername(this.value)" onkeypress="clearError('nameerror')" oninput="clearError('nameerror')" /><br>
    <label id="nameerror"></label><br>
    <label>Email</label>
    <input id="email" name="email" type="text" onblur="validateEmail(this.value)" onkeypress="clearError('emailerror')"/><br>
    <label id="emailerror"></label><br>
    Select a file<br />
    <label id="draganddroperror"></label><br>
    <input name="fileToUpload[]" id="fileToUpload" type="file" onchange="onChange(event)" multiple /><br />
    <button id="btnSubmit" onclick="sendData()"  style="background-color: gray; color: #ffffff;" />Отправить</button>
</form>

There is my JS

function sendData() {
    var file_data = $("#fileToUpload").prop("files");
    console.log(file_data);
    if ($("#file_data").val() != "") {
        var form_data = new FormData();
        //form_data.append('file', file_data);
        //console.log(file);
        form_data.append('file', file_data);
        console.log(form_data);
        $.ajax({
            url: 'uploadfile.php', // point to server-side PHP script
            dataType: 'text', // what to expect back from the PHP script, if anything
            cache: false,
            contentType: false,
            processData: false,
            data: form_data,
            type: 'post',
            success: function(data) {
                // get server responce here
                //alert(data);
                // clear file field
                //$("#your-files").val("");
                return false;
            }
        });
        return false; //event.preventDefault();
    } else {
        alert("Please select file!");
    }
}

So, this is the code in question. All works flawlessly, except redirect. Another questions contains submit, but I didn't have submit input. I tried to delink form from post method (1st line), but I got server error. Return false everywhere. I spent countless hours on this question, it consumed almost all my night hours for a few days. I would appreciate any help, thanks.

解决方案

The trick to prevent form submission is return false onsubmit as below:

<form id="form" onsubmit="return sendData()" method="post" enctype="multipart/form-data">
    <!--action="uploadfile.php" onsubmit="return false;" -->
    <label>Name</label>
    <input id="username" name="username" type="text" onblur="checkUsername(this.value)" onkeypress="clearError('nameerror')" oninput="clearError('nameerror')" />
    <br>
    <label id="nameerror"></label>
    <br>
    <label>Email</label>
    <input id="email" name="email" type="text" onblur="validateEmail(this.value)" onkeypress="clearError('emailerror')" />
    <br>
    <label id="emailerror"></label>
    <br> Select a file
    <br />
    <label id="draganddroperror"></label>
    <br>
    <input name="fileToUpload[]" id="fileToUpload" type="file" onchange="onChange(event)" multiple />
    <br />
    <button type="submit" id="btnSubmit" style="background-color: gray; color: #ffffff;">Upload</button>
</form>

Note that I have written onsubmit=return sendData(). When the sendData() will return true the form will get submitted, otherwise it will never get submitted. For that the last statement in sendData() is return false;. In this way the form never gets submitted in current window, instead only Ajax submit works.

function sendData() {
  var file_data = $("#fileToUpload").prop("files");
  console.log(file_data);
  if ($("#file_data").val()) {
    var form_data = new FormData();
    //form_data.append('file', file_data);
    //console.log(file);
    form_data.append('file', file_data);
    console.log(form_data);
    $.ajax({
      url: 'uploadfile.php', // point to server-side PHP script
      dataType: 'text', // what to expect back from the PHP script, if anything
      cache: false,
      contentType: false,
      processData: false,
      data: form_data,
      type: 'post',
      success: function(data) {
        // get server responce here
        //alert(data);
        // clear file field
        //$("#your-files").val("");
      }
    });
  } else {
    alert("Please select file!");
  }
  return false;
}

I hope this gives you the clear understanding.

这篇关于防止邮寄后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆