Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP Most web applications have a contact form. A server side form using PHP is

ID: 3734077 • Letter: H

Question

HELP Most web applications have a contact form. A server side form using PHP is more secure and offers significantly more functionality than an email link. The contents of the form could be loaded into a database and/or sent via an email. This assignment creates a simple contact form in HTML. The server side PHP will send the form data to you by using the PHP mail( ). Your assignment: Create an HTML form with the following fields. Contact Name: Contact Email Address: Comments: When the form is submitted it will send the form data to a page called formHandler.php. Implement your Email class on the formHandler.php page to create a new Email object(s) to use for this process. It will send a confirmation email to the Contact Email Address. This confirms to the customer that your application has received the customer's information and will respond to it. The page will also send an email to YOU with the form information. In this case you are acting as the point of contact for the client. Recommend using your DMACC email address as this point of contact. rmcdonell@dmacc.edu

Explanation / Answer

Belw is code for both HTML and PHP which will send mail to both customer and you.

I have created a basic HTML form with basic validations on JavaScript.

You can add CSS to the HTML elements to so form looks more beautiful.

HTML Code

<html>
<head>
   <title>Contact Us</title>
</head>
<body>
<form action="formHandler.php" id="ContactUs" method="post">
<script type="text/javascript">
function ValidateForm(frm)
{
   if (frm.cname.value == "")
   {
       alert('Name is required.');
       frm.cname.focus();
       return false;
   }
   if (frm.cemail.value == "")
   {
       alert('Email address is required.');
       frm.cemail.focus();
       return false;
   }
   if (frm.cemail.value.indexOf("@") < 1 || frm.cemail.value.indexOf(".") < 1)
   {
       alert('Please enter a valid email address.');
       frm.cemail.focus();
       return false;
   }
   if (frm.comments.value == "")
   {
       alert('Please enter comments.');
       frm.comments.focus();
       return false;
   }
return true;
}
</script>
<div align="center">
   <table>
       <tr>
           <td colspan="2" align="center">Contact Us</td>
       </tr>
       <tr>
           <td>Contact Name</td>
           <td> <input type="text" name="cname"></td>
       </tr>
       <tr>
           <td>Contact Email</td>
           <td> <input type="text" name="cemail"></td>
       </tr>
       <tr>
           <td>Comments</td>
           <td> <textarea name="comments"></textarea> </td>
       </tr>
       <tr>
           <td colspan="2" align="center" ><input type="submit" /></td>
       </tr>
   </table>
</div>
</body>
</html>

formHandler.php code

<?php
//getting form values in variables
$cname=$_POST['cname'];
$cemail=$_POST['cemail'];
$comments=$_POST['comments'];

if(isset($cname) && !empty($cname) && isset($cemail) && !empty($cemail) && isset($comments) && !empty($comments))
{
       //mail to customer
       $to = $cemail;
       $subject = "Contact query from ".$cname;
       $txt = "Hi ".$cname.", We have received your query. Our team wll contact you shortly. ";
       $headers = "From: rmcdonell@dmacc.edu";
       mail($to,$subject,$txt,$headers);
  
       //Mail to admin
       $to = "rmcdonell@dmacc.edu";
       $subject = "Contact us query from ".$cname;
       $txt = "Hi Admin You have one query from ".$cname." Customer Email: ".$cemail." Comment: ".$comments;
       $headers = "From: rmcdonell@dmacc.edu";
       mail($to,$subject,$txt,$headers);
      
       echo "We have received your query. Our team will contact you shortly!";
}
else
{
   echo "Invalid input";
   return 0;
}
?>