如何使这个Javascript使用特定的操作表单?

时间:2013-10-18 18:43:14

标签: javascript forms action

我目前正在处理上传视频脚本的一个上传进度条。 因此,当我的表单块中有action="[var.path_to_upload_script]"时,我的视频上传工作正常。

所以,让我先粘贴我正在处理的所有代码。

以下是表单代码:

            <div>
            <form name="form_upload" id="form_upload" method="post" enctype="multipart/form-data"  action="[var.path_to_upload_script]" style="margin: 0px; padding: 0px;">
            <input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $up_id; ?>"/>
              <noscript><input type="hidden" name="no_script" value="1" /></noscript>
              <input type="hidden" name="title" value="[var.title]" />
        <input type="hidden" name="description" value="[var.description]" />
        <input type="hidden" name="tags" value="[var.tags]" />
        <input type="hidden" name="location_recorded" value="[var.location_recorded]" />
        <input type="hidden" name="allow_comments" value="[var.allow_comments]" />
        <input type="hidden" name="allow_embedding" value="[var.allow_embedding]" />
        <input type="hidden" name="public_private" value="[var.public_private]" />
        <input type="hidden" name="channel" value="[var.channel]" />
        <input type="hidden" name="channel_name" value="[var.channel_name]" />
        <input type="hidden" name="sub_cat" value="[var.sub_cat]" />
        <input type="hidden" name="form_submitted" value="yes" />

        <div id="upload_slots"><span class="font5_16"><b>[var.lang_please_upload]</b></span><input type="file" name="upfile_0" size="71" value="" /></div>
<br />
<iframe id="upload_frame" name="upload_frame" frameborder="0" border="0" src="" scrolling="no" scrollbar="no" > </iframe>
<br />
        <noscript><br><input type="reset" name="no_script_reset" value="Reset" />&nbsp;&nbsp;&nbsp;<input type="submit" name="no_script_submit" value="Upload" /></noscript>
        </form>
                    <script language="javascript" type="text/javascript">
            <!--
            document.writeln('<input type="button" name="reset_button" value="Reset" onClick="resetForm();">&nbsp;&nbsp;&nbsp;<input type="button" id="submit" name="submit" value="Upload" onClick="uploadFiles();">');
            //-->
            </script>
        </div>

这里重要的是在表格中阻止此action="[var.path_to_upload_script]",否则上传文件将无效。

这是进度条JAVASCRIPT代码:

    $('#upload_frame').show();
    function set () {
        $('#upload_frame').attr('src','upload_frame.php?up_id=<?php echo $up_id; ?>');
    }
    setTimeout(set);

来自upload_frame.php的代码

<?php

$url = basename($_SERVER['SCRIPT_FILENAME']);

//Get file upload progress information.
if(isset($_GET['progress_key'])) {
    $status = apc_fetch('upload_'.$_GET['progress_key']);
    echo $status['current']/$status['total']*100;
    die;
}
//

?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.js" type="text/javascript"></script>
<link href="style_progress.css" rel="stylesheet" type="text/css" />

<script>
$(document).ready(function() { 
//

    setInterval(function() 
        {
    $.get("<?php echo $url; ?>?progress_key=<?php echo $_GET['up_id']; ?>&randval="+ Math.random(), { 
        //get request to the current URL (upload_frame.php) which calls the code at the top of the page.  It checks the file's progress based on the file id "progress_key=" and returns the value with the function below:
    },
        function(data)  //return information back from jQuery's get request
            {
                $('#progress_container').fadeIn(100);   //fade in progress bar  
                $('#progress_bar').width(data +"%");    //set width of progress bar based on the $status value (set at the top of this page)
                $('#progress_completed').html(parseInt(data) +"%"); //display the % completed within the progress bar
            }
        )},500);    //Interval is set at 500 milliseconds (the progress bar will refresh every .5 seconds)

});


</script>

<body style="margin:0px">
<!--Progress bar divs-->
<div id="progress_container">
    <div id="progress_bar">
         <div id="progress_completed"></div>
    </div>
</div>
<!---->
</body>

现在,如果我从表单块中的操作中删除[var.path_to_upload_script],则进度条正常工作,但文件上载脚本失败。

所以我想问一下如何让进度条在表单标签中使用action="[var.path_to_upload_script]"

提前致谢!

1 个答案:

答案 0 :(得分:0)

你用php代码设置属性,但看起来它是某种模板:

$('#upload_frame').attr('src','upload_frame.php?up_id=<?php echo $up_id; ?>');

尝试更改为

$('#upload_frame').attr('src','upload_frame.php?up_id=[var.up_id]');

同样的问题可以在这里:<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $up_id; ?>"/>