【发布时间】:2014-03-08 14:09:13
【问题描述】:
基本上我有这个onclick 事件,它序列化一些表单数据并将其保存到一个变量中,当用户运行另一个函数时,我希望能够通过函数中的 ajax 发送之前创建的变量。
这里是onclick 事件(第一种形式):
$('#new_shout_next').on('click', function () {
var new_shout_slide_1_form = $("#new_shout_form").serialize();
});
这是在onclick 事件之后执行的函数,所以希望你能明白我的意思(第二种形式):
function uploadFile(){
var file = _("new_shout_upload_pic").files[0];
var formdata = new FormData();
formdata.append("new_shout_upload_pic", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "scripts/dashboard/dash_new_shout_upload.php");
var new_shout_slide_1_form = $("#new_shout_form").serialize(); //This is the edit i have made and removed this line of code from the on click event above
ajax.send(formdata, new_shout_slide_1_form);
}
如果您需要,这里是dash_new_shout_upload.php:
$fileName = $_FILES["new_shout_upload_pic"]["name"];
$fileTmpLoc = $_FILES["new_shout_upload_pic"]["tmp_name"];
$fileType = $_FILES["new_shout_upload_pic"]["type"];
$fileSize = $_FILES["new_shout_upload_pic"]["size"];
$fileErrorMsg = $_FILES["new_shout_upload_pic"]["error"];
$new_shout_text = $_POST['hiddenNewShoutText']; //This is one of the fields in the serialized form first created in the onclick event.
这是我在控制台中遇到的错误:
Uncaught ReferenceError: new_shout_slide_1_form is not defined
对不起,如果这有点混乱,基本上简短的故事是我希望能够在一个事件中提交两个表单,所以我的想法是保存第一个表单并与第二个一起提交。
谢谢,如果您需要其他任何东西,请告诉我。
编辑
好吧,基本上 musa 在下面给了我这段代码
function uploadFile(){
var file = _("new_shout_upload_pic").files[0];
var formdata = new FormData($("#new_shout_form")[0]);// add new_shout_form fields to the formdata object
formdata.append("new_shout_upload_pic", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "scripts/dashboard/dash_new_shout_upload.php");
ajax.send(formdata);
}
这显然会更好,因为它将new_shout_form 数据与上传的文件一起发送。问题是我似乎无法访问 php 脚本中的 new_shout_form 字段,我可以访问并获取文件,例如 $fileName = $_FILES["new_shout_upload_pic"]["name"]; 但是,我不确定如何获取 new_shout_form 中的字段成变量。我试过$new_shout_text = $_FILES["dash_new_shout_location"]; 和$new_shout_text = $_POST["dash_new_shout_location"]; 但是我得到错误Undefined index: dash_new_shout_location 有什么想法吗?
编辑 2
这是对 Musa 最近评论的编辑,这里有两种形式,第一种是用户使用文本输入提交的第一种,第二种是文件。
第一个表单,提交时将textarea div内容设置为隐藏输入,然后显示第二个表单供用户选择文件/图像
<form id="new_shout_form">
<div class="dash_new_shout_content">
<div id="dash_new_shout_textarea" name="dash_new_shout_textarea" class="dash_new_shout_textarea" contenteditable="true" placeholder="Write your shout..."></div>
<input id="hiddenNewShoutText" name="hiddenNewShoutText" type="hidden"></input>
</div><!--end dash_new_shout_content-->
<div class="dash_new_shout_options">
<input name="new_shout_next" type="button" id="new_shout_next" class="new_shout_finish" value="Next" alt="Next" />
<div class="dash_new_shout_cancel" id="new_shout_cancel">Cancel</div><!--end dash_new_shout_cancel-->
</div><!--end dash_new_shout_options-->
</form>
带有文件上传的表格 2,当提交此文件时,我希望它发送表格 1 的输入。
<form id="new_shout_2_form" enctype="multipart/form-data" method="post">
<div class="dash_new_shout_content">
<div id="dash_new_shout_new_pic">
<img id="new_shout_img" src="#" class="new_shout_img" width="100%" />
</div><!--end dash_new_shout_new_pic-->
<div class="dash_new_shout_content_option_pic">
<div class="dash_new_shout_pic_file_upload_wrapper">
<input name="dash_new_shout_pic_name" id="new_shout_upload_pic" type="file" /><span id="dash_new_shout_pic_file_upload_span">Upload from Computer</span>
</div><!--end dash_new_shout_pic_file_upload_wrapper-->
</div><!--end dash_new_shout_content_option-->
</div><!--end dash_new_shout_content-->
<br style="clear: both">
<progress id="new_shout_image_progress_bar" value="0" max="100" style="width:80%;"></progress>
<div id="progress_status">0%</div>
<div id="new_shout_image_status"></div>
<div class="dash_new_shout_options">
<input name="new_shout_finish" type="button" id="new_shout_finish" onclick="uploadFile()" class="new_shout_finish" value="Finish" alt="Finish" />
<div class="dash_new_shout_cancel" id="new_shout_back">Back</div><!--end dash_new_shout_cancel-->
</div><!--end dash_new_shout_options-->
</form><!--end new_shout_2_form-->
【问题讨论】:
-
为什么不直接将第二种形式的变量附加到第一种形式而不是创建一个新形式?
-
我该怎么做,我不知道你可以追加两次
-
你检查文件是否真的在上传吗?阅读此stackoverflow.com/questions/21271060/… 还将您的 enctype 指定为 multipart/form-data
-
是的,文件上传正常,问题是我似乎无法以其他形式获取文本输入中的数据
-
您在
new_shout_form中没有名为dash_new_shout_location的字段,只有hiddenNewShoutText您是要改用它吗?
标签: javascript php jquery html ajax