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");
}
?>
Related
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!
Getting a white screen when trying to submit my contact form which has these entries:
- Name
- Email
- Subject
- Message
Im attempting to recieve emails through my website. Ive checked all variable names and such and it seems that everything is correct. Im new to PHP so im a little cloudy on what to try next.
Thanks
<form method"POST" action="action/form-submit.php"> <!--NO FOR ATTRIBUTE, NOT ADDING FUNCTIONALITY-->
<h2>Contact Me:</h2>
<label>Your Name:</label>
<input name="name" type="text" placeholder="Your Name..." required/>
<label>Email:</label>
<input name="email" type="email" placeholder="Email..." required/>
<label>Query Type:</label>
<select id="qry" name="query" required>
<option value="" disabled selected>Please Select:</option>
<option value="jobs">Jobs</option>
<option value="website">Website Issues</option>
<option value="info">Information</option>
</select>
<label>Your Message:</label>
<textarea name="info" placeholder="Your Message..." required></textarea>
<input type="submit" value="Submit">
</form>
Then the PHP code:
<?php
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$email_from = "test#gmail.com";
$email_subject = "New Website Submission";
$email_body = "Visitor Name: $vname.\n".
"Visitor Email: $vemail.\n".
"Visitor Subject: $vquery.\n".
"Visitor Message: $vmessage.\n";
$to = "bradleyarcher98#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $vemail \r\n";
mail($to,$email_subject,$email_body,$headers);
header("location: contact.html");
?>
In order to see what is wrong, you need to turn on the PHP error handling.
When this is on, you won't just see a white page anymore, an error message with filename, line number and a message describing the error is sent to the browser.
<?php
session_start();
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$subject = " YOUR MESSAGE Title ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: "YOUR SITE NAME" . "\r\n";
$message = "
<!-- you can add CSS Aswell -->
<div>
<p>Visitor Name: ".$_POST['name']."<p>
<p>Visitor Email: ".$vemail."<p>
<p>Subject: ".$vquery."<p>
<p>Message :".$vmessage."<p>
</div>
";
mail($to,$subject,$message,$headers);
?>
IF (I Started Receiving Spam Bot Forms)
THEN (I Implemented New PHP Email script using a basic Honey Pot Method)
$ERROR (New PHP is not sending ALL the forms fields. Upon sending the form, my email is only receiving the, textarea id="message", field)
$LOG_FILE (My previous PHP script implemented a dynamic catch-all solution for form fields)
$FAILED_SOLUTION (Conversely I attempted to add the individual, $phone & $address fields manually on lines #6 7 & 14 of the PHP but am still only receiving the, textarea id="message", field)
$NOTES (I am self taught & typically only deal with PHP on a need to know basis. Please try to keep it simple and include a step-by-step explanation. Feel free to suggest any "best practices" i may have overlooked unrelated to my problem!)
$QUESTION = "Can someone show me how to call the other form fields in the PHP script to send to my email?"
$SUCCESS = "Thanks in Advance For Any Help That Maybe Given!";
PHP:
<?php
if($_POST){
$to = 'your-email-here#gmail.com';
$subject = 'Contact Form Submission';
$name = $_POST['name'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$email = $_POST['email'];
$message = $_POST['message'];
$robotest = $_POST['robotest'];
if($robotest)
$error = "Spam Protection Enabled";
else{
if($name && $phone && $address && $email && $message){
$header = "From: $name <$email>";
if(mail($to, $subject, $message,$header))
$success = "Your message was sent!";
else
$error = "Error_36 there was a problem sending the e-mail.";
}else
$error = "Error_09 All fields are required.";
}
if($error)
echo '<div class="msg error">'.$error.'</div>';
elseif($success)
echo '<div class="msg success">'.$success.'</div>';
}
?>
HTML FORM:
<form method="post" action="Form_Email.php">
<input type="text" id="name" name="name" placeholder="name" required>
<input type="text" id="phone" name="phone" placeholder="phone" required>
<input type="text" id="address" name="address" placeholder="address" required>
<input type="text" id="email" name="email" placeholder="email" required>
<textarea id="message" name="message" placeholder="message" required> </textarea>
<p class="robotic">
<input name="robotest" type="text" id="robotest" class="robotest" autocomplete="off"/>
</p>
<input type="submit" id="SEND" value="Submit">
</form>
Your message contains only $_POST['message'] for now. If you want to append other values, use concatenation on your $message variable.
$message .= ($name . $phone . $address . $etc)
Notice: A $foo .= $bar construction stands for $foo = $foo . $bar.
Do not forget about whitesigns such as spaces or new lines wherever you want. Simply concatenate ' ' or "\n".
After that, just send a mail using your $message as message.
sorry but i didn't learn PHP yet so can any one help me . i want the out put of the form written in txt file and a messege like " thank you " be the answer if the visitor complete the form and press submit
<form role="form" method="post" id="contact-form">
<input type="text" placeholder="Name" name="Name" id="Name" required>
<input type="email" placeholder="Email" name="Email" id="Email" required>
<input type="text" placeholder="Phone" name="Phone" id="Phone">
<select name="test">
<option selected disabled="disabled"> Where do you live?</option>
<option value="test12"> Al Amirat</option>
<option value="test13"> Other</option>
</select>
<br>
<button type="submit" id="submit"> Submit</button>
<div id="success"></div>
</form>
the last PHP code was to make it to email
<?php
$name = $_POST['Name'];
$email = $_POST['Email'];
$phone = $_POST['Phone'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$to = 'test#email.com';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.' Phone: '.$phone.' Subject: '.$subject.' Message: '.$message;
$headers = 'From: youremail#domain.com' . "\r\n";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $headers); //This method sends the mail.
echo "Your email was sent!"; // success message
}else{
echo "Invalid Email!";
}
?>
but after the client need to add the choose from place and making it in txt not email i didn't know how it solved
Here is one example,
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
Check more the example Here,
There are few things you might get stuck with.
Folder Permission:
PHP must have write permission to the folder you are going to save the file. Which 777 permission. So be very careful about security.
Path Separator
Path separators are different for linux and windows. So choose accordingly.
Hey I have a simple coding issue and I am hoping someone can spot the error of my ways.
I have a contact page http://mattmcdougall.ca/contact
this page has a form that is connected to a form PHP.
it is half working, I receive the email with ONLY the name subject and recipient (me). I do not get the other information in the form. (phone number and content do not get placed in the email) ALSO, I want the website to bring the person filling out the form to http://mattmcdougall.ca/contactdone currently it goes to a blank form.php page
here is the form code and PHP code, please feel free to clean up PHP as I used a template I found online.
<form action="form.php" method="post" class="formtext">
<input name="Name" type="text" value="enter name" maxlength="60"><br>
<input name="Email" value="enter email" type="text"><br>
<input name="Phone Number" value="enter phone#"type="text"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br><input name="Submit" type="submit" value="Submit"><form
and the PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Matt McDougall Photography</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<?php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n";
mail($recipient, $subject, $mail_body, $phone, $header); //mail command :)
if(mail)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
?>
</BODY>
</HTML>
You're going to need to change the name of your form elements to match the params that you're expecting to receive via $_POST.
Right now you have (for example):
<input name="Phone Number" value="enter phone#"type="text"><br><br>
But you're going to need to name it:
<input name="phone" value="enter phone#"type="text"><br><br>
The name attribute is what is going to be submitted via the form to your PHP script. You need to use the same name on both sides.
Also what is the $phone variable in your mail function call? Those areas are reserved for additional headers and not just $phone. You'll need to concatenate the phone number into the $mail_body like so (with additional formatting of course):
$mail_body = $mail_body . " " . $phone;
This is the correct signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Edit: In addition to all of this, your $header is also wrong as it's trying to use the $Name variable which does not exist. The $name variable does exist.
I think this below should solve your problems.
Form:
<form action="form.php" method="post" class="formtext">
<input type="text" name="name" placeholder="Enter Name" maxlength="60"><br>
<input type="text" name="email" placeholder="Enter Email"><br>
<input type="text" name="phone" placeholder="Enter Phone #"><br><br>
<textarea name="interest" cols="60" rows="10" placeholder="Interest?"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
form.php
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$interest = $_POST["interest"];
$to = "matt#mattmcdougall.ca";
$subject = "Interested Client";
$headers = "From: {$name} <{$email}>\r\n";
$mail_body = <<<EMAIL
Name: {$name}
Phone: {$phone}
Message:
{$interest}
EMAIL;
$mail_success = mail($to, $subject, $mail_body, $headers);
if($mail_success) {
echo "http://mattmcdougall.ca/contactdone.html"; // are you trying to redirect to here?
} else {
echo "Sorry, something went wonky! Please try again!";
}
?>
HTTP POST variables are case sensitive, and the names of the input fields do not match that of those in PHP. <input type="text" name="datanamehere" /> with $_POST["datanamehere"]. Like what other answers said:
HTML:
<form action="form.php" method="post" class="formtext">
<input name="name" placeholder="Full name" type="text" maxlength="60"><br>
<input name="email" placeholder="Email" type="email"><br>
<input name="phone" placeholder="Phone number" type="tel"><br><br>
<textarea name="interest" placeholder="Interested in?" cols="60" rows="10"></textarea><br>
<input type="submit" value="Submit">
</form>
Notice I changed the value to placeholder, and used type="email" and type="tel".
form.php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $name . " <" . $email . ">\r\n";
if (mail($recipient, $subject, $mail_body, $header)) //mail command :)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
As they said, mail() is a function returning a boolean. Wrap it around if to check for success.
Your element names do not match the ones in the PHP code and your form is not properly closed using .
<form action="form.php" method="post" class="formtext">
<input name="name" type="text" value="enter name" maxlength="60"><br>
<input name="email" type="text" value="enter email"><br>
<input name="phone" type="text" value="enter phone#"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br>
<input name="submit" type="submit" value="Submit">
</form>
mail is a function, so checking it in the 'if' by just writing mail isn't going to work.
try this instead
if(mail()):
// go to contactdone.html
header('Location: http://www.google.com');
exit();
else:
echo "Sorry Something Went Wonky, Please Try Again!";
endif;
Couple of things you can try,
if(mail)'http://mattmcdougall.ca/contactdone.html' is not going to do
anything, try using Header('Location
http://mattmcdougall.ca/contactdone.html')
Whenever you use $_POST the parameter name inside the [''] need to
match your input names (Example: <input name="interest" type="text">
would be $_POST['interest'])
If you want to show text inside an input use placeholder="enter
phone#" instead of value.