PHP form with image upload - php

I'm trying to make a simple contact form page with a combination of HTML and PHP with an upload image file section to it. I am 99% sure I got the HTML part correct but I am just lost with the PHP part.
So here is my HTML
<form class="" action="contactform.php" method="post">
<input type="text" name="firstname" placeholder="First Name" required>
<input type="text" name="lastname" placeholder="Last Name" required>
<input type="text" name="mail" placeholder="Your e-mail" required>
<input type="text" name="phone" placeholder="Phone">
<input type="file" id="myFile" name="filename">
<textarea name="message" placeholder="Message" required></textarea>
<button type="submit" name="submit">Send</button>
</form>
And here is my PHP
if (isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "email#email.com";
$headers = "From: ".$mailFrom;
$subject = "You have received an e-mail from your web site";
$message = "First Name: " . $firstname . " Last Name: " . $lastname . "\r\nPhone: " . $phone . "\r\nemail: " . $mailFrom . "\r\nMessage: " . $message;
$retval = mail( $mailTo, $subject, $message, $headers );
if ( $retval == true ){
echo "<h1>Message Sent</h1>
<p>Thank you for contacting us your message has been sent successfully and we will get back to you a soon as possible.</p>";
}
else {
echo "<h1>Message Sent</h1><p>Message could not be sent Mail server did not accept mail.</p>";
}
}
Does anyone know how I can make the upload file part work?

try changing if($retval==true) to if($retval!=false) because I believe it only returns false if it fails and if it is successful it will return a hash value, so I believe making sure it's not true will work.
Also just you might want to take mail( $mailTo, $subject, $message, $headers ); and copy it into its own function, but I'm not sure. Kinda like this:
$retval = mail( $mailTo, $subject, $message, $headers );
mail( $mailTo, $subject, $message, $headers );
I'm not sure though, I hope this works, sorry if it doesn't!

Related

creating a contact page in HTML and PHP can getting a return error when testing (HTML 500)

So I am rather new to coding in these languages, I am getting a good understanding of how to write in them but not how to troubleshoot well. I went on and found some videos and read some sites to understand how to get this to work, and got this
HTML
<form id="Contact-form" method="post" action="contactform.php">
<input type="text" name="name" placeholder="Your Name" class="form-control" required>
<input type="text" name="Mail" placeholder="Your E-mail" class="form-control" required>
<input type="text" name="name" placeholder="Subject" class="form-control" required>
<textarea name="message" placeholder="Message" rows="7" class="form-control" required></textarea>
<button class="form-control submit" type="submit" name="sumbit"> Send message</button>
The PHP
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
I have it live and attempted to test send myself an email and I get a basic error (HTML Error 500) and no email, it changes the URL to "contactform.php" as expected. I followed the tutorials to the T, what am I doing wrong!
Thank you for the help in advance
Please change the codes from
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
to
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend");
}

display a message after submit and refresh

i'm trying to get a message to appear after my form has been submitted, i can get the form to submit and the page to then refresh but unsure how to add the message after the refresh.
i have no knowledge of PHP so sorry is its and obvious answer
thanks
my html & PHP if needed
<form action="action.php" method="POST" target="my_iframe" onsubmit="setTimeout(function(){window.location.reload();},10);">
<input type="text" id="name" name="name" placeholder="name" required="required">
<input type="email" id="email" name="email" placeholder="email" required="required">
<input type="tel" id="phone" name="phone" placeholder="phone">
<div class="form-row" style="display:none;"><input type="hidden" name="url" placeholder="URL"></div>
<textarea id="message" name="message" placeholder="message" style="height:200px" required="required"></textarea>
<input id="submit" type="submit" value="submit">
</form>
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
<?php
$to = 'zoeharrisondesign#gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];
//check honeypot
if( !empty( $honeypot ) ) {
echo 'There was a problem';
}
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = "$message \r\n |
From $name \r\n |
Tel: $phone";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your message has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Update your php sending email to this
if (mail($myEmail, $subject, $message)){
$success = "Message sent successful";
}else{
$success = "Message Sending Failed.";
}
Then add this php codes in your html codes add this code to display a message. Put it on top of your form html tag.
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
You may even add some styling on the message if you prefer.

