Form fires every time Website is Accessed [closed] - php

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
My website has a form that fires every time the website is accessed. It's still in testing phases so I get over 100 BLANK emails a day just from refreshing the page (and yes, all of the inputs are required). The tricky part is that I have a jquery script included. The script fires when you hit the submit button: it then refreshes the page and scrolls down (using a hashtag) to below my form with a message that basically says "thanks for emailing me!"
My code is posted below, but what I need to know is why I keep getting these blank emails even when the input fields are required! I am still very very new to php and jquery.
<?php
$to = 'design#carolbarone.com' ;
$subject = $_POST['subject'] ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$text = $_POST['message'] ;
$message = "From: $name \nEmail: $email \nMessage: $text \n";
$sent = mail($to, $subject, $message) ;
if($sent) {
echo "";
}else{
echo "";
}
?>
<form data-abide name="input" action="index.php#hashtag" method="Post" id="theForm">
<div class="row">
<div class="small-10">
<div class="row">
<div class="small-12 columns name-field">
<input type="text" name="name" required id="right-label" placeholder="Name">
<small class="error">Name is required.</small> </div>
</div>
<div class="row">
<div class="small-12 columns email-field">
<input type="email" name="email" required id="right-label" placeholder="E-mail Address">
<small class="error">An email address is required.</small> </div>
</div>
<div class="row">
<div class="small-12 columns">
<input type="text" name="subject" required id="right-label" placeholder="Subject">
<small class="error">A subject is required.</small>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<textarea name="message" placeholder="Your Message Here" rows="4" required></textarea>
<small class="error">A message is required.</small> </div>
</div>
</div>
</div>
<br/>
<button type="submit" name="submit" value="submit">Submit</button>
<button type="reset">Reset</button>
</form>
<br/>
<div class="success_message">
<h3>Thank you for your message!</h3>
<p>Your email has been sent successfully and I appreciate you getting in touch with me. I will be sending a reply soon.</p>
</div>
<script>
$(document).ready(function() {
if(window.location.hash == '#hashtag') {
$('.success_message').show();
$("html, body").animate({ scrollTop: $('#theForm').offset().top }, 1000);
}
});</script>

You never bothered fencing off your code to check if a form submission was actually performed, so the code will fire EVERY time the page is loaded. You'd want something at least like:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... handle form ...
}

They're required on the browser side, but you have no server-side validation. Older browsers that don't respect HTML5's required attribute, bots, etc. will happily submit all day long.
At its simplest, just check that there's data in each field:
$to = 'design#carolbarone.com' ;
$subject = $_POST['subject'] ;
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$text = $_POST['message'] ;
$message = "From: $name \nEmail: $email \nMessage: $text \n";
if($subject && $name && $email && $text) {
$sent = mail($to, $subject, $message) ;
...
You'd want to do more validation (like making sure $email is a valid format) but this'll at least prevent blank ones. Of note: your form is vulnerable to header injection. Using a proper library like SwiftMailer will make coding email easier as well as protecting you from malicious spambots somewhat.
As Marc B notes, by including the mailing code on the same page as the form, you're firing it whenever someone accesses that page. Typically, your POST handling should be in a different file/route.

Related

PHP script keeps sending Email evertime the page is loaded

I have put together a simple contact form for my website. Using PHP to POST the data and send directly to my email address. But for some reason every time I visit the page on my website. I still get the test message Displaying under the Form. Then when I reload the website and visit the link again it still displays the thank you message. and automatically sends an email. Im still in testing mode 2 days before my launch and I need this figure out. Considering I am novice to php I dont know what goes where.... check out my website to get a live view https://trillumonopoly.com (click "Contact Us" link in menu) I would like for the contact form to disappear and echo the thank you message once sent. And reset after the page is reloaded. I am also using Jquery ajax to load all my pages into a div container. So I would like to keep the content inside that div without forwarding to the Echo message page, leaving my index page
Heres My ajax code
$(document).ready(function () {
loadMainContent('main');
$('body').delegate('.navMenu', 'click', function (event) {
event.preventDefault();
loadMainContent($(this).attr('href'));
});
});
function loadMainContent(page) {
$('#main').load('pages/' + page + '.php');
}
here is html for the form:
<div class="general row container-fluid"><br>
<center><img src="img/divider.png" class="img-fluid"></center>
<div class="col-lg-6 col-sm-12">
<img src="img/logo.png" class="img-fluid" height="540px" width="540px">
</div>
<div class="col-lg-6 col-sm-12 container"><center>
<br><h1 class="form-title">Contact Us</h1><br></center>
<div class="container">
<form action="pages/mail.php" method="GET" class="box2">
NAME:
<input type="text" name="name" placeholder="YOUR NAME HERE" required>
<br><br>
EMAIL:
<input type="email" name="email" placeholder="YOUR EMAIL HERE" required>
<br><br>
MESSAGE:<br>
<textarea name="message" rows=10 cols=23 placeholder="YOUR MESSAGE HERE" required></textarea>
<br><Br>
<button type="submit" value="Message Sent" class="btn btn-danger btn-lg" style="background-color:"red">SUBMIT</button
</form>
<center><?php include('mail.php'); ?></center>
</div>
</div>
</div>
Here is My simple PHP:
<?php
$name = $POST['name'];
$email = $POST['email'];
$message = $POST['message'];
mail("info#trillumonopoly.com","ILLUMONOPOLY WEB Contact", $message,"From: $email\r\n");
echo "Thank You For Contacting Us!";
?>
Better than check everything before send mail.
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])){
$name = $POST['name'];
$email = $POST['email'];
$message = $POST['message'];
mail("info#trillumonopoly.com","ILLUMONOPOLY WEB Contact", $message,"From: $email\r\n");
echo "Thank You For Contacting Us!";
}
And send POST request to the index, not mail.php.
<form action="" method="POST">
...
So the user can see your message at the end of contact form.

