2011-09-13 6 views
0

PHPで$_POST関数を使用して正常に動作していたため、フォーム検証(登録ユーザー、ログインユーザー、プロジェクト/アルバムの作成など)を行うjQueryスクリプトを作成しました。私は特定のアルバムにファイルをアップロードするアップロードフォームを作成しようとしています.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 IS:

<?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);  

} 


?> 

これは問題やFINISHのユーザーに警告する必要が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"); 

    }); 

}); 

これは私がユーザーを登録するための機能とほとんど同じですtはうまく動作します。新しい(私にとって)唯一のことは、$_FILESを使ってファイル情報を取得することです。 jQueryでそれを使用する際に問題がありますか?

ありがとうございます。

+1

どの時点で応答しなくなるのですか?デバッグするときに、システムが停止する特定の行がありますか?その行のオブジェクトの状態は何ですか? – David

+0

私はクエリ機能の前にアラートを入れ、送信ボタンをクリックしたときにアラートがトリガーされるデフォルトを防ぐことができます。フォーム処理でPHP処理ページへのパスを入れ、デフォルトを防止すると、ページは処理され、有効なJASONを返します。 jquery関数(データ)の中にアラートを置くと、トリガーされません。 –

答えて

1

xmlhttprequestを使用してファイルをアップロードすることはできません。
非同期にするには、フレーム/ iframe(私とGoogleより優先)/ Flash/Javaを使用してください。

+0

私は分かりません。私はPHPのアップロードを作成するためのチュートリアルに従っていると彼らはうまく動作しますが、私は何をしたいかをフォームを検証するためにjava/jqueryを使用していません。 –

+0

@Craig:あなたが参照しているチュートリアルへのリンクを提供できますか? – Herbert

+0

www.phpacademy.org画像アップロードサイトのチュートリアルです。ファイルのアップロードセクションは12または15の動画です。 –

0

AJAXでファイルをアップロードするのは複雑な作業です。これは参考になるかもしれません:$(form).serialize() Workaround for File Fields。それはjQuery Form Pluginを使用することを示唆しています。

これは’が最高です。あなたは’となるでしょう。私’申し訳ありません私はcouldnt ’より多くの助けてください。

あなたに幸運。

0

あなたはjqueryのAJAXを使用してみませんなぜ、それはあなたのuploadDocument.php、あなたができるだけますprint_r($ _ FILES)で

function uploadDocument() { 
     var formArray = new FormData($('#upload_file')[0]); 
     $.ajax({ 
      url: "<?php echo Router::url('/uploadDocument'); ?>", 
      data: formArray, 
      type: "POST", 
      dataType: "json", 
      success: function(data){ 
      //do something upon success 
      } 
     }); 
    } 
$('#upload_file').live('submit',function(){ 
var file = $('#selected_file')[0].files[0]; 
//run validation 
uploadDocument(); 
return false; 
}) 

はるかに簡単です。 私はこれを試していませんが、理論的にはうまくいくはずです。

関連する問題