PHP contact form submits but does not send mail

I recently made a website using HTML, CSS, and JS. Since I don't know PHP, I am stuck at building the contact form where it is vital on the website. I learned a bit from YouTube tutorials and have the following HTML & PHP code:
<div class="contact_form">
<form action="/action_page.php">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$mailTo = 'example#something.in';
$headers = 'From: '.$mailFrom;
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsent");
}
?>
Why do I need the MIME and content-type headers at the bottom as that bit I added from another tutorial.
When I use the form and try sending the message, I get the "?mailsent" after the URL but I receive no email which is a professional plan by GoDaddy.
They are also hosting my website. I contacted them to know whether the server allows me to make my contact form with the plan I have and they said yes. So, I must be missing something important here.
refer to the documentation of mail function
there are 3 required parameter : email destination (to), subject and the message, and two additional options are: headers and parameters.
your code didnt respect that, because you missing to add the subjuct as parameter.
and you get ?mailsent because you use header("Location: index.html?mailsent") without any test if the email send successfully or not.
i suggest you to replace the last two lines of your php code with this
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
// error
}
EDIT:
you can get the error message with error_get_last() function.
thanks to https://stackoverflow.com/a/20203870/195835
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
print_r(error_get_last());
}
You are missing the form action, So PHP doesn't know what to do with your variable data.Try adding method="post" inside <form> tag. Like this
<div class="contact_form">
<form action="/action_page.php" method="post">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
And also. If you use your computer as localhost(using xampp , wamp, or something without a hosting service) You have to make some changes to the config files.
Also try this modified php code
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$title = "replace this";
$mailTo = 'support#udichi.in';
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL . 'X-Mailer: PHP/' . phpversion();
mail($mailTo,$title,$txt,$headers);
header("Location: index.html?mailsent");
}
?>

Why can't I see the email address from contact form?

I receive an email but it will read from unknown. Everything else seems to work. Here is a link to the page link I don't know much about php.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.html?mailsend");
}
?>
<form action="contactform.php" method="post" onsubmit="return confirm('Are you sure you want to submit this form?');">
<input type="text" name="name" placeholder="Name" required="required">
<input type="text" name="email" placeholder="Email" required="required">
<textarea name="message" placeholder="Write message here..." required="required"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</form>
In this you have mention this `$headers = "From: ".$mailFrom; but not $mailFrom found. Use $email in place of $mailFrom.
I changed the code. You guys were right. Thank you!
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "contact#podmtg.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$message;
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsend");
}
?>

Issue with simple PHP Contactform

So I have some issues with my PHP Contactform on a HTML site.
Code below is what I have written in HTML. (that's not te problem)
<form action="sendmail.php" method="POST" class="contact-form">
<input type="text" placeholder="Amount" name="amount">
<input type="text" placeholder="Name" required name="name">
<input type="text" placeholder="Email Address" required name="email">
<div class="validation">
<button class="btn" name="submit">Send request</button>
// END HTML code. (BELOW is starts PHP).
$email_to = "domain#website.com";
$amount = $_POST["amount"];
$name = $_POST["name"];
$email = $_POST["email"];
$email_subject = "DOMAINNAME";
$headers = "From: " . $email . "\n";
$headers .= "Reply-To: " . $email . "\n";
$message = 'Name: ' . $name . ', email: ' . $email . ', amount: ' . $amount;
ini_set("sendmail", $email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);}
{
header("Location:https://website.com");
} else {
echo "There has been an error sending your comments. Please try later.";
}
What do I wrong? I only wanna receive the --> name, amount and mail.
Only when I press submit on my HTML site, it's send me to website.com/sendmail.php (HTTP ERROR 500).
Thank you guys.

Categories