Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,194 members, 7,822,030 topics. Date: Thursday, 09 May 2024 at 02:44 AM

Using Php To Create A Form - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Using Php To Create A Form (730 Views)

GURUS! I Need Urgent Help With Using Php To Connect To Xampp Mysql / Using Html To Create A Form In Your Website / Using Php To Create A Form (2) (3) (4)

(1) (Reply)

Using Php To Create A Form by Nobody: 8:51pm On Mar 28, 2015
PHP is a great scripting language that allows many dynamic functions in your site. You can create custom forms, form validation, and email responses using PHP. This article will explain the basics in creating an email form that validates the inputs, produces errors when inputs are typed incorrectly, and send an email to you when submitted.
This section of the code will validate the form inputs
Below is the code you will use to validate whether the inputs have valid data or not. This can be customized for different form field validations.
Note! You can paste the entire code directly in the body section of your webpage to get it working.

<?php
if (isset($_REQUEST['submitted'])) {
// Initialize error array.
$errors = array();
// Check for a proper First name
if (!empty($_REQUEST['firstname'])) {
$firstname = $_REQUEST['firstname'];
$pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
if (preg_match($pattern,$firstname)){ $firstname = $_REQUEST['firstname'];}
else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
} else {$errors[] = 'You forgot to enter your First Name.';}

// Check for a proper Last name
if (!empty($_REQUEST['lastname'])) {
$lastname = $_REQUEST['lastname'];
$pattern = "/^[a-zA-Z0-9\_]{2,20}/";// This is a regular expression that checks if the name is valid characters
if (preg_match($pattern,$lastname)){ $lastname = $_REQUEST['lastname'];}
else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}
} else {$errors[] = 'You forgot to enter your Last Name.';}

//Check for a valid phone number
if (!empty($_REQUEST['phone'])) {
$phone = $_REQUEST['phone'];
$pattern = "/^[0-9\_]{7,20}/";
if (preg_match($pattern,$phone)){ $phone = $_REQUEST['phone'];}
else{ $errors[] = 'Your Phone number can only be numbers.';}
} else {$errors[] = 'You forgot to enter your Phone number.';}

if (!empty($_REQUEST['redmapleacer']) || !empty($_REQUEST['chinesepistache']) || !empty($_REQUEST['raywoodash'])) {
$check1 = $_REQUEST['redmapleacer'];
if (empty($check1)){$check1 = 'Unchecked';}else{$check1 = 'Checked';}
$check2 = $_REQUEST['chinesepistache'];
if (empty($check2)){$check2 = 'Unchecked';}else{$check2 = 'Checked';}
$check3 = $_REQUEST['raywoodash'];
if (empty($check3)){$check3 = 'Unchecked';}else{$check3 = 'Checked';}
} else {$errors[] = 'You forgot to enter your Phone number.';}
}
//End of validation
Sends the email if validation passes
The following code is what sends the email. The inputs must pass the previous validation in order for the email to send. You will need to replace the "to" email address with the email address you want to receive the email to.
if (isset($_REQUEST['submitted'])) {
if (empty($errors)) {
$from = "From: Our Site!"; //Site name
// Change this to your email address you want to form sent to
$to = "your@email.com";
$subject = "Admin - Our Site! Comment from " . $name . "";

$message = "Message from " . $firstname . " " . $lastname . "
Phone: " . $phone . "
Red Maple Acer: " . $check1 ."
Chinese Pistache: " . $check2 ."
Raywood Ash: " . $check3 ."";
mail($to,$subject,$message,$from);
}
}
?>
Error Reporting Code
This code will print any errors that occurs such as an empty input.
<?php
//Print Errors
if (isset($_REQUEST['submitted'])) {
// Print any error messages.
if (!empty($errors)) {
echo '<hr /><h3>The following occurred:</h3><ul>';
// Print each error.
foreach ($errors as $msg) { echo '<li>'. $msg . '</li>';}
echo '</ul><h3>Your mail could not be sent due to input errors.</h3><hr />';}
else{echo '<hr /><h3 align="center">Your mail was sent. Thank you!</h3><hr /><p>Below is the message that you sent.</p>';
echo "Message from " . $firstname . " " . $lastname . " <br />Phone: ".$phone." <br />";
echo "<br />Red Maple Acer: " . $check3 . "";
echo "<br />Chinese Pistache: " . $check2 . "";
echo "<br />Raywood Ash: " . $check3 . "";
}
}
//End of errors array
?>
Prints the contact form
This is the form that will display for the visitor to fill out.
<h2>Contact us</h2>
<p>Fill out the form below.</p>
<form action="" method="post">
<label>First Name: <br />
<input name="firstname" type="text" value="- Enter First Name -" /><br /></label>
<label>Last Name: <br />
<input name="lastname" type="text" value="- Enter Last Name -" /><br /></label>
<label>Phone Number: <br />
<input name="phone" type="text" value="- Enter Phone Number -" /><br /></label>
<label>Red Maple Acer:
<input name="redmapleacer" type="checkbox" value="Red Maple Acer" /><br /></label>
<label>Chinese Pistache:
<input name="chinesepistache" type="checkbox" value="Chinese Pistache" /><br /></label>
<label>Raywood Ash:
<input name="raywoodash" type="checkbox" value="Raywood Ash" /><br /></label>
<input name="" type="reset" value="Reset Form" /><input name="submitted" type="submit" value="Submit" />
</form>
You can paste the entire code directly in the body section of your webpage to get it working. We have more tutorials with other methods to send email from your website at the following links.
Re: Using Php To Create A Form by Nobody: 9:02pm On Mar 28, 2015
Use this code
?php

if(isset($_POST['email'])) {

     

    // CHANGE THE TWO LINES BELOW

    $email_to = "you@yourdomain.com";

     

    $email_subject = "website html form submissions";

     

     

    function died($error) {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }

     

    // validation expected data exists

    if(!isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['telephone']) ||

        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');      

    }

     

    $first_name = $_POST['first_name']; // required

    $last_name = $_POST['last_name']; // required

    $email_from = $_POST['email']; // required

    $telephone = $_POST['telephone']; // not required

    $comments = $_POST['comments']; // required

     

    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.\n\n";

     

    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href"wink;

      return str_replace($bad,"",$string);

    }

     

    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Telephone: ".clean_string($telephone)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\n";

     

     

// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers); 

?>

 

<!-- place your own success html below -->

 

Thank you for contacting us. We will be in touch with you very soon.

 

<?php

}

die();

?>
Re: Using Php To Create A Form by Nobody: 9:04pm On Mar 28, 2015
Alway use notepad or MS Word to edit the php before pasting it

(1) (Reply)

PHP Programmer Needed Urgently / If You Are On Laravel 5.1 You Should Read This / Winning Online Contests

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 57
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.