普通网友 2017-08-02 07:33
浏览 40

进度条达到100%时未提交的表单

I attached, below, the simplified version of my php. I have a from with multiple input field + upload file option.

When the form is submitted the upload bar fires up and when it reaches 100% I see the “success” alert and then the “complete” alert. (twice???) However neither the form “action=sendEmail.php” is triggered not the “url: sendEmail.php” – I thought of adding both to see if any is triggered. A note, when I remove the ajaxFrom triggers “sendEmail.php” is carried out.

So what am I doing wrong? What should change to make the “sendEmail.php” carried out. Also why do I see the success & completer alert twice? Success => complete => success => complete

The basic form:

 <form id="formmail" class="form-horizontal" name="formmail" method="POST" action="sendEmail.php" enctype="multipart/form-data">
.
.
.

I tried (separately of course) both with a submit button:

<input class="btn btn-sm" name='submit' type='submit' id='submit' tabindex='100' value="Submit"/>

And a simple button:

<button class="btn btn-sm" name='submit' id='submit'>Submit</button>

The javascript:

var progress
$(document).ready(function(){   
    $("#submit").click(function(){
        var form_data = $("#formmail").serialize();
        $("#formmail").ajaxForm({
            type: "POST",
            url: "sendEmail.php",
            data: form_data,
            beforeSend: function() {
                $("#progressBar").removeClass("hidden");
                progress = "0%";
                $('.progress-bar').css("width", progress);
                $('.progress-bar').html(progress);
            },
            uploadProgress: function (event, position, total, percentComplete) {
                progress = percentComplete + "%";
                $('.progress-bar').css("width", progress);
                $('.progress-bar').html(progress);  
            },
            success: function (data){
                alert("success");
            },
            complete: function(xhr){
                if(xhr.responseText) {
                    alert("complete");
                }
            },
        })
        .submit();
    });
});
  • 写回答

1条回答 默认 最新

  • duangekui7451 2017-08-02 07:37
    关注

    Your code has a very common problem. By default, Submit button (<input type="submit">) will submit the form automatically. Binding a click() function with jQuery won't stop the form from submitting. Therefore, for every click on submit button, sendEmail.php might be called twice, one by HTML form submit and one by JS. You probably want one of them only.

    You can either change the type of the button to type="button", or add event.preventDefault() in your click() function.

    By the way, here are some recommendations for your codes:

    1. for HTML tidiness, do not mix single quotes & double quotes in HTML structure. Also, remove unused class and id attributes.
    2. use lowercase filenames only. uppercase in URL is not friendly for users.
    评论

报告相同问题?

悬赏问题

  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序