How to setup contact form for PHP AWS BeanStalk - php

I have made a website and would like to use AWS to host it. I currently have it running on S3 as a static server however I can't use the contact form for it. So after doing some research I tried AWS beanstalk but can't seem to get it working. When I click submit, nothing happens. This is the code that's setting up the contact form in index.html
<form id="contact-form" action="" class="post-reply" method="POST">
<p><input class="w3-input w3-border" id="form_name" type="text" placeholder="Name" required name="Name"></p>
<p><input class="w3-input w3-border" id="form_number" type="text" placeholder="Phone Number" required name="Phone Number"></p>
<p><input class="w3-input w3-border" id="form_email" type="text" placeholder="Email" required name="Email"></p>
<p><input class="w3-input w3-border" id="form_subject" type="text" placeholder="Subject" required name="Subject"></p>
<p><input class="w3-input w3-border" id="form_message" type="text" placeholder="Message" required name="Message"></p>
</form>
<p>
<button class="w3-button w3-black" type="submit">
<i class="fa fa-paper-plane"></i> SEND MESSAGE
</button>
</p>
Then I created another file and named it mail.php with the following code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST["form_name"];
$email = $_POST["form_email"];
$phone = $_POST["form_phone"];
$subject = $_POST["form_subject"];
$message = $_POST["form_message"];
$to ="duvalprecisiongrinding#gmail.com";
$subject = $subject;
$message = "Email: {$email} Phone: {$phone} Message:" . $message;
//Always set content-type when sending HTML email
$headers ="MIME-Version 1.0" . "\r\n";
//More Headers
$headers .= 'From: duvalprecisiongrinding#gmail.com';
$mail = mail($to,$subject,$message,$headers);
if ($mail) {
echo "<script>alert('Message Sent.');</script>";
}else {
echo "<script>alert('Message not Sent. Please try again.');</script>";
}
}
?>

I made several changes to the contact form that should help, indicated with comments below.
I think the main things keeping it from working at all were the submit button being outside the form and the missing action attribute. Fixing only those would have at least gotten it as far as mail.php, but the names of the form controls need to match the keys of $_POST in mail.php for it to really work as intended.
<form id="contact-form" action="mail.php" class="post-reply" method="POST">
<!-- updated this attribute ^ -->
<p><input class="w3-input w3-border" name="form_name" type="text" placeholder="Name" required></p>
<p><input class="w3-input w3-border" name="form_phone" type="text" placeholder="Phone Number" required></p>
<p><input class="w3-input w3-border" name="form_email" type="text" placeholder="Email" required></p>
<p><input class="w3-input w3-border" name="form_subject" type="text" placeholder="Subject" required></p>
<p><input class="w3-input w3-border" name="form_message" type="text" placeholder="Message" required></p>
<!-- removed the name attributes from the inputs and changed their ids to
names that match the $_POST keys in mail.php -->
<p>
<button class="w3-button w3-black" type="submit" name="submit">
<!-- Moved the button inside the form and added this ^ name attribute -->
<i class="fa fa-paper-plane"></i> SEND MESSAGE
</button>
</p>
</form>

Related

How to send form data from my website to my email using php

I want to be able to send form data from my website to my email with php. However, I tried all of the examples online and none of them worked for me. I don't know why. Am I missing a plugin?
<form class="formmargin" id="email-form" action="send_form_email.php" method="post" target="iframe_dbcpmgmy" onsubmit="sent_dbcpmgmy = true" enctype="multipart/form-data">
<div style="float:left; width:50%; padding-right:2.5%">
<label for="name">Name:</label>
<input class="w-input" type="text" name="name" placeholder="Enter your name" id="name" required="required" data-name="Name">
<label for="company_name">Company Name:</label>
<input class="w-input" type="text" name="company_name" placeholder="Enter your company name" id="company_name" required="required" data-name="Company Name">
</div>
<div style="float:left; width:50%; padding-left:2.5%">
<label for="email_address">Email Address:</label>
<input class="w-input" type="email" name="email_address" placeholder="Enter your email address" id="email_address" required="required" data-name="Email Address">
<label for="phone_number">Phone Number:</label>
<input class="w-input" type="text" name="phone_number" placeholder="Enter your phone number" id="phone_number" required="required" data-name="Phone Number">
<label for="uploaded_file">Upload a file (Optional)</label>
<input type="file" name="uploaded_file" id="uploaded_file">
</div>
<input style="clear:both" type="submit" name="submit" value="Submit" class="submitbutton">
</form>
can someone tell me what i should put in the send_form_email.php file?
You will want to create a second page getting all of the post information then you will want to use the php mail function.
If you haven't already created the post variables they will look like this
$name = $_POST['name'];
$company = $_POST['company_name'];
$to = $_POST['email_address'];
If you want to format it to have new lines in the message, because by default it will be one huge line use \n as no html/css formatting will work easily
mail ($to , $subject , $message );