How to create php form handler and intergrate with existing html web form? [duplicate]

This question already has answers here:
Send email with PHP from html form on submit with the same script
(8 answers)
Closed 7 years ago.
So here is the problem I am facing. I have created a pretty simple web form:
<form method="post" action="#">
<div class="field"> <label for="name">Name</label>
<input name="name" id="name" type="text"> </div>
<div class="field"> <label for="email">Email</label> <input
name="email" id="email" type="email"> </div>
<div class="field"> <label for="message">Message</label> <textarea
name="message" id="message" rows="4"></textarea> </div>
<ul class="actions">
<li><input value="Send Message" type="submit"></li>
</ul>
</form>
I need to know how I can use this form to send data inputed by the user to my email address admin#nue-tech.uk I am aware this can be done in PHP but am unsure how to approach this as I am unfamilliar with PHP. If someone could please point me in the right direction as to how this can be done, and also where I should place the PHP file relative to this, that'd be awesome!
You could copy the code below and paste it in your HTML file below your html. In your html you set the action attribute empty. Don't forget to change your file extension to .php
<?php
if(isset($_POST['submit'])){
$to = "admin#nue-tech.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Blablabla"; //Write whatever you want here
$message = $name . "wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('location: thank-you.html'); //redirects the user to another page if the mail was send succesfully
} else {
header('location: contact.html'); //if it was not send succesfully it redirects to the contact page again
exit(0);
}
?>
In
header('location: contact.html');
you could also use
echo "Something went wrong. Try again later"
or something simular.
I would highly recommend you search and learn on W3Cschools or at php.net.

How do I make contact form success message appear in same page

