right now I am having code for database connection as well as mail.php
I want save contact form data in database(which is working very well) as well as send mail also to my email address(Don't know how to put code with manage_comments.php)
Here is my contact from, manage_comments.php, mail.php & javascript
Plz help me with same data to be saved in database as well as send email
contact form
<form method='post' action="manage_comments.php">
Name: <input type='text' name='name' id='name' />
<div style="color:red;" id="nameerror"></div><br />
Email: <input type='text' name='email' id='email' />
<div style="color:red;" id="emailerror"></div><br />
Contact: <input type='text' name='contact' id='contact' />
<div style="color:red;" id="phoneerror"></div><br />
<input type='submit' value='Submit' class="mailbtn" />
</form>
manage_comments.php
<?php
if( $_POST )
{
$con = mysql_connect("localhost","username","pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db_name", $con);
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_contact = $_POST['contact'];
$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$users_contact = mysql_real_escape_string($users_contact);
$query = "
INSERT INTO `my_db_name`.`table_name` (`id`, `name`, `email`, `contact`)
VALUES ( Null, '$users_name', '$users_email', '$users_contact');";
mysql_query($query);
echo "<h2>Thank you for your Comment!</h2>";
mysql_close($con);
}
?>
mail.php
<?php
$to = array("email_Ad1","email_Ad2");
$subject = "My subject";
$message = "Inquiry from <b>".$_POST['name']."</b> and phone number is <b>".$_POST['phn']."</b>!";
$message .= "<br><br>";
$message .= "<table border='1'>";
$message .= "<tr><td>Name </td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Phone </td><td>".$_POST['phn']."</td></tr>";
$message .= "<tr><td>Email </td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";
$from = "other_email_Ad";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= 'from: '.$from .'' . "\r\n" .
'Reply-To: '.$from.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
mail($row,$subject,$message,$headers);
}
echo "Mail Sent.";
die;
?>
javascript
<script type="text/javascript">
$('.mailbtn').live('click',function(){
name = $('#name').val();
phn = $('#contact').val();
email= $('#email').val();
---------validations----------------
$.ajax({
type: "POST",
async : true,
url: "mail.php",
data: { name:name, email:email, phn:phn}
})
.done(function( msg ) {
$('.mail_middle').html('');
$('.mail_middle').html('Thank you for quote request. One of the Flamingo Transworld team members will get back to you soon.');
return false;
});
</script>
You could simply use include and just POST to manage_comments.php:
manage_comments.php
[...]
mysql_close($con);
include "mail.php";
}
Related
I have Html website. There is newsletter signup form in the html. It is processed by php script file for sending the email. After the form submission I want it back to the original page and also an alert message for "Thank you for newsletter signup"
HTML :
<div class="col-xs-12 col-sm-12 col-md-6 newsletter-form">
<form name="contactform" method="post" action="scripts/contact.php" onsubmit="return ValidateForm(contactform)">
<input type="text" name="stremail" placeholder="Your email address" ><input type="submit" value="Submit">
</form>
</div>
Javascript Validation :
function ValidateForm(Form)
{
if (Form.stremail.value == "") {
alert("Please enter \"Email\" ");
Form.stremail.focus();
return (false);
}
if ((/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Form.stremail.value)) == false) {
alert("Invalid E-mail Address! Please re-enter.");
Form.stremail.focus();
return (false);
}
return (true);
}
PHP Script :
$stremail = $_POST["stremail"];
$to = "thebrandtgroupre#gmail.com";
$from = $stremail;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers. = "From: \"$from\" <$from>\r\nReply-To: \"$from\" <$from>\r\nX-Mailer: PHP/".phpversion();
$headers. = "BCC: luxuryproperties#gmail.com,pnparamasivan#gmail.com".
"\r\n"; //for testing purpose
$subject = "The Brandt Group Newsletter Signup";
$message = "Dear Administrator,\r\n\n";
$message = $message.
"The following information was submitted to the website:<br/><br/>";
$message = $message.
"Email Address : ".$stremail.
"<br/><br/>";
mail($to, $subject, $message, $headers);
header("Location: {$_SERVER["
HTTP_REFERER "]}");
$message2 = "Thank you for newsletter signup";
echo "<script type='text/javascript'>alert('Thank you for newsletter signup');</script>";
Any help ?
If you are using Jquery you can do like this:
HTML side
<div class="col-xs-12 col-sm-12 col-md-6 newsletter-form">
<form name="contactform" method="post">
<input type="text" class="stremail" name="stremail" placeholder="Your email address" >
<input type="submit" value="Submit">
</form>
</div>
The form action and onsubmit attributs have been removed
A class has been added on stremail input
Javascript
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(function(){
var form = $('form ');
form.submit(function(e) {
e.preventDefault();
if(ValidateForm(Form)) {
var data = {
'stremail' : $('.stremail').val()
}
$.post("scripts/contact.php", data, function(response) {
if(response.success) {
alert(response.success);
}
else {
// YOUR LOGIC WHEN ERROR OCCURED
}
});
}
});
function ValidateForm(Form) {
if (Form.stremail.value == "") {
alert("Please enter \"Email\" ");
Form.stremail.focus();
return(false);
}
if ((/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Form.stremail.value)) == false) {
alert("Invalid E-mail Address! Please re-enter.");
Form.stremail.focus();
return (false);
}
return(true);
}
});
</script>
PHP Side
Verify $_POST data and return response by type
<?php
$response = [];
if(isset($_POST["stremail"]) && !empty($_POST["stremail"])) {
$stremail = $_POST["stremail"];
$to="thebrandtgroupre#gmail.com";
$from = $stremail;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .="From: \"$from\" <$from>\r\nReply-To: \"$from\" <$from>\r\nX-Mailer: PHP/".phpversion();
$headers .="BCC: luxuryproperties#gmail.com,pnparamasivan#gmail.com" . "\r\n";//for testing purpose
$subject = "The Brandt Group Newsletter Signup";
$message = "Dear Administrator,\r\n\n";
$message = $message ."The following information was submitted to the website:<br/><br/>";
$message = $message ."Email Address : ".$stremail."<br/><br/>";
mail($to,$subject,$message,$headers);
$response['success'] = "Thank you for newsletter signup";
}
else {
$response['error'] = "YOUR_ERROR_MESSAGE";
}
return $response;
?>
The alert will not happen because you have already called header to redirect the user. To accomplish your goal of a javascript popup I'd perhaps set a session variable after the email has been sent and then when you are redirected back to the signup page, assuming that it is a PHP enabled page, you can test for that session and perform the javascript alert.
<?php
/* mail handler */
session_start();
$stremail = $_POST["stremail"];
$to="thebrandtgroupre#gmail.com";
$from = $stremail;
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: \"$from\" <$from>\r\nReply-To: \"$from\" <$from>\r\nX-Mailer: PHP/".phpversion();
$headers .= "BCC: luxuryproperties#gmail.com,pnparamasivan#gmail.com\r\n";
$subject = "The Brandt Group Newsletter Signup";
$message = "
Dear Administrator,
The following information was submitted to the website:
<br/><br/>
Email Address: {$stremail}
<br /><br />";
mail( $to, $subject, $message, $headers );
header( "Location: {$_SERVER["HTTP_REFERER"]}");
$_SESSION['mail']=true;
$message = "Thank you for newsletter signup";
?>
The signup page
<?php
session_start();
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>signup</title>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 newsletter-form">
<form name="contactform" method="post" action="scripts/contact.php" onsubmit="return ValidateForm(this)">
<input type="text" name="stremail" placeholder="Your email address" />
<input type="submit" />
</form>
</div>
<?php
if( !empty( $_SESSION['mail'] ) && $_SESSION['mail']==true ){
printf('<script>alert("%s");</script>', $message );
unset( $_SESSION['mail'] );
}
?>
</body>
</html>
Hi there I am having some trouble getting my php form to actually forward the information that is put in.
I have been struggling for a couple of days now and researched a lot online but unfortunately haven't found the answer.
The form works and redirects with a success message, is sent to my websites email then forwarded to a personal email. This is all instant but I only seem to receive blank emails.
Here is the php code:
<?php
$from = "admin#abievetattoo.com";
$to = "abievetattoo#gmail.com";
$subject = "Booking Enquiry";
$cf_name = Trim(stripslashes($_POST['cf_name']));
$cf_email = Trim(stripslashes($_POST['cf_email']));
$cf_message = Trim(stripslashes($_POST['cf_message']));
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
$Body = 'MIME-Version: 1.0' . "\r\n";
$Body .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$Body .= 'From: '. $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$Body = "";
$Body = "Name: ";
$Body = $cf_name;
$Body = "\n";
$Body = "eMail: ";
$Body = $cf_email;
$Body = "\n";
$Body = "Message: ";
$Body = $cf_message;
$Body = "\n";
$success = mail($to, $subject, $Body, "From: $from" . "\r\n");
?>
<script type="text/javascript">
alert("Thank you for the message. Abi will respond shortly.");
window.location = 'http://abievetattoo.com/contact.html';
</script>
Here is the html code:
<form action="contact.php" method="post">
<p>Your name</p>
<input type="text" name="cf_name" required>
<br>
<p>Your e-mail</p>
<input type="email" name="cf_email" required>
<br>
<p>Message</p>
<input type="text" name="cf_message">
<br><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Any help would be very much appreciated, thanks.
I have the following simple form that I am trying to get the email validation error to
show up within the form to show the error prior to submitting.
Is there a way to do this with PHP or do I have to use JSON?
If I have to use JSON, can anyone show me how to do this?
Thanks in advance.
form.html:
<form method="post" name="form" action="form.php">
<p>Robot: <input type="text" name="robot" ></p>
<p>Name: <input type="text" name="name" ></p>
<p>Email: <input type="email" name="email"></p>
<p>Phone: <input type="telephone" name="phone"></p>
<p>Message: <textarea name="message"></textarea></p>
<p><input type="submit" value="Send Form"></p>
</form>
<div id="error"></div>
form.php
<?php
// send to and from
$to = "email#example.com";
$headers = "From: email#example.com \r\n";
$headers .= "Reply-To: email#example.com \r\n";
// form inputs
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$robot = $_POST['robot'];
// email message
$email_subject = "Web Contact Message";
$email_body =
"A message from your website contact form \n\n".
"Email: $email \n\n".
"Phone: $phone \n\n".
"From: $name \n\n".
"Message: \n".
"$message \n";
// honeypot
if($robot)
header( "Location: http://www.example.com/nothankyou.html" );
else{
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
echo '<div id="error">Please Enter a Valid Email</div>';
}
else
{
// send it
mail($to,$email_subject,$email_body,$headers);
header( "Location: http://www.example.com/thankyou.html" );
}
}
?>
You could try using a javascript/jquery plugin to do your front end validation (ex. http://jqueryvalidation.org/, http://bootstrapvalidator.com/). If you still wanted to keep your existing code I'd suggest something like:
First, merge your form.html to form.php
Make sure your php mailing code stays at the top of the file because php headers cannot have any output done before calling them.
<?php
if (count($_POST)) {
// send to and from
$to = "email#example.com";
$headers = "From: email#example.com \r\n";
$headers .= "Reply-To: email#example.com \r\n";
// form inputs
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$robot = $_POST['robot'];
// email message
$email_subject = "Web Contact Message";
$email_body = "A message from your website contact form \n\n" .
"Email: $email \n\n" .
"Phone: $phone \n\n" .
"From: $name \n\n" .
"Message: \n" .
"$message \n";
// honeypot
if ($robot)
header("Location: http://www.example.com/nothankyou.html");
else {
//validate email
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: form.php?error=email_error");
} else {
// send it
mail($to, $email_subject, $email_body, $headers);
header("Location: http://www.example.com/thankyou.html");
}
}
}
?>
<form method="post" name="form" action="">
<p>Robot: <input type="text" name="robot" ></p>
<p>Name: <input type="text" name="name" ></p>
<p>Email: <input type="email" name="email"></p>
<p>Phone: <input type="telephone" name="phone"></p>
<p>Message: <textarea name="message"></textarea></p>
<p><input type="submit" value="Send Form"></p>
</form>
<?php
if (isset($_GET["error"]) && $_GET["error"] == "email_error") {
?>
<div id="error">Please Enter a Valid Email</div>
<?php
}
I am making contact form which sends form data into database as well as into mail(both working fine) but when submitting form, I want to display thank you message within .mail_middle class and hide the form.
Right now I am getting message "Thank u for your comment!" instead of "Thank you for quote request" with in same class.
I want "Thank you for quote request" to be displayed and hide form.
Plz help me further with code as I am unaware of what to do now.....
form.php
<form method='post'>
Name: <input type='text' name='name' id='name' />
Email: <input type='text' name='email' id='email' />
<input type='submit' value='Submit' class='mailbtn' />
</form>
process_db.php
<?php
[...]
mysql_query($query);
echo "Thank you for your Comment!";
mysql_close($con);
include("mail.php");
}
HTML
<div class="middle mail_middle">
<fieldset>
<?php include("form.php"); ?>
<?php include("process_db.php"); ?>
</fieldset>
</div>
<script type="text/javascript">
$('.mailbtn').live('click',function(){
$.ajax({
type: "POST",
async : false,
url: "mail.php",
data: { name:name, email:email, phn:phn, budget:bgt, comment:txt, loc:loc}
})
.done(function( msg ) {
$('.mail_middle').html('');
$('.mail_middle').html('Thank you for quote request.');
return false;
});
});
</script>
mail.php
<?php
$to = array("email-id1","email-id2");
$subject = "my_subject";
$message .= "<table border='1'>";
$message .= "<tr><td>Name </td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Email </td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";
$from = "email-id";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= 'from: '.$from .'' . "\r\n" .
'Reply-To: '.$from.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
mail($row,$subject,$message,$headers);
}
echo "Mail Sent.";
die;
?>
In your jQuery .done() callback, i am not sure what the effect of having return false here will be, but from what I can see, the AJAX method is most probably not getting called.
This would be the case if your button is not receiving a false response from the click event.
I suggest changing your code to this...
form.php
<form method='post' class="mailform">
Name: <input type='text' name='name' id='name' />
Email: <input type='text' name='email' id='email' />
<input type='submit' value='Submit' class='mailbtn' />
</form>
process_db.php
<?php
[...]
mysql_query($query);
echo "Thank you for your quote request.";
mysql_close($con);
include("mail.php");
$myformsubmitted = true;
}
html.php
<div class="middle mail_middle">
<fieldset>
<?php include("process_db.php"); ?>
<?php if(!$myformsubmitted) include("form.php"); ?>
</fieldset>
</div>
<script type="text/javascript">
// if js is supported submit by ajax...
$('.mailform').live('submit', function(event){
event.preventDefault();
$.ajax({
type: "POST",
async : false,
url: "process_db.php",
data: {
name:name,
email:email,
phn:phn,
budget:bgt,
comment:txt,
loc:loc
}
})
.done(function( msg ) {
$('.mail_middle').html('Thank you for quote request.');
});
});
</script>
I have a simple contact form I'm trying to implement but I'm getting an "incorrect function" error when I try to launch it. My code below is as follows, and when I click submit, it redirects to
http://mywebsite.com/contactme.php
but with the text "Incorrect function" and that's it. My debug on firefox shows the following error:
POST http://www.mywebsite.com/v/vspfiles/contactform/contactme.php [HTTP/1.1 405 Method Not Allowed 33ms]
13:54:52.368 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range.
The character encoding of the page must be declared in the document or in the transfer protocol.
I am using volusion software if that helps. But I have no idea if the error is in my code or because my webhost won't allow the function. Can someone give me some insight? I have tried the "contactme.php" page with and without the doctype declared. My two files are below. I do not have an "error.htm" page.
contact.html:
<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
</head>
<body >
<div id="contact-area">
<form method="post" action="/v/vspfiles/contactform/contactme.php">
<h3>Contact us</h3>
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
<div style="clear: both;"></div>
</div>
</body>
</html>
contactme.php:
<?php
$EmailFrom = "email#gmail.com";
$EmailTo = "email#gmail.com";
$Subject = "contact form";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// informs user they submitted, redirects to homepage
if ($success){
alert("Thank you for your interest in our multiple sample processing system. A member of the Claremont Bio team will respond to you shortly.");
window.location.assign(location.hostname);
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
alert("Thank you for your interest in our multiple sample processing system. A member of the Claremont Bio team will respond to you shortly.");
window.location.assign(location.hostname);
This is not valid php code, it is javascript. It definitely should not be in your php script.
As an alternative, in your if($success) condition you could redirect to a "success.php" page. For example:
if($success){
header("Location: http://www.mydomain.com/success.php");
}
An example of a contact.php page I use is as follows, note as per Jorge's comments, I make use of echoing the command...
<?php
// configuration
require("../includes/config.php");
// if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// submission is sanitised using the "query" function
$name = check_input($_POST["name"]);
$email = check_input($_POST["email"]);
$phone = check_input($_POST["phone"]);
$message = check_input($_POST["message"]);
$category = $_POST["category"];
switch($category)
{
// do some checking here
}
// insert user into db
// Success Message
$success = "
<div class=\"row-fluid\">
<div class=\"span11\">
<div class=\"alert alert-block alert-success\" id=\"thanks\">
<h4>Got it!</h4>
<br/>
<p>I'll be in touch within 24 hours. <strong> Promise.</strong></p>
<br/>
<p>In the meantime, why not check out my Facebook page...</p>
<br/>
www.facebook.com/myfacebooksite
</div>
</div>
</div>
";
$subject = 'New Website Message!';
$mailto = 'your#email.com';
// HTML for email to send submission details
$body = "
<br>
<p>The following information was submitted through the contact form on your website:</p>
<p><b>Name</b>: $name<br>
<b>Email</b>: $email<br>
<b>Phone</b>: $phone<br>
<b>Category</b>: $category<br>
<b>Message:</b>: $message<br>
";
$headers = "From: $name <$email> \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mailtext = "<html><body>$body</body></html>";
if (mail($mailto, $subject, $mailtext, $headers)) {
echo "$success"; // success
}
else
{
echo 'Form submission failed. Please try again...'; // failure
}
}
else
{
// else render form
redirect("/index.html");
}
?>
edit:
my contact form page has the following js:
// do the mailing
$('#contact_form').on('submit', function(e) {
e.preventDefault(); //Prevents default submit
var form = $(this);
var post_url = form.attr('action');
var post_data = form.serialize();
$.ajax({
type: 'POST',
url: 'contact.php',
data: post_data,
success: function(msg) {
$(form).fadeOut(200, function(){
form.html(msg).fadeIn();
});
}
});
});
Hope that helps steer you...
So turns out this isn't my error, this is my webhost. Had to call them up and they told me they don't support PHP currently. so I'm off to rewrite this is javascript. I'll give the answer to Jason as his was the most technically correct and pointed out the error. Thanks guys.