Just Start Typing Text and Press Enter

alt text
PANKAJ SINGH
Web Designer & Developer

DRINK A COFFEE WITH ME TODAY

Loading...
Message Snet
Error!

Blog posts

Menu
Close
Apr 22, 2016

How to Fetch data from form and store data into database as well as sending all data via mail

0 Comment | By
phpmysql-pankajsingh.me

<?php include(‘config.php’) ?>
<?php

// fetch data from  register page
$imgname=$_FILES[“clogo”][‘name’];
$type=$_FILES[“clogo”][‘type’];
$fn= $_POST[‘fn’];
$desg= $_POST[‘desg’];
$mn= trim($_POST[‘mn’]);
$email= $_POST[’email’];
$category= $_POST[‘comp_categ’];
$cn= $_POST[‘cn’];
$web_url= $_POST[‘web_url’];
$comp_desc= $_POST[‘comp_desc’];
$fb= $_POST[‘fb’];
$ln= $_POST[‘ln’];
$twiter= $_POST[‘tw’];
$gurl= $_POST[‘gurl’];

     // store image
$fp=addslashes(file_get_contents($_FILES[‘clogo’][‘tmp_name’]));

     // insert data into db
$sql = “INSERT INTO comp_personal_info(complogo, name, desg, contact,email, category, comp_name, webs_url, comp_desc, fb_url, ln_url, twiter_url, google_url, status)
VALUES (‘{$fp}’,’$fn’,’$desg’,$mn,’$email’,’$category’,’$cn’,’$web_url’,’$comp_desc’,’$fb’,’$ln’,’$twiter’, ‘$gurl’,0); “;

     // the result of the query
$result = mysql_query( $sql, $link) or die(“Invalid Inputs: you have entered some wrong inputs, Please try again….  ” );

?>

<?php

//Retrieve form data.
//GET – user submitted data using AJAX
//POST – in case user does not support javascript, we’ll use POST instead

$email = ‘info@yourdomain.com’; or just pass &email ( from your form)
$subject = “New  Registration “;
$errors=”;

//flag to indicate which method it uses. If POST set it to 1

if ($_POST) $post=1;

//Simple server side validation for POST data, of course, you should validate the email

if (!$email) $errors[count($errors)] = ‘Please enter your email.’;
if (!$subject) $errors[count($errors)] = ‘Please enter your subject.’;

//if the errors array is empty, send the mail
if (!$errors) {

    //recipient – replace your email here
$to = ‘info@yourdomain.com’;
    //sender – from the form
$from = $cn . ‘ <‘ . $email . ‘>’;

    //subject and the html message

$message = ‘Company Name: ‘ . $cn . ‘<br/><br/>
Category: ‘ . $category . ‘<br/><br/>
Contact Person: ‘ .$fn. ‘<br/><br/>
Designation: ‘ . $desg. ‘<br/><br/>
Description: ‘ . $comp_desc . ‘<br/><br/>’;

    //send the mail
$result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
if ($_POST) {
if ($result)
{
?>
<h1 style=” color:green; font-size:25px; text-align:center; margin:20% auto”>
<?php

mysql_close($link);
echo “YOUR CONTENT HAS BEEN SUCCESSFULLY UPLOADED “;?>
<h1>

     // This code is used for redirecting page after showing successful information
<?php
echo “<script>setTimeout(\”location.href = ‘http://yourdomain/thankyou’;\”,1500);</script>”;

?>
<?php

}
else echo ‘Sorry, unexpected error. Please try again later’;

//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
echo $result;
}

//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . ‘<br/>’;
echo ‘<a href=”index.html”>Back</a>’;
exit;
}

//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = “MIME-Version: 1.0” . “\r\n”;
$headers .= “Content-type:text/html;charset=iso-8859-1” . “\r\n”;
$headers .= ‘From: ‘ . $from . “\r\n”;

$result = mail($to,$subject,$message,$headers);

if ($result) return 1;
else return 0;
}

?>

Leave A Comment

Author Details

My name is Pankaj Singh and I am a web designer and developer with over 3+ years of professional experience in the design industry, currently living in New Delhi, India. I have a Master of Computer Application from GGSIPU University, and my primary focus and inspiration for studies is Web Development.

Share Post