Avatar of flynny
flynny
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Help with jquery ajax call with wordpress

Hi all,

I am struggling to get my PHP file firing form an jquery ajax call?

I have the following attached code.

Now the first test alert is firing, but the second isn't its not even giving any kind of error??

Any ideas guys


<script type="text/javascript">
	var name;
	var email;
	var telephone;
	var enquiry;
	var allow;
	
	$(function(){
		$('a#scrollLink').click(function(event){
			var clicked = $(this).attr('href');
			var destination = $(clicked).offset().top;
			$('html:not(:animated), body:not(:animated)').animate({ scrollTop: destination-40}, 400 );
			event.preventDefault();
		});
		
		$('#subNav').hide();
		$('#servicesNav').click(function(event){
			event.preventDefault();
			});
		$('#servicesNav').parent().hover(function(){
			$('#subNav').show();
			}, function(){
			$('#subNav').fadeOut('slow');
			});
		
		$('.error').hide();
		
		$('#contactForm form').submit(function(event){
				$('.error').hide();
				$('.row .textInput').css('border-color', '#dfdbdb');
				allow=true;
				$('form .row').each(function(){
					validate($(this));	
				});
				
				if(allow){

//alert($('#contactForm form').serialize());

alert('TEST 1');
						$.post('<?php bloginfo('template_url'); ?>/sendemail.php', $('#contactForm form').serialize(), function(response){

alert('TEST 2');
						//deal with the response from the ajax request
						if(response.result){

alert(response.result);
							$('#contactForm').before("<div class='alpha grid_8' id='thanksDiv'>Thank you! Your email has been received</div>").fadeOut('fast');
							$('#thanksDiv').hide().delay('200').fadeIn('slow');


						}
						else{
alert('HERE!!');
							alert('There was en error sending the form:' + response.result + ':' );
							console.log("error with sending email");
						}
						}, 'json');
				}
				else{
					$('form .row input').first().focus();	
				}
				event.preventDefault();
				
			})
		
	});
	
	function validate(jobj){
		var val = jobj.find('input').val();
		if(val == '' && jobj.find('.textInput')){
			jobj.find('.error').html('Required field').show();
			jobj.find('.textInput').css('border-color', '#a6373a');
			allow = false;
		}
		
		if(jobj.find('#emailInput').length && jobj.find('#emailInput').val() != ''){
			var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
			if(!emailPattern.test(jobj.find('#emailInput').val())){
				allow=false;
				jobj.find('.error').html('Invalid email').show();
				jobj.find('#emailInput').css('border-color', '#a6373a');
				allow=false;
			}
		}
		
		if(jobj.find('#telephoneInput').length && jobj.find('#telephoneInput').val() != ''){
			var phonePattern = /[\d ]{10,12}/;
			
			if(!phonePattern.test(jobj.find('#telephoneInput').val())){
				jobj.find('.error').html('Invalid number').show();
				jobj.find('#telephoneInput').css('border-color', '#a6373a');
				allow=false;
			}
		}
	}
</script>

Open in new window

AJAXJScript

Avatar of undefined
Last Comment
flynny

8/22/2022 - Mon