We help IT Professionals succeed at work.
Get Started

Help with jquery ajax call with wordpress

flynny
flynny asked
on
436 Views
Last Modified: 2012-05-11
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

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE