i do have a little problem with my contact.php :(
I needed for my page a contact form (very simple, just name, email and message).
It works perfectly but it sends the message even if the inputs are empty. My inputs do have "required" but it doesn't seem to work. My php skills are not that great. But you understand for sure where the problem is.
If there are empty fields, the form should not send the message and show an alert.
My current code:
HTML
<form method="post" id="myform" action="contact.php">
<div class="field half first">
<label for="name">Ihr Name</label>
<input type="text" name="name" id="name" required="required">
</div>
<div class="field half">
<label for="email">Ihre E-Mail</label>
<input type="text" name="email" id="email" required="required">
</div>
<div class="field">
<label for="message">Ihre Nachricht</label>
<textarea name="message" id="message" rows="5" required="required"></textarea>
</div>
<ul class="actions">
<li>
Senden
</li>
</ul>
</form>
contact.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'mail#gmail.com';
$subject = 'Nachricht von '.$field_name;
$body_message = 'Von: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Nachricht: '.$field_message;
$headers = 'Von: '.$field_email."\r\n";
$headers .= 'Antworte an: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Ich bedanke mich für Ihre Nachricht. Bald werde ich Sie kontaktieren./ Thank you for your message.');
window.location = 'http://';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Senden fehlgeschlagen./ Could not send the message');
window.location = 'http://';
</script>
<?php
}
?>
Any ideas, how to fix that small problem? :(
I think if your goal is to prevent someone send a blank message/contact, just use "required" in the end of your input tag like this :
<input type="text" name="something" required/>
or in your case, it should be like this :
<form method="post" id="myform" action="contact.php">
<div class="field half first">
<label for="name">Ihr Name</label>
<input type="text" name="name" id="name" required>
</div>
<div class="field half">
<label for="email">Ihre E-Mail</label>
<input type="text" name="email" id="email" required>
</div>
<div class="field">
<label for="message">Ihre Nachricht</label>
<textarea name="message" id="message" rows="5" required></textarea>
</div>
<ul class="actions">
<li>
Senden
</li>
</ul>
</form>
The "required" is a simple html5 tag attribute that helps you prevent someone send a blank form. I hope it works on you, make sure you are working with html5.
There is a function in PHP called empty(). You can combine this with the $_POST function like this example:
<?php
// The form is submitted.
if(isset($_POST["submit_button"])) {
// Now check if the posted input element is empty, if empty stop by echo a error message
// Otherwhise continue executing script
if(empty($_POST["form_name"])) {
echo "You forgot to fill in this form-element.";
}else{
// Continue
}
}
?>
Read more about this function: http://php.net/manual/en/function.empty.php
Check if the form is submitted using isset() function:
isset — Determine if a variable is set and is not NULL
Then, check if the fields are filled using empty() function:
empty — Determine whether a variable is empty
// this will return true if the Submit Button is clicked
if(isset($_POST["submit_button"])) {
if(empty($_POST['name']) {
echo "Name is not filled";
} else if (empty($_POST['email']) {
echo "Email is not filled";
} else if (empty($_POST['message']) {
echo "Message is not filled";
} else {
// Your original code
}
Related
I'm currently working on my portfolio. As of right now I'm trying to get the contact form to work. So I don't really know php that well and I'm not that good at it, but I've been googling and trying different ideas for myself. As of right now none of them have worked. Below I will attach the contact form html portion as well as the php I have now. Any help would be appreciated greatly
<section id="contact-form">
<!--Contact form section-->
<form method="POST" name="emailForm" action="index.php">
<!--Beginning of form and mail to my email address-->
<div class="contact-left">
<!--Contact info on the left side-->
<h1 class="c-l-heading">Contact</h1>
<!--Major heading-->
<div class="f-name">
<!--Name portion-->
<font>Name</font>
<input type="text" name="name" placeholder="Full Name" required/>
<!--Input field with a placeholder-->
</div>
<div class="f-email">
<!--email portion-->
<font >Email</font>
<input type="email" name="name" placeholder="Example#gmail.com" required/>
<!--Input field with a placeholder-->
</div>
</div>
<div class="contact-right">
<!--Contact info on the right side-->
<div class="message">
<!--Message portion-->
<font >Message</font>
<textarea name="message" rows="5" cols="20" placeholder="Write Message..." required></textarea>
<!--Textbox created-->
</div>
<button>submit</button>
<!--Submit button-->
</div>
</form>
</section>
PHP section now:
<?php
if(!isset($_POST['submit']))
{
echo "Error! You need to submit the form!";
}
//Collection
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validation
if(empty($name) || empty($visitor_email))
{
echo "Name and email are required"
exit;
}
$email_from = 'desmondlambkin17#gmail.com';
$email_subject = "dnjls.ca Contact Submission";
$email_body = "Name: $name\n"
"Email address: $visitor_email\n"
"Message: $message";
$to = "desmondlambkin17#gmail.com";
$headers = "From: $email_from";
//Send Email
mail($to, $email_subject, $email_body, $headers);
?>
Currently when I try to submit an email it says This page isn't working, and is currently unable to handle the request
I am working on my version 2 of my portfolio site, I had a working mailer I created with a guide about a year ago, transferred it, and can't get my page to display now.
Here is the code I am using:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo <form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>
}
?>
I ran it through a PHP syntax checker and it did not pull anything out, does anyone have any ideas?
For reference: Here is the original code I used on Version 1. I formatted it so it would display the email a little more cleaner, which probably royally screwed it up.
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My Portfolio Website"] ;
$message = $_REQUEST['message'].", Name: ".$name.", ".$phone.", Email: ".$email;
mail("TylerJStelmach#gmail.com", $subject, $message, "From:" . $email);
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo "<form method='post' action='index.php'>
<input type='text' input name='name' id='name' class='contacttext' placeholder=' Your Name' required>
<input type='text' input name='email' id='email' class='contacttext' placeholder=' Your Email Address' required>
<textarea input type='text' name='message' id='message' class='contacttext' placeholder=' Your Message' cols='55' rows='5' required></textarea>
<input type='submit' id='submit' class='submitcontacttext' value='Send Message'>
</form>";
}
?>
There are some syntax errors.
Also, your snipplet does not contain code to send mail.
Corrected code is as following:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
//if "email" is not filled out, display the form
echo '<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>';
}
?>
as all are saying where is the line to send mail but as i have seen your site there is no sign of any code.
as i am just guessing, even if the if is false, the else should run as it is running in my localhost.
the problem must be something with your sever.
put a error_reporting(E_ALL); on top it will show errors if it there.
else try writing in a new file.
<?php
error_reporting(E_ALL);
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST["My_Portfolio_Website"] ;
$message = $_REQUEST['message'] . "\nName: " . $name . "\nEmail: ".$email; "\n \nMessage: ".$message;
echo "<h1>Thank you for contacting me. I will get back to you ASAP!</h1>";
}else{
?>
<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<div class="metro-button" type="submit">Click me</div>
</div>
</form>
<?php
}
?>
I was able to figure this out by re-writing the original code I had based on the guide I used about a year ago, I came up with this for the answer which indeed works.
For all of the handling of where the information goes and how it was formated I included this chunk of php before I declared the doctype.
<?php
if($_POST["submit"]) {
$recipient="myemail#email.com";
$subject="Client Mail";
$name=$_POST["name"];
$email=$_POST["email"];
$email_from = $_POST['email'];
$message=$_POST["message"];
$mailBody="Name: $name\nEmail: $email\n\nMessage: $message";
mail($recipient, $subject, $mailBody, "From: $sender <$email>");
$thankYou="<p class='thank-you'>Thank you! Your message has been sent.</p>";
}
?>
From here, I figured out that the reason it wasn't submitting to it's own page was because my submit button was neither a button or input. I had it set as a div so with some tweaks here and there I rewrote the lower half to include an input as the submit. Which left me with this:
<?=$thankYou ?>
I included a small amount of text to display ( the $thankYou ) when you submit to the page and it reloads.
<form method="post" action="contact.php" class="connect">
<div>
<input id="name" name="name" type="text" required>
<label for="name">Your Name</label>
</div>
<div>
<input id="email" name="email" type="text" required>
<label for="email">Your Email</label>
</div>
<div>
<textarea id="message" name="message" required></textarea>
<label for="message">Your Message</label>
</div>
<div class="metro">
<input type="submit" name="submit" class="metro-button submit-me">
</div>
</form>
So now when submitted, the email comes in with:
The 'from' being set by $email
The 'subject' being set by $subject
and the message ($message) being formatted in this manner:
Name: John Doe
Email: JDoe#fake.com
Message: This is John Doe's message.
My apologies for the terrible question phrasing before hand, it was late at night and I was getting frustrated and losing my place, got a good nights rest and was able to solve it.
Here is the live version, I encourage you to test it, it works for me perfectly, hopefully this snippet can help someone else in the future!
I have a problem with checking if a email is valid. but the weird is that i have the same form on to different pages/urls, and on one of the forms it keeps saying that the email is invalid and on the form its valid.
The form on this page works - http://night.sendme.to/about
the form on this page doesnt - http://night.sendme.to/book/jokeren
The HTML on the forms is the same
<form action="" method="post" id="myform">
<div class="form-group">
<label for="name">Navn *</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Navn" required="required">
</div>
<div class="form-group">
<label for="corp">Virksomhed</label>
<input type="text" class="form-control" id="corp" name="corp" placeholder="Virksomhed">
</div>
<div class="form-group">
<label for="email">Email adresse *</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email adresse" required="required">
</div>
<div class="form-group">
<label for="tel">Telefon *</label>
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefon" required="required">
</div>
<div class="form-group">
<label for="message">Kommentar</label>
<textarea class="form-control" id="message" name="message" rows="10" required="required"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit">Send</button>
</form>
<div id="success" style="color:red;"></div>
The PHP is this
<?php // Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'YOURMAIL';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "from:".$_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $header); //This method sends the mail.
echo "Your email was sent!";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
} else {
echo "Invalid Email, please provide an correct email.";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
The javascript is this
$(document).ready(function(){
$('#submit').click(function(){
$.post("email.php", $("#myform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
Hope some one can help, why the form only works on the http://night.sendme.to/about and the others
So, to not leave this question answer-less:
In your HTML code, you actually had <form action="" method="post" id="myform"> in both pages – but in your second page, you had another <form> tag right before it … and because of this invalid HTML, the browser ignored the second form tag, and that made $("#myform").serialize() not return any data at all, because it could not find the form element with that id.
You should always validate your HTML code. This helps avoiding such errors.
So I recently uploaded my first website to an iPage server. The website runs perfectly with the exception of the Contact Form which for some reason refuses to send any email whatsoever from the form.
I use a PHP Script with the Post Method, and tried to fix my code multiple times to correct any error I might have entered, but so far to no avail. Here is the code:
HTML:
<
form action = "js/mailer.php" form method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
mail($to, $subject, $body);
// redirect to confirmation
header('Location: confirmation.html');
} else {
echo "Failure";
}
?>
Can someone help me with this? It might be an error with my hosting server, or an error with my code. Your help is very much appreciated
Additional Comments: Followed the help provided (thank you for that) I made the necessary changes to the HTML and PHP, but the form is still not functional. It does not echo failure or redirect to the confirmation page, and upon inspecting the element with Firefox, I notice that upon hitting the submit button a subdivision appears under it saying "Sending....". But no email is sent, no message is echoed or page opened.
From what i see in the code you posted, the PHP mailing script won't work as you are checking if a POST variable with the name 'submit' exists which it does not as in your form the submit button does not have a name attribute.
Try giving the submit button a name and put that name in the PHP if statement.
Submit button name is missing in your form, You need to add name into your submit button,
<input type="submit" name="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
^^^^^^^^^^^^
instead of
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
Your HTML part should be something like below,
<form action = "js/mailer.php" method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="sendemail" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
In above, I have added name="sendemail" in submit button.
And your PHP script that sends email should be like
<?php
if(isset($_POST['sendemail'])) { // <-- variable name changed
$to = "aravindm3095#gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
$headers = "From: " . $email_field; // <-- Code added
mail($to, $subject, $body, $headers); // <-- Code added
// redirect to confirmation
header('Location: confirmation.html');
exit;
} else {
echo "Failure";
}
?>
I have this PHP contact form script (as shown below) which I have used before so I know works, BUT, due to that I have hidden the html form inside of a new jQuery powered div:
(<div class="toggle hidemail" id="drop-button" href=""> Email us here </div>
<div class="hidden hiddenform">)
that toggles up and down, I cant seem to send the form at all, the form when sent is supposed to fade out then give the user a confirmation message (as shown in the script) but it doesn't do anything, I don't suppose anyone would know whats going wrong here so I can get this form working again?
<?php
$to = 'example#gmail.com';
$subject = 'Company Name';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hello my name is $name.
$message
From $name
my email address is $email
EMAIL;
if($name == '' || $email == '' || $message == "") {
echo "<h5>Sorry, I think you missed bit....</h5>";
$error = true;
} else {
$error = false;
mail("example#gmail.com", $name, $email, $message);
echo "<h5>Thank you for getting in touch. I'll get back to you ASAP.</h5>";
}
if($error == true):
?>
<article id="contact-form">
<article id="load_area">
<form id="my-form">
<input type="text" name="name" placeholder="Name" />
<input type="text" name="email" placeholder="Email" />
<textarea name="message" placeholder="Message" /></textarea>
<input type="button" class="submit_button" value="Send" name="send_button" />
</form>
</article>
</article>
<?php
endif;
?>
Here is the HTML :
<div class="toggle hidemail" id="drop-button" href=""> Email us here </div>
<div class="hidden hiddenform">
<article id="contact-form">
<article id="load_area">
<form id="my-form">
<input type="text" name="name" placeholder="Name" />
<input type="text" name="email" placeholder="Email" />
<textarea name="message" placeholder="Message" /></textarea>
<input type="button" class="submit_button" value="Send" name="send_button" />
</form>
</article>
</article>
</div>
$(document).ready(function() {
$(".submit_button").live("click", function() {
$("#load_area").fadeOut('slow', function() {
$("#load_area").fadeIn();
$.post("process.php", $("#my-form").serialize(), function(data) {
$("#load_area").html(data);
})
});
})
})
Try using
$(".submit_button").on("click", function(){
instead of '$(".submit_button").live("click", function(){
From jQuery Docs:
As of jQuery 1.7, the .live() method is deprecated
also try to use
$('#my-form').submit(function() {
alert('Handler for .submit() called.');
return false;
});
and set an action in your html form like so:
<form id="my-form" action="process.php">