【发布时间】:2011-09-13 23:30:15
【问题描述】:
我创建了一个 jQuery 脚本来进行一些表单验证(即注册用户、登录用户、创建项目/相册等),它与 PHP 中的 $_POST 函数配合得很好。我正在尝试创建一个上传表单来将文件上传到特定的相册,而 jQuery 没有响应。代码如下:
形式:
<?PHP
include 'init.php';
if (!isset ($_SESSION['user_id'])){
header('Location: index.php');
exit();
}
if (!logged_in()){
header('Location: ../index.php');
exit();
}
$shots = get_shots();
?>
<head>
<title></title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script src="js/jquery_1_6_2.js" type="text/javascript"></script>
<script src="js/jfunc.js" type="text/javascript"></script>
</head>
<div class="grid_1_head"> Upload File <span class="right_shots"><a id="close-panel" href="#"class="create_new_shot">Close this window</a></span></div>
<div class="hover_1_body">
<p> Select the file you would like to upload then click the Upload File Button. </p>
<div class="project_form_text">
<div class="hover_1_error">
<div class="message_error"></div>
<div class="project_create_success"></div>
</div>
<div class="form_left">
<form id="upload_file" action="widgets/upload_file_process.php" method="post" enctype="multipart/form-data">
<p>
<label for="selected_file">Choose a File:</label>
<input id="selected_file" name="selected_file" type="file" class="field_boarder" value="" /> <br />
Choose a shot:
<select name="shot_id" class="select_style">
<?php
foreach ($shots as $shot){
echo '<option value="', $shot['shot_id'], '">', $shot['shot_name'], ' </option>';
}
?>
</select>
</p>
<div class="login_button_container">
<input name="upload_file" type="submit" class="login_button" value="Upload File"/>
</div>
</form>
</div>
</div>
这是处理表单的 PHP:
<?php
include '../init.php';
if (!logged_in()){
header('Location: ../index.php');
exit();
}
if (isset($_FILES['selected_file'], $_POST['shot_id'])){
$file_name = $_FILES['selected_file']['name'];
$file_size = $_FILES['selected_file']['size'];
$file_temp = $_FILES['selected_file']['tmp_name'];
$allowed_ext = array('mov','r3d','avi','mp4','wmv','ogg','webm', //Video Files
'mp3','aif','aiff','wav','ac3', //Audio Files
'gif','jpg','jpeg','png','pic','pict','psd','bmp','dpx','raw','jpf','pcx','tga', // Still bitmap files and Sequences
'tif','tiff','hdr','exr', 'picio','rla','rpf','sgi','iff','cin','als',
'ai','svg','svgz','eps', // Still Vector files
'fcp','color','edl','mxf','aaf','omf','xml', // Interchange formats
'pdf','xls','doc','txt','ppt','xlsx','docx','rtf'); // Documents
$file_ext = strtolower(end(explode('.', $file_name)));
$shot_id = $_POST['shot_id'];
$errors = array();
if (empty($file_name) || empty($shot_id)){
$file_exists_check = 'invalid';
}else {
$file_exists_check = 'valid';
}
if (in_array($file_ext, $allowed_ext) === false){
$file_type_check = 'invalid';
}else {
$file_type_check = 'valid';
}
if ($file_size > 2097152000) {
$file_size_check = 'invalid';
}else {
$file_size_check = 'valid';
}
if (shot_check($shot_id) === true){
$shot_exists_check = 'invalid';
}else {
$shot_exists_check = 'valid';
}
$errors["file_exists_check"] = $file_exists_check;
$errors["file_type_check"] = $file_type_check;
$errors["file_size_check"] = $file_size_check;
$errors["shot_exists_check"] = $shot_exists_check;
echo json_encode($errors);
}
?>
这是需要提醒用户出现问题并完成表单的 JQuery 功能:
// UPLOAD FILE VALIDATION
$(function(){
$("#upload_file").submit(function(e){
// e.preventDefault();
$.post("widgets/upload_file_process.php", $("#upload_file").serialize(),
function(data){
if(data.file_exists_check == 'invalid'){
$('div.message_error').hide();
$('div.project_create_success').hide();
$('div.message_error').fadeIn();
$('div.message_error').html("<div>You need to select a file.</div>");
} else if (data.file_type_check == 'invalid'){
$('div.message_error').hide();
$('div.project_create_success').hide();
$('div.message_error').fadeIn();
$('div.message_error').html("<div>File type not supported</div>");
} else if (data.file_size_check == 'invalid'){
$('div.message_error').hide();
$('div.project_create_success').hide();
$('div.message_error').fadeIn();
$('div.message_error').html("<div>File must be less than 2GB</div>");
}else if (data.shot_exists_check == 'invalid'){
$('div.message_error').hide();
$('div.project_create_success').hide();
$('div.message_error').fadeIn();
$('div.message_error').html("<div>That shot does not exist</div>");
}else {
$('div.message_error').hide();
$('div.project_create_success').fadeIn();
$('div.project_create_success').html("<div>File Uploading </div>");
$(function(){
$('#lightbox_content').delay(300).fadeOut(300 , function(){
$(this).remove();
});
$('#lightbox_bg').delay(300).fadeOut(300, function(){
$(this).remove();
window.location.replace("producer.php");
});
});
return false;
}
}, "json");
});
});
这与我用于注册用户的功能几乎相同,效果很好。 (对我而言)唯一的新功能是使用$_FILES 获取文件信息。使用 jQuery 有问题吗?
提前谢谢你。
【问题讨论】:
-
什么时候会变得无响应?当您调试它时,系统是否有特定的线路挂起?那条线上的对象的状态是什么?
-
如果我在查询功能之前放置一个警报并注释掉阻止默认值,警报将在单击提交按钮时触发,这很奇怪。如果我将 php 处理页面的路径放在表单操作中并注释掉阻止默认值,则页面将处理并返回有效的 JASON。如果我将警报放在 jquery 函数(数据)中,它将不会触发。
标签: php jquery ajax file-upload