No email received when sent with contact form

When I am filling out the contact form on the website that I am making, the e-mail will be sent, but I am not receiving it in the inbox of my computer.
The code looks like this:
HTML:
<div id="form">
<form action="mailto:psteintj#xs4all.nl" id="contactForm" method="post">
<span></span>
<input type="text" name="name" class="name" placeholder="Enter your name" tabindex=1 />
<span></span>
<input type="text" name="email" class="email" placeholder="Enter your email" tabindex=2 />
<span id="captcha"></span>
<input type="text" name="captcha" class="captcha" maxlength="4" size="4" placeholder="Enter captcha code" tabindex=3 />
<span></span>
<textarea class="message" placeholder="Enter your message" tabindex=4></textarea>
<input type="submit" name="submit" value="Send e-mail" class="submit" tabindex=5>
</form>
</div>
JS:
if ((captchaVal == captchaCode) && (emailFilter.test(emailText)) && (nameFilter.test(nameText)) && (messageText > 50)) {
$.post("mail.php", {
name: $(".name").val(),
email: $(".email").val(),
message:$(".message").val()
});
$("#contactForm").css("display", "none");
$("#form").append("<h2>Message sent!</h2>");
return false;
}
and PHP:
<?php
$name = $POST['name'];
$email = $_POST['email'];
$message = $POST['message'];
Could someone tell me where I am going wrong?
Well, you are not sending any emails (or at least you haven't posted any code about it), so of course you are not receiving any emails. You should configure the mailing, and use the mail function.
The function needs a working SMTP server to actually send out the e-mail.
Your PHP has no mail() call or similar? I'm probably missing something here.

How to get the page title into a input's value?

I want my hidden input field to get the page title as its value.
This is my testmail.php code.
<p>From: <?php echo $_POST['name']; ?></p>
<p>Subject: <?php echo $_POST['subject']; ?></p>
<p>Email: <?php echo $_POST['email']; ?></p>
<p>Phone Number: <?php echo $_POST['phone']; ?></p>
<p style="width:300px;">Message: <?php echo $_POST['message']; ?></p>
and this is my form code
<form action="testmail.php" method="post" class="cf">
<label for="name">* Name:</label>
<input type="text" name="name" placeholder="Your Name">
<input type="hidden" name="subject" value="<?php value $_POST['pagetitle']; ?>">
<label for="email">* Email:</label>
<input type="text" name="email" placeholder="Enter a valid Email Address">
<label for="phone"> Phone:</label>
<input type="text" name="phone" placeholder="Enter areacode and number">
<label for="message">* Message:</label>
<textarea name="message"></textarea>
<button type="input">Send</button>
</form>
Is there a way to get the title automatically?
Here's a pure javascript way using the onsubmit event.
<form onsubmit="this.subject.value=document.title;">
<input type="text" name="subject" value="" />
<input type="submit" />
</form>
To give you a better understanding of the onsubmit event as the name suggests it executes the contained javascript when the user submits the form. The operation this.subject.value=document.title working from right to left basically says assign the value of document.title to the value attribute of the element with the name of subject in this specific form.
Using your existing form it should look like this (I added the onsubmit event and fixed up errors in your html as well as added the appropriate id's to form elements):
<form action="testmail.php" method="post" class="cf" onsubmit="this.subject.value=document.title;">
<label for="name">* Name:</label>
<input type="text" id="name" name="name" placeholder="Your Name" />
<input type="hidden" name="subject" value="" />
<label for="email">* Email:</label>
<input type="text" id="email" name="email" placeholder="Enter a valid Email Address" />
<label for="phone"> Phone:</label>
<input type="text" id="phone" name="phone" placeholder="Enter areacode and number" />
<label for="message">* Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="submit" />
</form>
This will set the value of your hidden input field once the page has finished loading.
<script>
$(function() {
$('input[name="subject"]').val($('title').text());
});
</script>
You can use JavaScript to do so, assuming you're using jQuery, bind submit event to your form
$('your_form_selector').on('submit', function(){
$('<input />').attr('type', 'hidden')
.attr('name', 'title')
.attr('value', document.title)
.appendTo($(this));
});

PHP form without opening a new window

Good day/evening. I have this sort of form in HTML:
<form action="scripts/form-testimonial.php" class="form" method="post">
<ul>
<li><label for="name">Name</label> <input class="required" id="namef3" name="name" placeholder="Name" type="text"></li>
<li><label for="email">E-mail</label> <input class="required" id="emailf3" name="email" placeholder="E-mail" type="text"></li>
<li><label for="company">Company</label> <input id="companyf3" name="company" placeholder="Company (if applicable)" type="text"></li>
<li><label for="msg">Message</label> <textarea class="required" cols="43" id="msgf3" name="msg" placeholder="Your feedback" rows="8"></textarea></li>
</ul>
<button class="submit" type="submit">Send your feedback</button>
</form>
and this sort of php sript for this form as a separate file:
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
header("Location: ../error.html");
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
header("Location: ../msg_sent_feedback.html");
}
?>
I also have a validator for this form, but this is not important. What is more important here is the way how this form works. Basically - when you click submit button - it goes to a separate php file, process the form and then - from what is already there - goes to msg_sent_feedback.html file. What I want to do is to NOT opens a separate file but just display a message in the same basic html file where the actual form exists. A message e.g. "Message sent".
Another thing - if I already have a validation script - do I need to have the first "if" in this form? or can I remove it totally? the one which redirects to error.html. Or can I remove these 4 lines? That line - I guess - is responsible for errors in the form, but as I mentioned, I have a validation script. I don't really want to change html. is it possible to modify the php script to work within the html document, as instructed above? thanks
Here is an Jquery ajax example that will help you.
<?php
$receip = "office#avehire.co.uk";
$subj = "Testimonial";
$bodymsg = "Name: ".$_POST['name']."\n"
."Company: ".$_POST['company']."\n"
."Email: ".$_POST['email']."\n"
."Message: ".$_POST['msg']."\n";
if(!$_POST['msg']){
echo "<h1>Error sending e-mail";
exit;
}
$email = $_POST['email'];
if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
echo "<h1>Email sent successfully</h1>";
}
?>
Html :
<!Doctype html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function submitmyform(){
data=$('#myform').serialize();
$.ajax({
url: "myphpfile.php",
type:'POST',
data:data,
async:false,
dataType:'html',
success: function(msg){
$('#response_goes_here').html(msg);
}
});
}
</script>
<form action="" id="myform">
<ul>
<li> <label for="name">Name</label> <input id="namef3" name="name" type="text" placeholder="Name" class="required"/>
</li>
<li> <label for="email">E-mail</label> <input id="emailf3" name="email" type="text" placeholder="E-mail" class="required"/>
</li>
<li> <label for="company">Company</label> <input id="companyf3" name="company" type="text" placeholder="Company (if applicable)" />
</li>
<li> <label for="msg">Message</label> <textarea id="msgf3" name="msg" cols="43" rows="8" placeholder="Your feedback" class="required"></textarea>
</li>
</ul>
<button type="button" class="button" onclick="submitmyform();">Send your feedback</button>
</form>
<div id="response_goes_here"></div>

