Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working on a website and the form in the code is not working up. What can I do to make it work and put it in action. Here is the form code....
<div class="form"; action="mail.php"; method="post">
<input class="input-text" type="text" name="name" value="Your Name *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<input class="input-text" type="text" name="email" value="Your E- mail *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<textarea class="input-text text-area" name="message" cols="0" rows="0" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">Your Message *</textarea>
<input class="input-btn" type="submit" value="send message">
</div>
Here is the PHP code I am using to fetch data from form.
<html>
<body>
<?php
$errors = '';
$myemail = 'myemail#example.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
The problem is that after entering data in the form, it is not proceeding any further.
Change the markup so that it is a <form> not a <div> and get rid of the semicolons.
<form class="form" action="mail.php" method="post">
...
</form>
Related
Not a PHP expert. With that said, I've used this system before and it seemed to stop working one day. I can't seem to get emails sent anymore even if I'm not getting any error messages. I don't know what's wrong.
Form page:
<form method="POST" name="contactform" action="contact-form-handler.php">
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
What does this concern?<br>
<select name="options">
<option value="null">--</option>
<option value="IEP">IEP</option>
<option value="Section 504">Section 504</option>
<option value="Other">Other</option>
</select>
<p style="width: 50%">Please include in your message what type of service you are inquiring about. Please be as descriptive as possible so we can help you more efficiently. Thank you.</p>
<label for='message'>Message:</label> <br>
<textarea name="message" cols="45" rows="7"></textarea>
<input type="image" src="img/submit.png" alt="Submit"> <br>
</form>
Handling:
<?php
$errors = '';
$myemail = 'louie540x#gmail.com;';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['options']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$options = $_POST['options'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Regarding a(n): $options \n \n Message: \n \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.php');
}
?>
<?php include 'template/top.php'; ?>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
<?php include 'template/bottom.php'; ?>
$myemail = 'louie540x#gmail.com;';
Should be:
$myemail = 'louie540x#gmail.com';
However that may not be your only problem...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I used this post to make a simple ‘contact us’ form. The form collects a few pieces of information (email, name and a message) from your visitor and emails it to you.
Instead of redirecting to a thank you page, I'd prefer it to just print the thank you (or error) text in the same HTML form page.
How can this be done? Where do I need to change to be able to keep the user in the same page and show the confirmation message ("Thank you")?
Edit: Found a much simpler way to do this - with ajax.
Here is my suggestion.
1- Change your file name contact-form.html to contact-form.php
2- Added include line and edit your form action in contact-form.php:
<h1>Contact us</h1>
<?php include './contact-form-handler.php'; ?>
<form method="POST" name="contactform" action="#">
3- Edit your contact-form-handler.php, disable header and added your thank you with personal message, you can also added some layout and decoration that is left to your fantasy.
//header('Location: contact-form-thank-you.html');
echo "Thank you " . $name . "<br />";
echo "We will contact you soon";
4- Last thing I will added condition to check if the form is submitted or not
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (empty... etc
}
5- (Optional) To re-fetch the posted values if failed I have added following to the form inputs (3 of them, each field got one with the respective field/variable name):
value="<?php if (isset($_POST['name'])) echo $name; ?>"
But in general When all this done, I will personally will go through the form and clean it up a little bit. Like if I am check the content in JavaScript it is not necessary to check if the fields are empty, so checking submission form is enough. I have not talk about form security yet against any kind of misuse, this just to make some thoughts when you come so far.
Complete code
Here is your final results, with extra things I done:
contact-form.php
<form method="POST" name="contactform" action="#">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name" value="<?php if (isset($_POST['name'])) echo $name; ?>">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email" value="<?php if (isset($_POST['email'])) echo $email_address; ?>"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message" value="<?php if (isset($_POST['message'])) echo $message; ?>"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>
contact-form-handler.php
<?php
$errors = '';
$myemail = 'yourname#website.com';//<-----Put Your email address here.
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])
)
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address)
)
{
$errors .= "\n Error: Invalid email address";
}
if (empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. " .
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
//redirect to the 'thank you' page
//header('Location: contact-form-thank-you.html');
echo "Thank you " . $name . "<br />";
echo "We will contact you soon";
$name = "";
$email_address = "";
$message = "";
}
}
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hope you can help, I am trying to knock up a contact form for my website which is HTML, styled with CSS and the email sent with PHP.
Website: Evan Ciniello
Issue One: When a message is submitted through the contact form it does not redirect, or refresh (ends up at www.evanciniello.ca/php/submit.php)
Issue Two: The body of the message itself is not visible in my inbox (i.e I can see who sent the email but can not read the message).
Contact HTML form
<div id="container" class="clearfix">
<div class="element clearfix col2-3 contact">
<form id="contact-us" action="/php/submit.php" method="post">
<h1>Contact Us!</h1>
<input type="text" name="first_name" placeholder="NAME" required>
<input type="email" name="email" placeholder="MAIL" required>
<textarea name="message" placeholder="MESSAGE" required ></textarea>
<input id="button" type="submit" class="submit"></button>
</form>
<div id="error" style="display:none;"> Please Provide Valid Information</div>
</div>
</div>
PHP Form
<?php
if(isset($_POST['submit'])) {
$to = "contact#evanciniello.ca";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body);
}
?>
This is normal. In your /php/submit.php
if (isset($_POST['submit'])) {
if (empty($_POST["message"]) || empty($_POST["first_name"]) || empty($_POST["email"])) {
//Set up some error messages here and show that to the users.
} else {
$to = "contact#evanciniello.ca";
$subject = "Form Tutorial";
$name_field = $_POST['first_name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body);
if ($succes) {
//Now we need to redirect
$url = "http://www.evanciniello.ca/WHEREYOUWANTTOREDIRECT";
Header("Location: " . $url);
} else {
//Set up error message, that message can not be setn
die("Message can not be sent");
}
}
}
Add a name to the submit:
<input id="button" type="submit" class="submit" name="submit"></button>
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Hi guys this is my first question on stackoverflow.
I'm an amateur web designer designing a mostly static website..I need to create a contact form for user queries.What's the best approach for this task?
1)php's mailto function?
2)form data stored in a text file or spreadsheet?
3)database-connected(not preferred)
Any help would be really appreciated!!
To create a simple contact us form in php,you dont need to create any database. You have to use mail() function in PHP
You can refer following links to built simple contact us form :-
http://www.phpeasystep.com/phptu/8.html
http://www.freecontactform.com/email_form.php
OR
page1.php
<h2>Your Title</h2>
<form action="receiving.php" method="POST">
Name:<br><input type="text" name="name" size="40" /><br><br>
Email:<br><input type="text" name="email" size="40" /><br><br>
Phone:<br><input type="text" name="phone" size="40"><br><br>
Message:<br><textarea name="message" rows="3" cols="31" > </textarea><br><br>
<input type="submit" name="submit" value="Submit" />
<br><br>
</form>
receiving.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if(isset($_POST['submit']))
{
$from_add = "contactform#yourwebsite.com";
$to_add = "yourname#yourwebsite.com";
$subject = "Your Subject Name";
$message = "Name:$name \n Email: $email \n Phone: $phone \n
Message: $message";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent";
}
}
print "<p> Thank you $name for your message,
we will be in contact shortly. Click here
to continue </p>" ;
?>
NOTE :- You cannot send mail from localhost, configure some other smtp at localhost eg : google,yahoo...
Sample code of contact form using mail function
<?php
if (isset($_POST['action'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($name == "" || $email == "" || $message == "") {
echo "All fields are required, please fill the form again.";
} else {
$from = "From: $name<$email>\r\nReturn-path: $email";
$subject = "Message sent using your contact form";
mail("youremail#yoursite.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
<form action="" method="POST">
Your name:<br>
<input name="name" type="text" /><br>
Your email:<br>
<input name="email" type="text"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" name="action" value="Send email"/>
</form>
Simply build a html-form and send the data to a php-file. Database is not needed at all, unless you want to store all messages or IP.
A simple search will get you a lot of results.
In the phpfile you have two options: Use php's mail() as is, or try PHPmailer. The latter is a bit more complecated, but ends up less as spam, it sets headers and everything for you.
Especially when you mail from multiple pointer on your website, I recommend PHPmailer.
In reply to one of your comments:
All mailing happends on the server. PHP is a serverside language, all actions take place on the server, not on the users computer. For that reason, your code should always be on a server, or local with WAMP (or LAMP)
I need a very simple, but effective PHP form handler that will work with this form:
<form id="contact" method="post" action="contact_handle.php">
<label for="name">Name:</label>
<input type="text" class="text_style" placeholder="John Doe" name="name" /><br />
<label for="email">Email:</label>
<input type="email" class="text_style" placeholder="email#example.com" name="email" /><br />
<label for="message">Subject:</label>
<textarea class="areawidth" rows="4" name="message" /></textarea><br />
<button id="contactbutton" type="submit">Submit</button>
</form>
This is what I currently have but it doesn't send an email and it doesn't redirect like I intended. And I cant seem to figure out whats going on.
<?php
$invalid = '';
$my_email = 'my#email.com';
// Validate input:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$invalid.= "\n All fields are required";
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Validate email:
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email))
{
$invalid .= "\n Invalid email address";
}
// Send email if no errors detected:
if( empty($invalid))
{
$to = $my_email;
$subject = "Contact form submission: $name";
$body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message:\n $message";
$headers = "From: $email\n";
$headers .= "Reply-To: $email";
mail($to,$subject,$body,$headers);
//redirect to the thank you page:
header('Location: contact_thanks.php');
}
?>
When it is submitted it takes me to contact_handle.php that displays blank and fails to redirect.
i think the redirect has to be the first output to the browser, so if anything else is being output it won't work.
why don't you just include the thank you page rather than redirecting to it, ie.
include 'contact_thanks.php';
If you're providing all of the code, the issue is that your form is throwing an error (by adding text to $invalid, but there is no code to handle $invalid not being empty. To solve this, add this after the closing } at the end:
echo $invalid;