文件上传后如何显示隐藏的输入值

时间:2013-01-20 16:16:59

标签: php javascript jquery html ajax

 var $fileVideo = $("<form action='videoupload.php' method='post' enctype='multipart/form-data' target='upload_target_video' onsubmit='return videoClickHandler(this);' class='videouploadform' >" + 
    "Video File: <input name='fileVideo' type='file' class='fileVideo' /></label>" +  
    "<input type='submit' name='submitVideoBtn' class='sbtnvideo' value='Upload' /></label>" + 
     "<p class='listVideo' align='left'></p>" +
    "<iframe class='upload_target_video' name='upload_target_video' src='/' style='width:0px;height:0px;border:0px;solid;#fff;'></iframe></form>"); 

现在,我有一个jquery代码,在文件上传完成后会触发。

  function htmlEncode(value) { return $('<div/>').text(value).html(); }

      function stopVideoUpload(success, videofilename){

          var result = '';
          videocounter++;

          if (success == 1){
             result = '<span class="videomsg'+videocounter+'">The file was uploaded successfully</span>';
              $('.listVideo').eq(window.lastUploadVideoIndex).append('<div>' + htmlEncode(videofilename));
          }


          return true;   
    }

下面是上传文件的代码,然后使用javascript从数据库中接收文件的名称和文件的id,它会将其发回上面的javascript函数以便能够显示信息:

 move_uploaded_file($_FILES["fileVideo"]["tmp_name"],
      "VideoFiles/" . $_FILES["fileVideo"]["name"]);
      $result = 1;

    $videosql = "INSERT INTO Video (VideoFile) 
    VALUES (?)";

        if (!$insert = $mysqli->prepare($videosql)) {
      // Handle errors with prepare operation here
    }

    //Assign the variable
$vid = 'VideoFiles/'.$_FILES['fileVideo']['name'];

            //Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$vid);

 $insert->execute();
 $id = $mysqli->insert_id;

        if ($insert->errno) {
          // Handle query error here
        }

        $insert->close();     

        }else{
  echo "Upload was not successful";   
  }


     <script language="javascript" type="text/javascript">
     window.top.stopVideoUpload(<?php echo $result; ?>,'<?php echo $id . $_FILES['fileVideo']['name'] ?>');
     </script> 

因此,如果上传文件VideoFiles/tulips.png,那么在db中它看起来像这样:

视频表:

ImageId  VideoFile
23       VideoFiles/tulips.mp4

所以在上面的javascript函数中显示:

23ImageFile/tulips.mp4

我的问题是,我希望$id或上面23中的示例显示为隐藏的输入值。但我的问题是如何做到这一点?

更新

  function htmlEncode(value) { return $('<div/>').text(value).html(); }

      function stopVideoUpload(success, videofilename, videoID){

          var result = '';
          videocounter++;

          if (success == 1){
             result = '<span class="videomsg'+videocounter+'">The file was uploaded successfully</span>';
      $('.listVideo').eq(window.lastUploadVideoIndex).append('<input type="hidden" name="vidid" value="videoID" />');
              $('.listVideo').eq(window.lastUploadVideoIndex).append('<div>' + htmlEncode(videofilename));

          }


          return true;   
    }

..........

 move_uploaded_file($_FILES["fileVideo"]["tmp_name"],
          "VideoFiles/" . $_FILES["fileVideo"]["name"]);
          $result = 1;

        $videosql = "INSERT INTO Video (VideoFile) 
        VALUES (?)";

            if (!$insert = $mysqli->prepare($videosql)) {
          // Handle errors with prepare operation here
        }

        //Assign the variable
    $vid = 'VideoFiles/'.$_FILES['fileVideo']['name'];

                //Dont pass data directly to bind_param store it in a variable
    $insert->bind_param("s",$vid);

     $insert->execute();
     $id = $mysqli->insert_id;

            if ($insert->errno) {
              // Handle query error here
            }

            $insert->close();     

            }else{
      echo "Upload was not successful";   
      }


 <script language="javascript" type="text/javascript">
 window.top.stopVideoUpload(<?php echo $result; ?>,'<?php echo $id; ?>','<?php echo $_FILES['fileVideo']['name']; ?>');
 </script>  

1 个答案:

答案 0 :(得分:1)

“显示”是什么意思?我想你想要的是将隐藏<input />字段的值设置为"23"。 如果我是对的,您只需将stopVideoUpload()更改为3个参数:successvideofilenameID(或您想要的其他名称)。然后,将<input type="hidden" name="(name you want)" value="(ID you received)" />附加到$('.listVideo')或您想要的其他容器中。

如果你已经有一个隐藏字段,只需使用jQuery的val()函数来设置值=)

http://api.jquery.com/val/