jQuery的动态表单,和PHP $ _FILES空 [英] jQuery dynamic Form, and the PHP $_FILES empty
本文介绍了jQuery的动态表单,和PHP $ _FILES空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要动态地创建jQuery的HTML表单,当我点击提交按钮,然后我送的形式DATAS通过Ajax的'add_sw.php',这是处理它们。
I need to create dynamically an HTML form in jQuery, and when i click the submit button, then i send the form datas via Ajax to the 'add_sw.php' which is process them.
但我的问题是,这个PHP脚本不能访问PHP $ _FILES变量,所以是空的。
But my problem is that this PHP script cant access the PHP $_FILES variable, so that is empty..
下面是JS相关的js codeS:
Here is the js relevant js codes:
<script type="text/javascript">
$("body").on("click", "#submit", function() {
var frm = $('#sw_add');
frm.submit(function (ev) {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('ok');
}
});
ev.preventDefault();
});
});
</script>
<script type="text/javascript">
$("body").on("change", "select", function() {
var tag = $("<div></div>");
var content = "<form id='sw_add' action='add_sw.php' method='post' enctype='multipart/form-data'>";
content += "<input type='text' name='sw_name'>";
content += "<label for='file'>Fájl:</label>";
content += "<input type='file' name='file' size='40'>";
content += "<textarea rows='4' cols='50' name='sw_comment'></textarea>";
content += "<div class='buttons'><button id='submit' type='submit'>Save</button></div>";
content += "</form>";
tag.html(content).dialog({title:'Add new software', modal:false, width:500, height:360}).dialog('open');
});
</script>
和开始的PHP脚本:
<?php
$sw_name = $_REQUEST['sw_name'];
$sw_file = $_FILES['file']['name'];
$sw_comment = $_REQUEST['sw_comment'];
// process the form datas...
?>
我的问题是,'$ sw_file'变量是空的,我没有得到上传的文件名。为什么呢?
My problem is that the '$sw_file' variable is empty, i do not get the uploaded file name. Why?
推荐答案
我修改了第一个js脚本,这是一个工作版本。
I modified the first js script, and this is a working version.
<script type="text/javascript">
$("body").on("click", "#submit", function() {
$("#sw_add").submit(function(e) {
var formObj = $(this);
var formURL = formObj.attr("action");
var formData = new FormData(this);
$.ajax({
url: formURL,
type: 'POST',
data: formData,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
success: function(data, textStatus, jqXHR) {
alert('ok');
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error');
}
});
e.preventDefault();
e.unbind();
});
});
</script>
这篇关于jQuery的动态表单,和PHP $ _FILES空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文