Php contact form errors - php

I'm building a php contact form that checks if some fields are filled it or not (no ajax).
The form is not working for 100%. I will try to explain. When I submit my form, The user navigates to http://mydomain.com/send.php
There is receive my error messages (if there are any). When I refresh the send.php page I receive the following errors: Notice: Undefined index: name in /Library/WebServer/Documents/~aledvertising/send.php
Here is my code
html form
<form method="post" action="send.php">
<div id="form-top">
</div>
<div id="form-left">
<label>Naam:<span class="star">*</span></label>
<input name="name" placeholder="Uw naam">
<label>Email:<span class="star">*</span></label>
<input name="email" type="email" placeholder="Uw email">
<label>Hoeveel is 2+2? (Anti-spam)<span class="star">*</span></label>
<input name="human" placeholder="Uw antwoord">
</div>
<div id="form-right">
<label>Uw bericht:<span class="star">*</span></label>
<textarea name="message" placeholder="Uw bericht"></textarea>
</div>
<input id="submit" name="submit" type="submit" value="Verzenden">
</form>
SEND.PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
?>
<?php
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
Can somebody please help me to get a full working form? Thank you

You should see isset method from php.
<?php
if(isset($_POST["name"]))
{
//code here
}
?>
The problem is that when you reload the page, the server side code looks for the index name and it does not find it because name does not have any value yet. The value is received once you submit the page. So that's why the error is there.
update
The error is that when you submit the form, there are some fields that may be send empty, so once that the post values goes to send.php there are $_POST that will be expecting values. if some of them do not recive does values, it will tell you: "Hey, i do not have values, so i im null". That why you have to check that the fields are all filled or make a validation on server side.
<?php
if(isset($_POST["name"],$_POST["last_name"],etc,etc))
{
//if everything ok, ill go on.
}
else{
//if there are empty field, go back
}
?>
UPDATE 2
if (isset($_POST['name'],$_POST['email'],$_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}

<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
setting the isset() function and then declaring the variables worked for me, tested it in my server, not giving any errors and form is submitted successfully...
please check it once...

Related

Php form does not send [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I've tried to built a php form. However, it won't send.
html (index.html):
<div class="row">
<div id="Contact" class="container">
<h1>Meer weten?</h1>
<h2>Neem vrijblijvend contact op</h2>
<div id="ContactBlock">
<form class="contactform" method="post" action="submitform.php">
<div class="FormTop">
<div class="half-left-cf">
<input type="text" id="input-name" name="name" placeholder="Naam (verplicht)">
<input type="text" id="input-email" name="email" placeholder="Email address (verplicht)">
<input type="text" id="input-telnr" name="telnr" placeholder="Telefoonnummer">
</div>
<div class="half-right-cf">
<textarea name="message" type="text" id="input-message" name="message" placeholder="Uw bericht"></textarea>
</div>
</div>
<label>*Hoeveel is is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Antwoord hier">
<input type="submit" value="Verzenden" id="input-submit">
</form>
</div>
</div>
php (submitform.php):
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$telnr = $_POST['telnr'];
$message = $_POST['message'];
$from = 'From: Contact Form';
$to = 'e-mail';
$subject = 'Hello';
$human = $_POST['human'];
$body = "Afkomstig van: $name\n E-Mail: $email\n Telnr:\n $telnr Bericht:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Uw bericht werd goed verzonden. Wij contacteren u zo snel mogelijk.</p>';
} else {
echo '<p>Er is iets misgegaan, probeer a.u.b. opnieuw!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>U heeft de anti-spam vraag niet correct ingevuld</p>';
}
} else {
echo '<p>U heeft niet alle verplichte velden ingevuld</p>';
}
}
?>
When I press submit the browser returns a blanc page with /.../submitform.php in the url-bar.
This happens both in an online environment as well as on localhost.
Am I doing something wrong? Thanks
If you not sure about your folder structure just use this simple trick.
Change post url to absolute one like http://www.example.com/xyz/submitform.php
In local
http://www.localhost/xyz/submitform.php
Instead of trying simple submitform.php

