Javascript表单提交

时间:2013-06-10 15:43:51

标签: javascript forms sugarcrm

我有一个将数据发布到Sugar CRM的表单。数据已在Firefox中成功提交,但未正确重定向。在Chrome中,数据未提交,但正确重定向。

<form method="POST" class="brochurelead webtoleadform" name="WebToLeadForm" id="WebToLeadForm">    
  <fieldset>
    <label for="firstname">First name*:</label>
    <input type="text" name="first_name" id="first_name" />
  <br />
    <label for="lastname">Last name*:</label>
    <input type="text" name="last_name" id="last_name" />
  <br />
    <label for="phone_work">Primary phone*:</label>
    <input type="text" name="phone_work" id="phone_work" onchange="validateHuman();"/>
  <br />
  <label for="email">Email*:</label>
  <input type="text" name="email1" id="email1" onchange="validateEmailAdd();" />
  <span style="padding-left:268px"><input type="submit" value="Download" name="Submit" class="button" onclick="submit_form();" />
</fieldset1>

  <input type="hidden" name="lead_source" id="lead_source" value="Brochure Download" />
  <input type="hidden" name="user" id="user" value="lead_capture_form" />
  <input type="hidden" value="dbce057c" name="campaign_id" id="campaign_id" />
  <input type="hidden" value="http://www.somewebsite.com/somearticle" name="redirect" id="redirect" />
  <input type="hidden" value="blahblah" name="assigned_user_id" id="assigned_user_id" />
  <input type="hidden" value="first_name;last_name;phone_work;email1;" name="req_id" id="req_id" />
  <input type="hidden" value="<?php echo $article->category; ?>" name="area_of_interest_c" id="area_of_interest_c">
  <input type="hidden" name="probability_c" id="probability_c" value="1" />
  <input type="hidden" id="human" name="human" value="0">
</form>

这是我处理表单的javascript:

function submit_form(){
    if(typeof(validateCaptchaAndSubmit)!='undefined'){
        validateCaptchaAndSubmit();
    }else{
        check_webtolead_fields();
    }
}

function check_webtolead_fields(){
    if(document.getElementById('req_id') != null){
        var reqs=document.getElementById('req_id').value;
        reqs = reqs.substring(0,reqs.lastIndexOf(';'));
        var req_fields = new Array();
        var req_fields = reqs.split(';');
        nbr_fields = req_fields.length;
        var req = true;
        for(var i=0;i<nbr_fields;i++){
            if(document.getElementById(req_fields[i]).value.length <=0 || document.getElementById(req_fields[i]).value==0){
                req = false;
                break;
            }
        }
        if(req && document.getElementById('human').value == '50'){
            document.WebToLeadForm.action = "http://crm.somewebsite.com/index.php?entryPoint=WebToLeadCapture";
            document.WebToLeadForm.submit();
            window.location = document.getElementById('redirect').value;
            return true;
        }
        else{
            alert('Please provide all the required fields');
            return false;
        }
        return false
    }
    else{
        document.WebToLeadForm.action = "http://crm.somewebsite.com/index.php?entryPoint=WebToLeadCapture";
        document.WebToLeadForm.submit();
        window.location = document.getElementById('redirect').value;
    }
}

function validateEmailAdd(){
    if(document.getElementById('email1').value.length >0) {
        if(document.getElementById('email1').value.match(/^\w+(['\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/) == null){
            alert('Not a valid email address');
        }
    }
}

function validateHuman(){
    document.getElementById('human').value = "50";
}

1 个答案:

答案 0 :(得分:2)

因为您正在提交表单并尝试同时设置页面的位置。这不会发生。这是竞争条件!

您必须在重定向的响应上提交带有Ajax的表单,或者您需要服务器端实际进行重定向!