Page 1 of 1

Sending PHP Email issue with modal window

Posted: Tue Jun 29, 2010 12:18 pm
by antonio2396
Hi PHP Gurus!

I am having an issue with a modal window/php email.

Here is the basic premise. I have a simple "Contact Us" link that brings up a modal window. The windows pops up nicely however when I hit submit and send the data to the "contactus.php" page that does all the emailing, nothing happens.

here is the javascript that does the work in the modul window:

Code: Select all

$(function() {
	$('.error').hide();
	$('#gears').hide();
	$("#submit").click(function() {
		// validate
		
		$('.error').hide();
		var name = $('input#name').val();
		if(name == "") {
			$('p.name').append('<span class="extra error">Please provide your name.</span>');
			$('input#name').focus();
			return false;
		}
		
		var email = $('input#email').val();
		if(email == "") {
			$('p.email').append('<span class="extra error">Please provide your e-mail.</span>');
			$('input#email').focus();
			return false;
		}
		
		var subjectbody = $('input#subjectbody').val();
		if(subjectbody == "") {
			$('p.subjectbody').append('<span class="extra error">Please provide a subject.</span>');
			$('input#subjectbody').focus();
			return false;
		}	
		
		var message = $('textarea#message').val();
		if(message == "") {
			$('p.message').append('<p class="error" style="margin:10px 0px 0px 165px;">Please provide a message.</p>');
			$('input#message').focus();
			return false;
		}
		
		var dataString = 'name='+ name + '&email=' + email + '&subjectbody=' + subjectbody + '&message=' + message;
		
		$('#contact_form').slideUp(500);
		$('#gears').animate({opacity:1.0}, 510).fadeIn(1000);
				
	[b][color=#BF0000]	$.ajax({
			type: "POST",
			url: "www.myurl.com/contact_us-modul.php",
			data: dataString,
			success: function() {
				$('#gears').fadeOut(500);
				
				$('#contact_form').animate({opacity: 1.0}, 600).fadeIn(2000).html('<p style="text-align:center; margin-top:120px;"><strong>Your message has been submitted.</strong> You should receive a response within 24 hours.');
			}
		});[/color][/b]
		
		return false;
		
	});
});
-----------------------------------------------------------------------------------------------------------------------------------------------

Here is the page that does all the back end work (inserts in table and sends email) - THIS IS THE PAGE THAT DOES NOT WORK PROPERLY!!

Code: Select all

<?php
//include the connection file

require_once('connection.php');

//save the data on the DB and send the email


	//recieve the variables
	
	$name = $_POST['name'];
	$email = $_POST['email'];
	$subjectbody = $_POST['subjectbody'];
	$message = $_POST['message'];
	$ip = gethostbyname($_SERVER['REMOTE_ADDR']);
	
	//save the data on the DB
	
	mysql_select_db($database_connection, $connection);
	
	$insert_query = sprintf("INSERT INTO contacts (name, email, subjectbody, message, date, ip) VALUES (%s, %s, %s, %s, NOW(), %s)",
							sanitize($name, "text"),
							sanitize($email, "text"),
							sanitize($subjectbody, "text"),
							sanitize($message, "text"),
                                               		sanitize($ip, "text"));
	
	$result = mysql_query($insert_query, $connection) or die(mysql_error());
	
	if($result)
	{
		//send the email
		
		$to = "customerservice@mysite.com";
		$subject = "New contact from the website";
		
		//headers and subject
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
		$headers .= "From: ".$name." <".$email.">\r\n";
		
		$body = "New contact<br />";
		$body .= "First Name: ".$name."<br />";
		$body .= "Subject: ".$subjectbody."<br />";
		$body .= "Email: ".$email."<br />";
		$body .= "Message: ".$message."<br />";
		$body .= "IP: ".$ip."<br />";
		
		mail($to, $subject, $body, $headers);
		
		//ok message
		
		echo "<font color=red>Your message has been sent</font>";
	}


function sanitize($value, $type) 
{
  $value = (!get_magic_quotes_gpc()) ? addslashes($value) : $value;

  switch ($type) {
    case "text":
      $value = ($value != "") ? "'" . $value . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $value = ($value != "") ? intval($value) : "NULL";
      break;
    case "double":
      $value = ($value != "") ? "'" . doubleval($value) . "'" : "NULL";
      break;
    case "date":
      $value = ($value != "") ? "'" . $value . "'" : "NULL";
      break;
  }
  
  return $value;
}
?>

ANY assistance would be great appreciated!

Thank you in advance.

Regards,

Ashish Vohra