Norwegian letters "æ, ø, å" not showing when contact form is submitted

I have created a contact form, you can view it here. When I fill out the contact form and go to my inbox folder - the Norwegian letters æ, ø, å aren't shown in the message.
This is what I have currently added:
<meta http-equiv="content-type" content="text/html" charset="ISO-8859-1">
I have also tried <form accept-charset="ISO-8859-1">, but with no luck.
Here is a screenshot from the e-mail I receive after contact form has been submitted
as you can see there are no æ, ø, å letters.
Is there any way I can fix this?
PHP Code:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$number = $_POST['number'];
$from = 'Ny melding sendt fra kontaktskjema på Helsespesialisten.no';
$to = 'test#test';
$subject = 'Helsespesialisten | Du har motatt en ny melding';
$body = "Fra: $name\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Vennligst skriv inn ditt navn';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vennligst skriv inn din e-post';
}
//Check if message has been entered
if (!$_POST['number']) {
$errNumber = 'Vennligst skriv inn ditt telefonnummer';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Vennligst skriv en melding';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
if (mail ($to, $subject, $body, $from, $number)) {
$result='<div class="alert alert-success">Takk for din henvendelse! Vi tar kontakt i løpet av kort tid!</div>';
} else {
$result='<div class="alert alert-danger">Beklager, en feil skjedde! Kontakt oss på: +47 35 11 15 40</div>';
}
}
}
Try this:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$number = $_POST['number'];
$from = 'Ny melding sendt fra kontaktskjema på Helsespesialisten.no';
$to = 'test#test';
$subject = 'Helsespesialisten | Du har motatt en ny melding';
$headerFields = array(
"From: $from",
"MIME-Version: 1.0",
"Content-Type: text/html;charset=utf-8"
);
$body = "Fra: $name\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Vennligst skriv inn ditt navn';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vennligst skriv inn din e-post';
}
//Check if message has been entered
if (!$_POST['number']) {
$errNumber = 'Vennligst skriv inn ditt telefonnummer';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Vennligst skriv en melding';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
if (mail ($to, $subject, $body, implode("\r\n", $headerFields))) {
$result='<div class="alert alert-success">Takk for din henvendelse! Vi tar kontakt i løpet av kort tid!</div>';
} else {
$result='<div class="alert alert-danger">Beklager, en feil skjedde! Kontakt oss på: +47 35 11 15 40</div>';
}
}
}

Input form doesn't parse error and displays blank page on submit

