0% found this document useful (0 votes)
9 views5 pages

Contact Handler - PHP

This PHP script handles AJAX requests for a contact form by sanitizing and validating input data. It sends an email with the form details and optionally stores the submission in a database. The script returns a JSON response indicating success or failure based on the email sending and database storage operations.

Uploaded by

tofikjem2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Contact Handler - PHP

This PHP script handles AJAX requests for a contact form by sanitizing and validating input data. It sends an email with the form details and optionally stores the submission in a database. The script returns a JSON response indicating success or failure based on the email sending and database storage operations.

Uploaded by

tofikjem2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

<?

php

// Set headers to handle AJAX requests

header('Content-Type: application/json');

// Get form data

$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);

$phone = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING);

$subject = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_STRING);

$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);

// Validate required fields

if (empty($name) || empty($email) || empty($subject) || empty($message)) {

echo json_encode([

'success' => false,

'message' => 'Please fill in all required fields.'

]);

exit;

// Validate email

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

echo json_encode([

'success' => false,

'message' => 'Please enter a valid email address.'


]);

exit;

// Prepare email content

$to = '[email protected]'; // Replace with your email

$email_subject = "Contact Form: $subject";

$email_body = "You have received a new message from your website contact form.\n\n";

$email_body .= "Name: $name\n";

$email_body .= "Email: $email\n";

$email_body .= "Phone: $phone\n";

$email_body .= "Subject: $subject\n\n";

$email_body .= "Message:\n$message\n";

$headers = "From: $email\n";

$headers .= "Reply-To: $email\n";

// Send email

$mail_success = mail($to, $email_subject, $email_body, $headers);

// Store in database (optional)

$db_success = storeContactInDatabase($name, $email, $phone, $subject, $message);

// Return response

if ($mail_success || $db_success) {
echo json_encode([

'success' => true,

'message' => 'Thank you for your message! We will get back to you soon.'

]);

} else {

echo json_encode([

'success' => false,

'message' => 'There was an error sending your message. Please try again later.'

]);

/**

* Store contact form submission in database

* @param string $name

* @param string $email

* @param string $phone

* @param string $subject

* @param string $message

* @return bool

*/

function storeContactInDatabase($name, $email, $phone, $subject, $message) {

// Database connection details

$host = 'localhost';

$db_username = 'db_username';
$db_password = 'db_password';

$db_name = 'radiology_db';

try {

// Create connection

$conn = new mysqli($host, $db_username, $db_password, $db_name);

// Check connection

if ($conn->connect_error) {

error_log("Connection failed: " . $conn->connect_error);

return false;

// Prepare statement

$stmt = $conn->prepare("INSERT INTO contact_messages (name, email, phone, subject, message,


created_at) VALUES (?, ?, ?, ?, ?, NOW())");

$stmt->bind_param("sssss", $name, $email, $phone, $subject, $message);

// Execute statement

$result = $stmt->execute();

// Close connection

$stmt->close();

$conn->close();

return $result;
} catch (Exception $e) {

error_log("Database error: " . $e->getMessage());

return false;

?>

You might also like