I have build my own contact form for first time just to learn. this is my php code :
<?php
// define variables and set to empty values
$nameErr = $emailErr = $phoneErr = $contentErr = "";
$name_sender = $email_sender= $phone = $content =$succes= "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// post
$name_sender = strip_tags($_POST['name_sender']);
$email_sender = strip_tags($_POST['email_sender']);
$phone = strip_tags($_POST['phone']);
$content = strip_tags($_POST['content']);
// validate name
if (empty($_POST["name_sender"])) {
$nameErr = "Name is required";
} else {
$name_sender = test_input($_POST["name_sender"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z-' ]*$/",$name_sender)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email_sender"])) {
$emailErr = "Email is required";
} else {
$email_sender = test_input($_POST["email_sender"]);
// check if e-mail address is well-formed
if (!filter_var($email_sender, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
// validate phone
if (empty($_POST["phone"])) {
$phoneErr = "Phone is required";
} else {
$phone = test_input($_POST["phone"]);
// check if phone is well-formed
if(!preg_match("/^[0-9\s\-]+$/", $phone)) {
$phoneErr = "Invalid phone number";
}
}
// validate content
if (empty($_POST["content"] )) {
$contentErr = "Content is required";
} else {
$content = test_input($_POST["content"]);
}
// send
if ( empty($nameErr) && empty($emailErr) && empty($phoneErr) && empty($contentErr) ) {
// Variables
$to = 'johannes@webdesignleren.com';
$subject = 'Contact form';
$success = 'Thank you! You will receive a response as soon as possible.';
// Message content
$content .= '';
// Headers
$headers = 'From: ' .$name_sender. ' <' .$email_sender. '>' . PHP_EOL;
$headers .= 'Reply-To: <' .$email_sender. '>' . PHP_EOL;
$headers .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL;
$headers .= 'Content-Type: text/plain; charset=UTF-8' . PHP_EOL;
// Send email
mail($to, $subject,$content, $headers);
echo $success;
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// form
?>
and this is the HTML code:
<div class="container">
<form id="contact" method="post">
<h3>Quick Contact</h3>
<h4>Contact us today, and get reply with in 24 hours!</h4>
<fieldset>
<input placeholder="Your name" type="text" name="name_sender" value="<?php echo $name_sender;?>">
<span class="error">* <?php echo $nameErr; ?></span>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="text" name="email_sender" value="<?php echo $email_sender;?>">
<span class="error">* <?php echo $emailErr; ?></span>
</fieldset>
<fieldset>
<input placeholder="Your phone number" type="text" name="phone" value="<?php echo $phone;?>">
<span class="error">* <?php echo $phoneErr; ?></span>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here.." type="text" name="content"><?php echo $content;?></textarea>
<span class="error">* <?php echo $contentErr; ?></span>
</fieldset>
<fieldset>
<button name="submit" type="submit" value="reset" id="contact-submit">Submit</button></fieldset>
</form></div>
I can send and recieve an email everything is OK . but there is one problem after submitting and sending data all the data remains in the fields. the fields doesn't clean.
I see I can use reset button but I don't want that .I want that when customer sends an email all the fields become clean . I think this is the way of professional contact form. I searched in Google but I couldn't find something that satisfies me. I know I can do it with JS with ad listener event or sothing like this . but I want to achieve with php if it is possible . can you help me to achieve my goal exactly?
2- I see also after I insert many names or emails or phone numbers they all stay there as auto fill in the fields I want also to clean those informations after submitting the form!