EDIT: Changed PHP to recommended code, still can't get success/error to display on page.
I'm trying to get this contact page to work, but it seems to be breaking when I click the submit button. As soon as I click, it redirects me to /contact_form.php and shows a blank page. If all forms have been correctly filled in, the message does get sent. If any of them are wrong or not filled in, the same blank page is shown.
HTML:
<form method="post" action="contact_form.php">
<div class="input-group-lg">
<label for="name">Your Name<i>*</i></label>
<input name="name" type="text" class="form-control" placeholder="What should I call you?">
</div>
<div class="input-group-lg">
<label for="email">Email Address<i>*</i></label>
<input name="email" type="email" class="form-control" placeholder="Email Address">
</div>
<div class="input-group-lg">
<label for="phone">Phone Number</label>
<input name="phone" type="text" class="form-control" placeholder="Phone Number">
</div>
<div class="input-group-lg">
<label for="message">Message<i>*</i></label>
<textarea name="message" class="form-control" placeholder="What's on your mind?"></textarea>
</div>
<div class="input-group-lg">
<label for="human">What is 2 + 2? (Anti-spam)<i>*</i></label>
<input name="human" type="text" class="form-control" placeholder="2 + 2 = ?">
</div>
<div class="text-center">
<?php echo $result; ?>
</div>
<div class="text-center">
<input name="submit" type="submit" value="Send Message" class="btn btn-custom"></input>
</div>
</form>
PHP:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 4) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
}
else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
}
?>
Try this. I havnt tested it, but this should work like a charm. If you still get errors please post them.
if (isset ($_POST['name'])){ //RUN ONLY IF name IS SET
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
// Error handling for missing data
if ((!$name) || (!$email) || (!$phone) || (!$message) || ($human !== 4)){
$result = '<div class="alert alert-danger"><i class="glyphicon glyphicon-warning-sign"></i> <strong>ERROR:</strong> You did not submit the following required information:</div>';
if(!$name){
$result .= '<div class="alert alert-danger"><strong>Please enter your name</strong></div>';
}else if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$result .= '<div class="alert alert-danger"><strong>Please enter a valid email address</strong></div>';
}else if(!$phone){
$result .= '<div class="alert alert-danger"><strong>Please enter a phone number</strong></div>';
}else if(!$message){
$result .= '<div class="alert alert-danger"><strong>Please enter your message</strong></div>';
}else if($human !== 4){
$result .= '<div class="alert alert-danger"><strong>Please correct your addition</strong></div>';
}
} else { // Error handling is ended, process form and send email
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
mail($to, $subject, $body, $from);
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} // Close else after duplication checks
} else { // if the form is not posted with variables, place default empty variables so no warnings or errors show
$name = "";
$email = "";
$phone = "";
$message = "";
$human = "";
$result = "";
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
You have no print or echo statement in your PHP file ...
add echo $result to the end of your PHP file to see the results
Change your success code this. This will print your success or error msg on page reload.
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
Determine if a variable is set:
if (!isset($errName) && !isset($errEmail) && !isset($errMessage) && !isset($errHuman)) {
if (mail ($to, $subject, $body, $from)) {
$result='í<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
if(isset($result)){
echo $result;}

How to get a popup box after submitting a form?

[SOLVED] Thanks everyone that replied.
I've got a PHP script that is run after a user submited a contact form.
This script shows a message in the top left corner saying if it worked properly or if it encountered a problem.
Now my question is, how can I put those messages into a popup box?
I know there is JS involved, but I barely have knowledge of that.
( Example: http://www.dylanvanheugten.nl/contact.php)
PHP
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Site Contact';
$to = 'info#dylanvanheugten.nl';
$subject = $_POST['subject'];
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $subject != '' && $message != '') {
if ($human == '12') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>';
} else {
echo '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>';
}
} else if ($_POST['submit'] && $human != '12') {
echo '<p>U heeft de spam preventie som foutief beantwoord.</p>';
}
} else {
echo '<p>Alstublieft alle velden invullen.</p>';
}
}
HTML
<form method="post" action="contact.php">
<label>Naam</label>
<input name="name" placeholder="Naam">
<label>Email</label>
<input name="email" type="email" placeholder="Email">
<label>Onderwerp</label>
<input name="subject" placeholder="Onderwerp">
<label>Bericht</label>
<textarea name="message" placeholder="Laat hier ook uw telefoonnummer achter als u telefonisch contact wilt."></textarea>
<label><strong>*Spam preventie*</strong><br/>Wat is 11+1?</label>
<input name="human" placeholder="11 + 1 =">
<input id="submit" name="submit" type="submit" value="Verzend">
<label><u>Alle velden zijn verplicht.</u></label>
</form>
Enclose your echo with <script> tags like this.
echo '<script>alert("Alstublieft alle velden invullen");<script>';
In the same way that you can print HTML from PHP, you can 'print' javascript. Here's an example with an alert box:
print("<script>window.alert('This is a javascript alert from PHP');</script>");
You are able to put your PHP variables inside of the statement, as well, if you need to.
You can simply alert msg like,
$msg='';
if ($_POST['submit']) {
if ($name != '' && $email != '' && $subject != '' && $message != '') {
if ($human == '12') {
if (mail ($to, $subject, $body, $from)) {
$msg='Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug';
} else {
$msg='Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.';
}
} else if ($_POST['submit'] && $human != '12') {
$msg='U heeft de spam preventie som foutief beantwoord.';
}
} else {
$msg='Alstublieft alle velden invullen.';
}
echo '<script>
alert("'.$msg.'");
</script>';
// you can replace the above javscript code to any plugin like jquery ui modal box
}
Add some javascript to the top of your html markup where you define a method to display a popup. Something like this:
<script type="text/javascript">
function displayPopup()
{
alert("Form submitted!");
}
</script>
Then within your PHP when the form has been submitted, just call the method displayPopup() method within a script tag.
You can certainly optimise the approach above, but it should give you somewhere to start.
Store the error message in a variable and simply echo them in the required div.
if ($_POST['submit'])
{
if ($name != '' && $email != '' && $subject != '' && $message != '')
{
if ($human == '12')
{
if (mail ($to, $subject, $body, $from))
{
$message = '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>';
} else
{
$message = '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>';
}
} else if ($_POST['submit'] && $human != '12')
{
$message = '<p>U heeft de spam preventie som foutief beantwoord.</p>';
}
}
else
{
$message = '<p>Alstublieft alle velden invullen.</p>';
}
}
And in your HTML, you can create a and display the error inside it:
<div id="errorMessage"> <?php echo $message; ?> </div>
Try with this code
if ($query) {
echo "<script type='text/javascript'>alert('data Updated successfully!'); window.location.href = 'home.php';</script>";
} else {
echo "<script>alert('ERROR! Something Went wrong. Try Again')</script>";
}