Why won't my PHP form send emails correctly?

It sends the email, but without a body, subject or anything. This is what I get:
HTML:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" class="text" /></label>
<label for="message">Message:<textarea id="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
PHP (mail.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "me#christianselig.com";
$subject = "Message From Website";
$headers = "From: " . $email;
mail($recipient, $subject, $message, $headers);
echo "Thanks! The message was sent. :)";
?>
Any insight? Thanks so much.
Forms fields are based on their names not their ids.
replace your :
id="(...)"
by
id="(...)" name="(...)"
You should have a look to Swift Mailer which is strongly safer than your current method.
You need to have a name attribute on your HTML input fields. That's what the form uses to create the indexes in the $_POST array. Corrected HTML is below
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
you need to give name attribute for each html element . Use below html code:
<div class="contact-form">
<form action="mail.php" method="post">
<label for="name">Name:<input type="text" id="name" name="name" class="text" /></label>
<label for="email">Email:<input type="text" id="email" name="email" class="text" /></label>
<label for="message">Message:<textarea id="message" name="message"></textarea></label>
<input type="submit" class="submit" value="Send Message" />
</form>
</div>
Into my server works, are you sure that in $_POST are set?
If you can't send email assicure you can try to set SMTP to send your email
Try to use something like phpmailer to send email.
In your form for every field you have to put:
- id="example_id"
- name="example_name"

Categories