Below is the PHP code I deleted some of the PHP mailer code because stack overflow refused to allow me to submit the code saying the code was too long and that I needed more explanation for the problem at hand.
<?php
if(isset($_POST['submit'])) {
$mail->addAddress($_POST['email']);
// $mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->isHTML(true);
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
$mail->addAttachment($_POST['attachment']);
try {
$mail->send();
echo 'Your message was sent successfully!';
} catch (Exception $e) {
echo "Your message could not be sent! PHPMailer Error: {$mail->ErrorInfo}";
}
}
else {
echo "There is a problem with the contact.html document!";
}
?>
Below is the HTML form. I have tried submitting the form data to be processed by the PHP code but it refuses to be submitted. I have posted the PHP code above and the HTML code below.
<form method="POST" role="form" enctype="multipart/form-data">
<div class="row">
<div class=" col-md-6 mb-3">
<input type="email" class="form-control" name="email" id="email">
</div>
<div class=" col-md-6 mb-3">
<input type="text" class="form-control" name="subject" id="subject">
</div>
<div class="mb-3">
<textarea class="form-control" rows="5" name="message" id="message"></textarea>
</div>
<div class=" col-md-6 mb-3">
<input type="file" class="form-control" name="attachment" id="attachment">
</div>
</div>
</form>
<button name="submit" type="submit" class="btn btn-primary">Send mail</button>
You have to add submit button under form tag.
<form method="POST" role="form" enctype="multipart/form-data">
<div class="row">
<div class=" col-md-6 mb-3">
<input type="email" class="form-control" name="email" id="email">
</div>
<div class=" col-md-6 mb-3">
<input type="text" class="form-control" name="subject" id="subject">
</div>
<div class="mb-3">
<textarea class="form-control" rows="5" name="message" id="message"></textarea>
</div>
<div class=" col-md-6 mb-3">
<input type="file" class="form-control" name="attachment" id="attachment">
</div>
</div>
<button name="submit" type="submit" class="btn btn-primary">Send mail</button>
</form>
Thanks
Your submit button is not inside the form tag.. That is the issue Please put the submit button inside the form tag. before the closing tag
You should keep the submit button inside form tags.
<form><button name="submit" type="submit" class="btn btn-primary">Send mail</button></form>
Related
I'm trying to create a multistep form that sends an email to me every time the form has been submitted. The form fully works except it won't send me the email. The code I used does work though for another test I did where the form is not a multistep form.
Here's the code I use:
<form <?php
error_reporting(E_ALL);
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$to='email#email.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Email :".$email."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$message;
$headers="From: ".$email;
if(mail($to, $subject, $message, $headers)){
echo "<h1>Sent Successfully! Thank you"." ".$name.", We will contact you shortly!</h1>";
}
else{
echo "Something went wrong!";
}
}
?> method="post" name="form" class="form-box">
<div class="step step-1 active">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name">
</div><br>
<button type="button" class="next-btn">Next</button>
</div>
<div class="step step-2">
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email">
</div><br>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="number" id="phone" name="phone">
</div>
<button type="button" class="prev-btn">Previous</button>
<button type="button" class="next-btn">Next</button>
</div>
<div class="step step-3">
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" class="message-box" placeholder="Enter Your Message Here..."></textarea>
<!-- <input type="text" id="message" name="message">-->
</div>
<button type="button" class="prev-btn">Previous</button>
<button type="submit" name="submit" value="Send" class="submit-btn">Submit</button>
</div>
</form>
Thanks in advance and I hope someone can help me figure it out.
I am trying to get all the fields of a html form and sent them by email when the submit button is pressed.
But it doesn't work and I don't know why.
When I test the send button, nothing display in action.php page I don't know why.
I made method="post" and action=action.php in form tag.
HTML FORM
<form action="action.php" method="POST">
<div class="p pb-3"><strong>Feel free to contact me </strong></div>
<div class="row mb-3">
<div class="col">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user-circle"></i></span>
<input class="form-control" type="text" name="name" placeholder="Name" required="required"/>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-file-text"></i></span>
<input class="form-control" type="text" name="subject" placeholder="Subject" required="required"/>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input class="form-control" type="email" name="mail" placeholder="E-mail" required="required"/>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Your Message" required="required"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col">
<button class="btn btn-primary" type="submit" name="submit" value="Send">Send</button>
</div>
</div>
</form>
```.
PHP CODE
``` code
<?php
echo 'bonjour'
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$subjectF=$_POST['subject'];
$msg=$_POST['message'];
$to='boussidan.raphael#gmail.com';
$subject='Form Submission';
$message="Name :".$name."\n"."Subject :".$subjectF."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
if(mail($to, $subjectF, $message, $headers)) {
echo "<h1>Sent Successfully! Thank you"." ".$name.", I will contact you shortly!</h1>";
echo "Message :"." ".$message. , .$subjectF., .$email.
}
else{
echo "<h1>Something went wrong!</h1>";
}
}
?>
If you are making website not on local server, php mail will be probably turned off, also try https://github.com/PHPMailer/PHPMailer it is if mail does not work, cuz from your question I did not understand what is not working. Another thing if you are doing your php code into another file, not in the file where your html, you don't have to put
if(isset($_POST['submit']))
because if you are doing it in another file it means you are already pressed button to make action in that file
On sending the email by clicking the send message button an alert box should show up, but it is not coming up neither it is showing inquiry sent successfully nor it is showing email not sent I don't know where I'm going wrong. Please help me resolve this issue.
Any help will be highly appreciable. I'm attaching herewith a part of my contact.php code and my mail.php code.
Thank You
contact.php
<form id="contact-form" method="POST" action="mail.php">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<button class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">Send Message</button>
</div>
</form>
mail.php
<?php
if (isset($_POST['submit'])){
$to = "contact#imatrixautomation.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "<script>alert('Enquiry sent successfully!');</script>";
}
else
{
echo "<script>alert('Mail was not sent. Please try again later');</script>";
}
}
header('Location: https://imatrixautomation.com/contact.php');
exit;
?>
Replace
<button class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">Send Message</button>
To
<input class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">
you should add name to your button
<button class="btn btn-primary solid blank button" id="btn" name="submit" type="submit" value="submit">Send Message</button>
also as you're redirecting by changing header it won't show you the alert as alert was on different page.
try this code
<form id="contact-form" method="POST" action="">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<button class="btn btn-primary solid blank button" id="btn" name="submit" type="submit" value="submit">Send Message</button>
</div>
</form>
and below this write your php code in same file
<?php
if(isset($_POST['submit'])) {
$to = "contact#imatrixautomation.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .= "\r\n from: $email";
if(mail($to, $subject, $name, $message)) {
echo "<script>alert('Enquiry sent successfully!');</script>";
}
else {
echo "<script>alert('Mail was not sent. Please try again later');
</script>";
}
}
?>
In the contact.php file
name="submit" is missing for the submit button.
This line should be:
<button class="btn btn-primary solid blank button" id="btn" type="submit" name="submit" value="submit">Send Message</button>
Explanation:
Currently the
if (isset($_POST['submit'])){
always evaluates to FALSE as POST['submit'] is empty so it continues at the header()...
################
try this code as mail.php and tell us what happens ;-)
<?php
if (isset($_POST['submit'])){
echo "submit is set to {$_POST['submit']} and now we send the email<br>";
$to = "contact#imatrixautomation.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "Enquiry sent successfully!<br>";
}
else
{
echo "Mail was not sent. Please try again later<br>";
}
} else{
echo"submit is not set<br>";
}
echo "after the if condition";
exit;
?>
Here i have the new contact.php file - just copy paste this
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unbenanntes Dokument</title>
</head>
<body>
<form id="contact-form" method="POST" action="mail.php">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<button class="btn btn-primary solid blank button" id="btn" type="submit" name ="submit" value="submit">Send Message</button>
</div>
</form>
</body>
</html>
I am creating a simple CMS and I would like the users to be able to upload an image in their posts. The form is working properly besides the image. For some reason I get this message 'Notice: Undefined index: post_img'. Can someone help me solve this?
Thanks.
Code:
<form method="post" action="add_post.php">
<?php
if(isset($_POST['add_post'])) {
$add_title = $_POST['post_title'];
$post_img = $_FILES['post_img']['name'];
$post_img_temp = $_FILES['post_img']['tmp_name'];
$add_content = $_POST['post_content'];
move_uploaded_file($post_img_temp, "images/$post_img");
$add_post_query = "INSERT INTO posts (post_title, post_img, post_content) ";
$add_post_query .= "VALUES ('$add_title', '$post_img', '$add_content') ";
mysqli_query($connection, $add_post_query);
}
?>
<div class="form-group">
<label for="post-title">Title</label>
<input type="text" name="post_title" class="form-control">
</div>
<div class="form-group">
<label for="post-img">Image</label>
<input type="file" name="post_img" class="form-control">
</div>
<div class="form-group">
<label for="post-content">Content</label>
<textarea name="post_content" class="form-control"></textarea>
</div>
<div class="form-group">
<input type="submit" name="add_post" class="btn btn-primary" value="Add Post">
</div>
</form>
You need to properly place your form tag. Also you need to set the form to accept files by adding the enctype attribute to the form tag
<form method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="post-title">Title</label>
<input type="text" name="post_title" class="form-control">
</div>
<div class="form-group">
<label for="post-img">Image</label>
<input type="file" name="post_img" class="form-control">
</div>
<div class="form-group">
<label for="post-content">Content</label>
<textarea name="post_content" class="form-control"></textarea>
</div>
<div class="form-group">
<input type="submit" name="add_post" class="btn btn-primary" value="Add Post">
</div>
</form>
I have been building a Google ReCaptcha into a simple contact me form. I have been working on this for a couple of days now and I feel like there are 10 different ways to do it, but not of it "makes sense" or has clicked so far. The current code I have down is what I feel like would make sense best for me, but maybe someone else has a better way or tutorial.
Here is my question: Why is my form not connecting to Google's Servers and verifying and why am I not getting an echo when submitted.
CODE:
<div class="contactme" id="contactme">
<form class="form-horizontal" method="post" name="myemailform" id="myform" action="form-to-email.php">
<fieldset>
<h2>Let's Create Together</h2>
<hr class="small">
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">E-Mail</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="visitor_email" placeholder="E-Mail" required>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="8" name="message" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" name="submit" id="submitbutton" class="btn btn-primary mainButton">Submit</button>
<div class="g-recaptcha" data-sitekey="--KEY--"></div>
</div>
</div>
</fieldset>
</form>
</div>
PHP on seperate document.
<?php
if($_POST['submit']) {
$yoursecret='--KEY--';
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$yoursecret."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$googleobj = json_decode($response);
$verified = $googleobj->success;
if($verified === true){
//do stuff
echo "Success!, you have been verified!";
} else {
echo "No Success!";
}
}
?>
I also strangely get an "=" at the top of page ever since messing PHP and RECAPTCHA. The site is live at joshkelley.video If you submit the form it just takes you to a blank page, regardless of the whether you verify the Recapcha.
Is there a community standard for doing these? All I could find was a few youtube out of date youtube tutorials.