contact form not working well with non English characters

I have the following contact form:
(HTML with PHP)
<form method="post" action="index.php">
<p>
<label>Namn</label>
<input name="name" placeholder="Skriv här">
<label>Epost adress</label>
<input name="email" type="email" placeholder="Skriv här">
<label>Meddelande</label>
<textarea name="message" placeholder="Skriv här"></textarea>
<label>Hur mycket är 2+2? (Anti-spam)</label>
<input name="human" placeholder="Skriv här">
</p>
<p>
<input id="submit" name="submit" type="submit" value="Submit">
</p>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Från: Forall.se';
$to = 'info#forall.se';
$subject = 'Ny meddelande';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Ditt meddelande har skickats!</p>';
} else {
echo '<p>Någonting gick fel. Var vänlig och försök igen!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>Du har gett fel svar på Anti-Spam frågan!</p>';
}
} else {
echo '<p>Du behöver att fylla alla fält i formuläret!</p>';
}
}
?>
</form>
Everything is woring as it should, except non English characters like ã, õ, ä, å, ö, ç and so on. This shouldn't be a problem if it wasn't a swedish website.
Here's a sample of the last e-mail I received:
"Jag hoppas du har förståelse för detta och att du hittar någon annan marknadsförare!
Jag återkommer när och om jag startar upp min firma, just nu är det lite osäkert hur det blir med dettas"
How do I solve this issue?
you need to add headers
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Från: Forall.se';
$to = 'info#forall.se';
$subject = 'Ny meddelande';
$human = $_POST['human'];
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "From: $from <$email> ". PHP_EOL;
$headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;
$name = str_replace( '[at]','#', $name);
$message = str_replace( '[at]','#', $message);
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
if ($human == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Ditt meddelande har skickats!</p>';
} else {
echo '<p>Någonting gick fel. Var vänlig och försök igen!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>Du har gett fel svar på Anti-Spam frågan!</p>';
}
} else {
echo '<p>Du behöver att fylla alla fält i formuläret!</p>';
}
}
?>
PHP mail function 4th parameter isn't exclusive to send a from value. Acording with PHP's manual:
-This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).
Including charset which will encode the text of the message. In your case, as the comments says, you'll have to use UTF-8.
So, your code would be like this:
$header = "From: somebody#example.com\r\nContent-Type: text/plain; charset=UTF-8";
$message = $_POST['message'];
$to = 'info#forall.se';
$subject = 'Ny meddelande';
mail ($to, $subject, $body, $header)

Categories