My website is one pager with nav that links to different parts of the page within the same document. So my contact is at stie.com/#contact rather than site.com/contact.html
I have my contact form coded in html using post method linking to mail.php. Upon hitting the submit button I get redirected to site.com/mail.php where the "Your message was succesfully sent" is displayed. How do I get it so that it displays right on top of the contact form since I don't have a contact.html file to turn into a contact.php and put the php code right where I want the success message to display?
<div class="row">
<div class="12u">
<form method="post" action="mail.php">
<div>
<div class="row half">
<div class="6u">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="6u">
<input type="email" name="email" id="email" placeholder="Email" />
</div>
</div>
<div class="row half">
<div class="12u">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
Send Message
Clear Form
</div>
</div>
</div>
</form>
</div>
My Mail.php
<?php
//GET INFO FROM CONTACT FORM
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST ['subject'];
$message = $_POST['message'];
$from .= $_POST ['email'];
$to = 'email#site.com';
// compose headers
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
//POST SUBMIT
if ($_POST['sumbit']);
if ($name != '' && $subject != '' && $message !='' && $email != '') {
if (mail ($to, $subject, $from, $message, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>Please fill in all required fields!!</p>';
}
?>
You can use URL parameters with PHP:
<?php
$confDisplay = 'display:none;';
// if the url param exists, display confirmation
if(isset($_GET["confirm"]) && $_GET["confirm"]==true){
$confDisplay = 'display:inline;';
}
?>
...
<div style="<?php echo $confDisplay; ?>">
Your form has been submitted!
</div>
...
Just set your form action URL to the same page with ?confirm=true at the end.
Make your action field empty. Put action="" instead of action="mail.php" Then include your mail.php content inside your contact page. As you know, you have to save that page as PHP, too; for example, mycontactform.php. In this way you have more control over the content and format of the "your message submitted" message. If you separate mail.php you can't address divisions in the mycontactform.php.
Security and vulnerability of PHP codes you are using should be addressed after you have completed the page coding and tested it as up and running in your desired format, since it needs more in-depth study of PHP conventions and usages. source: A Set of Step by Step Tutorials Using HTML5, CSS3 and PHP (8)
Note that your script mail.php is vulnerable to headers injection attack. You need to escape your variable $_POST['email']. You have to remove the special characters \n and \r. This can be made easily by using the str_replace function.

Javascript Form Submit with Email Action?

I need make this form send me a email like a contact form:
Script code:
<script type="text/javascript">
$(document).ready(function(){
$("#contactLink").click(function(){
if ($("#contactForm").is(":hidden")){
$("#contactForm").slideDown("slow");
}
else{
$("#contactForm").slideUp("slow");
}
});
});
function closeForm(){
$("#messageSent").show("slow");
setTimeout('$("#messageSent").hide();$("#contactForm").slideUp("slow")', 2000);
}
</script>
HTML CODE:
<div class="box">
<div id="contactFormContainer">
<div id="contactForm">
<fieldset>
<label for="Name">Nome: </label>
<input id="name" type="text" />
<label for="Telefone">Telefone Fixo: </label>
<input type="text" id="phone" maxlength="15" onkeypress="Mascara(this);" />
<label for="Message">Assunto:</label>
<textarea id="Message" rows="3" cols="20"></textarea>
<input id="sendMail" type="submit" name="submit" onclick="closeForm()" />
<span id="messageSent">Sua solicitação foi enviada com sucesso, por favor, aguarde...</span>
</fieldset>
</div>
<div id="contactLink"></div>
</div>
When click and close the form i need send me a email with the content of form, how to?
Some idea? thanks!
Firstly i can't see the form tags in your code. According to me you're doing this wrong and i'm sure many of our friends on stack will agree too.
Your question suggests that you basically want to receive an email with the data submitted through the form. Why don't you try the below method.
HTML
<form action="mail.php" method="POST">
<input type="text" name="fname"></input>
<input type="text" name="lname"></input>
<button>SUBMIT</button>
</form>
PHP
<?php
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$to = "someone#example.com";
$subject = "Hello World";
$message = "Firstname: $firstname \n\n Lastname: $lastname";
$from = "sender#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
The above example is the most simplest method of sending an email. You can go advance by adding more header information and graphically formatting the email.
Go through these tutorials if you get confused.
http://www.w3schools.com/php/php_mail.asp
http://www.phpeasystep.com/phptu/8.html
And since you mentioned that you want to perform the task via javascript you can try submitting the form via ajax, refer the below tutorials
http://teachingyou.net/php/simple-php-contact-form-using-ajax/
http://www.sitepoint.com/forums/showthread.php?1055068-Send-PHP-email-using-jQuery-AJAX
Since you've tagged the question php, have a look at php's mail function. http://php.net/manual/en/function.mail.php
$to = 'you#domain.com';
$subject = 'Contact Form';
$message = '...' //concatenate the $_POST (or $_GET) variables to create this message
mail($to, $subject, wordwrap($message, 70, "\r\n");
This function requires that your server has a properly configured to send mail - see the php documentation for requirements: http://www.php.net/manual/en/mail.requirements.php

Do not understand why my Contact Form isn't sending emails

I am trying to make a email send in a pop up on my site that you can see with the link below:
http://www.madaxedesign.co.uk
However it redirects perfectly to the thank you message however after it has redirected it does not implement the PHP. Below I have shown the PHP, HTML and Jquery used for this contact form.
HTML:
<form id="submit_message" class="hide_900" action="/send.php" method="post">
<div id="NameEmail">
<div>
<label for="name">Name*</label>
<input type="text" title="Enter your name" name="name"/>
</div>
<div>
<label for="email">Email*</label>
<input type="text" title="Enter your email address" name="email"/>
</div>
</div>
<div id="MessageSubmit">
<div>
<textarea maxlength="1200" title="Enter your message" name="message"></textarea>
<label for="message">Message</label>
</div>
<div class="submit">
<input type="submit" value="Submit"/>
</div>
</div>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "maxlynn12#gmail.com";
$subject = "Email From Madaxe";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: /thanks.html');
exit();
?>
Jquery:
$('form#submit_message').live('submit', function() {
$('#popup').load('/thanks.html');
return false;
});
I was wondering if anyone could quickly look and see if I am missing anything obvious that I can quickly fix or even point me in the right direction.
Thanks
Your jQuery is interfering.
I think this might help, using AJAX to post your form: jQuery Ajax POST example with PHP
You do not need to your jquery code. actually it prevents your default action form action=send.php. and it does not pass your inputs to send.php
$.live() is deprecated in jQuery 1.9 and that's what you appear to be using. Please either downgrade or use an alternative function like .submit().
$('#submit_message').submit(function (e) {
e.preventDefault();
$('#popup').load('/thanks.html');
}
Also, it is crucial that you sanitise your $_POST inputs before using them for your e-mail headers, otherwise a hacker can inject bad things into your headers.
if you are trying to send mail through localhost you need to change some setting in php.ini file. Refer below link to do this.
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/

Categories