8

我正在尝试使用照片提交表单。在用户加载他按下提交按钮的照片后,我希望表单暂停 10 秒,为这 10 秒制作一个进度条动画然后提交表单,你们能说我做错了什么吗,似乎没有10 秒后提交表单。这是代码:

HTML:

<form action="uploadpic.php" method="post" id="upload_form">
<input type="text" name="title" id="title">
<p id="title_p">Title</p>

<hr />

<input type="text" name="theme" id="picture_theme" size="40"/>
<p id="theme">Picture Theme<img src="../simages/info.gif" id="info" width="12" height="12" style="margin-left:10px;"></p>
<hr />

<div class="custom-upload">
    <input type="file" name="picture" id="true_pic" />
    <div class="fake-file">
        <input disabled="disabled" >
    </div>
</div>
<p id="upload_pic">Upload picture</p>​

<input type="submit" name="submit" id="submit" value="Upload" />
</form>

JAVASCRIPT:

form = document.getElementById("upload_form");
    size=1;
    form.onsubmit = function()
    {
        if (size < 10)
        {
            setTimeout(delayedSubmit,1000); 
        }
        return false;
    }
    function delayedSubmit() {
        size++;
            if (size<5)
            {
                setTimeout(delayedSubmit,1000);
                alert("Counting "+size);    
            }
            else
            {
                alert("Form submitted");
                form.submit();
            }
    }

PHP:

<?php

if ($_POST['submit'])
{
    $title = $_POST['title'];
    $theme = $_POST['picture_theme'];
    echo $title," ",$theme; 
}




 ?>

我可以告诉表单不会提交任何东西,因为 php 变量不会显示任何东西,然后页面不会加载。

4

2 回答 2

4

当表单有一个名称和/或 ID 为“提交”的按钮时,它将不再起作用(我的旧帖子是错误的)。

因此,您需要做的是更改按钮的名称/ID:

<input type="submit" name="submit-button" id="submit-button" value="Upload" />

请记住:您也需要更改 PHP:

if ($_POST['submit'])
{
    $title = $_POST['title'];
    $theme = $_POST['picture_theme'];
    echo $title," ",$theme; 
}

if ($_POST['submit-button'])
{
    $title = $_POST['title'];
    $theme = $_POST['picture_theme'];
    echo $title," ",$theme; 
}
于 2012-12-20T09:52:22.103 回答
1

我会简化javascript:

form = document.getElementById("upload_form");
size=0;

form.onsubmit = delayedSubmit;

function delayedSubmit () {
    if (++size < 5) {
        alert("Counting "+size);    
        setTimeout(delayedSubmit,1000);
        return false;
    } 
    alert("Form submitted");
    form.submit();
}

并且 - 当然 - 从提交按钮中删除(或更改)id 和名称,例如:

<input type="submit" value="Upload" />

例如:http: //jsbin.com/equyit/1/edit

于 2012-12-20T10:15:45.853 回答