troubleshooting Question

Help with jquery ajax call with wordpress

Avatar of flynny
flynnyFlag for United Kingdom of Great Britain and Northern Ireland asked on
AJAXJScript
8 Comments1 Solution439 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
PimOnline

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros