将数据提交到Google脚本并让用户访问我的网站(验证冲突)

时间:2017-08-06 04:31:32

标签: javascript php jquery ajax forms

我有一个表单,可以将数据提交给Google脚本。目前,我的用户在提交表单时会被发送到Google的页面。我尝试添加一个ajax脚本,以便在提交后将它们保留在页面上,但是当我这样做时,我的验证脚本不起作用。当我尝试将它们组合时,脚本都不起作用。其中一个问题是" post" url是使用php脚本决定的(由于谷歌的限制)以下是我的代码,任何帮助将不胜感激。谢谢!


向Google提交信息并将用户留在我的页面上:



$('#agentForm').submit(function(e){
    e.preventDefault();
    $.ajax({
        url:'<?php echo $actionURLs[$counter]; ?>',
        type:'post',
        data:$('#agentForm').serialize(),
        complete:function(){
            //whatever you wanna do after the form is successfully submitted
			 window.location = "agents.php?agentID=<?php echo $_GET['agentID']; ?>&email=<?php echo $_GET['email']; ?>&action=submitted";
        }
    });
});
&#13;
&#13;
&#13;


验证

&#13;
&#13;
// Wait for the DOM to be ready
function validateForm()
{
	var pax = jQuery('input[name="passengers"]:checked').length > 0;
	var rph = jQuery('input[name="reservation"]:checked').length > 0;
	
	var validationPassed = true;
	var msg = '';
	//console.log(pax);
	//console.log(rph);
	//console.log(jQuery('#mco').val());
	//console.log(jQuery("input:radio[name='flights']").is(":checked"));
	//console.log(jQuery("input:radio[name='iscorrect']").is(":checked"));
	
	if(!pax){
		validationPassed = false;
		msg +='Please select at least one passenger.</br>';
	}
		
	if(!rph){
		validationPassed = false;
		msg +='Please select at least one segment.</br>';
	}
		
	if(jQuery('#mco').val() != '' && !jQuery.isNumeric(jQuery('#mco').val())){
		validationPassed = false;
		msg +='MCO Amount must be a numeric value.</br>';
	}
	
	if (!jQuery("input:radio[name='flights']").is(":checked")){
		validationPassed = false;
		msg +='Are all flights being flown?</br>';
	}
		
	if (!jQuery("input:radio[name='iscorrect']").is(":checked")){
		validationPassed = false;
		msg +='Is the total correct?</br>';
	}
	else if(jQuery('input[name=iscorrect]:checked').val() == 'INCORRECT' && jQuery('#correct_amount').val() == ''){
		validationPassed = false;
		msg +='Please specifiy the correct amount.</br>';
	}
	else if(jQuery('input[name=iscorrect]:checked').val() == 'INCORRECT' && jQuery('#correct_amount').val() != '' && !jQuery.isNumeric(jQuery('#correct_amount').val())){
		msg +='Correct amount must be a numeric value.</br>';
	}
	
	if(!validationPassed){ 
		jQuery('.errors').show();
		jQuery(window).scrollTop(jQuery('.errors').offset().top);
	}
	
	jQuery('.errors').html(msg);
	return validationPassed;
}

jQuery( document ).ready(function() {
	jQuery("input[name='iscorrect']").click(function(){
		jQuery('#correct_amount').val('');
		/*if(jQuery('input[name=iscorrect]:checked').val() == 'INCORRECT'){
			
			jQuery("#correct_amount").prop("readonly", false);
		}
		else{
			jQuery('#correct_amount').val('');
			jQuery("#correct_amount").prop("readonly", true);
		}*/
	});
	
	jQuery("input[name='correct_amount']").click(function(){
		jQuery('#INCORRECT').prop('checked', true);
	});
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我认为您的验证无效,因为您已使用e.preventDefault()停止在JavaScript第一个代码中传播事件。 尝试直接在第一个提交事件监听器中调用validateForm(),如下所示:

$('#agentForm').submit(function(e){
    validateForm();
    e.preventDefault();
    $.ajax({
        url:'<?php echo $actionURLs[$counter]; ?>',
        type:'post',
        data:$('#agentForm').serialize(),
        complete:function(){
            //whatever you wanna do after the form is successfully submitted
             window.location = "agents.php?agentID=<?php echo $_GET['agentID']; ?>&email=<?php echo $_GET['email']; ?>&action=submitted";
        }
    });
});