How to prevent form submission if message contain links? - php

can anyone help me? I'm trying to prevent spam in the contact form.
I would like to achieve this with PHP. I can't find any solution on the web. Thanks in advance for who can help me out.
HTML
<form name="myForm" id="myForm" method="post" action="contact-form.php">
<input type="text" name="name" value="" class="form-control mt-3" id="name" placeholder="Full Name" required>
<input type="email" name="email" value="" class="form-control mt-3" id="email" placeholder="Email" required>
<textarea class="form-control mt-3" name="message" id="message" placeholder="Type your message" rows="7" required></textarea>
<input type="text" id="Nomoboto" name="Nomoboto" value="800 800 1000" autocomplete="off" />
<input type="text" id="PostItBoto" name="PostItBoto" value="93940" autocomplete="off" />
<input type="text" id="Empty_Me" name="Empty_Me" value=""/>
<input type="text" id="Empty_You" name="Empty_You" value=""/>
<button type="submit" class="btn btn-primary float-right mt-3">Send Message</button>
</form>
PHP
<?php
$name = $_POST ['name'];
$email = $_POST ['email'];
$message = $_POST ['message'];
$Empty_Me = $_POST ['Empty_Me'];
$Empty_You = $_POST ['Empty_You'];
$email_from = 'Contact Form';
$email_subject = "New Form Submission";
$email_body = "User Name: $name.\n".
"User Email: $email.\n".
"User Message: $message.\n".
"Empty_Me: $Empty_Me.\n".
"Empty_You: $Empty_You.\n";
function email_validation($str) {
return (!preg_match(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^", $str))
? FALSE : TRUE;
}
$to = "info#mail.com";
$headers = "From $email_from \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");

Related

Bad conection between contact html and mail php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
CSS y PHP of my contact-form.
The page works perfectly but I'm trying to send an email and the button of send doesn't work. In the image you can see that when I try to send an email the button stays white. At the same time the button Clear works perfect! I attach my code
CONTACT.HTML
<form action="mail.php" method="POST" class="contact-form">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email address" class="contact-form-email required">
<input type="text" name="subject" placeholder="Subject" class="contact-form-subject">
<textarea name="message" placeholder="Message" class="required" rows="7"></textarea>
<div class="response-message"></div>
<button class="border-button" type="reset" id="reset" name="reset">Limpiar</button>
<button class="border-button" type="submit" id="submit" name="submit">Enviar</button>
MAIL.PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "sa*********#*******.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
mail function doesn't work on local server and also you need to set SMTP configuration in PHP to send mail.
Are you placing your form inside the form tag?
<form action="mail.php" method="post">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email address" class="contact-form-email required">
<input type="text" name="subject" placeholder="Subject" class="contact-form-subject">
<textarea name="message" placeholder="Message" class="required" rows="7"></textarea>
<div class="response-message"></div>
<button class="border-button" type="reset" id="reset" name="reset">Limpiar</button>
<button class="border-button" type="submit" id="submit" name="submit">Enviar</button>
</form>

Php Contact Form issue

I'm having problems with this code. After submitting the filled form the display message (Email sent!) is not displaying next to the submit button. I need to display the Email sent message next to the submit button on the contact form. After submitting the form, I also need to clear the form fields even after a refresh or a back action. Kindly help me out with this code
Form Action Images:
Before Submitting the Form
After Submitting the form
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" value="Submit">
</form>
<?php
}
else /* send the submitted data */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Email sent!";
}
}
?>
Your code should be like this. Notice isset($_POST['submit'], $msg , value="".
<?php
if(isset($_POST['submit']))
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
$msg = "All fields are required";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$msg = "Email Sent";
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" value="">
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required value="">
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" name="submit" value="Submit">
<?php echo $msg; ?>
</form>
You can redirect the page after sent email or also can set a variable like $msg='email sent'; and call this variable before your submit button.
$action=$_REQUEST['action'];
if ($action!="") /* display the contact form */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name#email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
header('location: yoururl?msg=error');
//$msg = "All fields are required, please fill the form again.";
}
else{
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('location: yoururl?msg=succss');
//$msg = 'Email Sent!';
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<?php if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='success'){echo "Email sent!";}if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='error'){echo "All fields are required, please fill the form again.";} ?>
<?php echo $msg; ?>
<input type="submit" value="Submit">
</form>

Keep my form when echoing

For some reason whenever this script get's execited to the point where it is suppose to echo out "IT WORKED!", the message displays but the form that is suppose to be above it disapears.
My Code:
<?php
$message = $_REQUEST['message'];
$email = $_REQUEST['email'];
$times = $_REQUEST['times'];
$subject = $_REQUEST['subject'];
$to = $_REQUEST['to'];
for ($i=1; $i<=$times; $i++) {
mail( "$to", "$subject", $message, "From:" . rand() . "#$email" ) ;
}
?>
<form method="POST" id="email">
<h1>Email Bomber</h1>
<fieldset id="inputs">
<input name="times" type="text" placeholder="How Many Emails" autofocus required>
<input name="email" type="text" placeholder="Email Suffix" autofocus required>
<input name="to" type="email" placeholder="Who Do You Want To Email" autofocus required>
<input name="subject" type="text" placeholder="Email Subject" autofocus required>
<textarea name="message" placeholder="The Email Message" rows="15" cols="40"></textarea>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" name="Send" value="Send Email">
</fieldset>
</form>
<?php
if (isset($_REQUEST['message'])) {
echo "IT WORKED!";
}
?>
Should be :
PHP code :
<?php
if (isset($_REQUEST['Send'])) {
$message = $_REQUEST['message'];
$email = $_REQUEST['email'];
$times = $_REQUEST['times'];
$subject = $_REQUEST['subject'];
$to = $_REQUEST['to'];
for ($i=1; $i<=$times; $i++) {
mail( "$to", "$subject", $message, "From:" . rand() . "#$email" ) ;
}
echo "IT WORKED!";
}
?>
HTML :
<form method="POST" id="email" action="#">
<h1>Email Bomber</h1>
<fieldset id="inputs">
<input name="times" type="text" placeholder="How Many Emails" autofocus required>
<input name="email" type="text" placeholder="Email Suffix" autofocus required>
<input name="to" type="email" placeholder="Who Do You Want To Email" autofocus required>
<input name="subject" type="text" placeholder="Email Subject" autofocus required>
<textarea name="message" placeholder="The Email Message" rows="15" cols="40"></textarea>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" name="Send" value="Send Email">
</fieldset>
</form>

Basic contact form php not working, getting empty messages from my form [duplicate]

I’m new to PHP so bear with me. My form is sending empty emails when I press submit. Any help would be great.
This is my form:
<form class="contact_form" action="kontakt.php" method="post">
<p><input type="text" required="required" id="name" name="name" class="text_input" value="" size="22" />
<label for="name">Namn *</label></p>
<p><input type="text" required="required" id="company" name="company" class="text_input" value="" size="22" />
<label for="company">Företag *</label></p>
<p><input type="email" required="required" id="email" name="email" class="text_input" value="" size="22" />
<label for="email">Epost *</label></p>
<p><textarea required="required" name="content" class="textarea" cols="30" rows="5"></textarea></p>
<p><button type="submit" class="button white"><span>Skicka</span></button></p>
<input type="hidden" value="info#web.se" name="contact_to"/>
</form>
And my PHP code so far is:
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$content = $_POST['content'];
$mail_to = 'info#web.se';
$subject = 'Lilla form'.$name;
$body_message = 'From: '.$name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$content;
$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
?>
Please help me, I’m really stuck.
Thank you all!
Try this
<form class="contact_form" action="kontakt.php" method="post">
<p><input type="text" required="required" id="name" name="name" class="text_input" size="22" />
<label for="name">Namn *</label></p>
<p><input type="text" required="required" id="company" name="company" class="text_input" size="22" />
<label for="company">Företag *</label></p>
<p><input type="email" required="required" id="email" name="email" class="text_input" size="22" />
<label for="email">Epost *</label></p>
<p><textarea required="required" name="content" class="textarea" cols="30" rows="5"></textarea></p>
<p><button type="submit" class="button white"><span>Skicka</span></button></p>
<input type="hidden" value="info#web.se" name="contact_to"/>
</form>
Edit:(Debug)
$name = $_POST['name'];
$company = $_POST['company'];
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$content = $_POST['content'];
echo $name.$comapny.$email.$content;exit; // check whether the values are posted successfully or not

Mail Contact Form

What is wrong with this code?
Here is the first page of the code:
<strong>Contact Form</strong><form name="form1" method="post" action="sendcontact.php">Subject:<input name="subject" type="text" id="subject" size="50">Detail:<textarea name="detail" cols="50" rows="4" id="detail"> </textarea>Name:<input name="name" type="text" id="name" size="50">Email:<input name="email" type="text" id="email" size="50"><input type="submit" name="Submit" value="Submit"><input type="reset" name="Submit2" value="Reset">
Here is the sendcontact.php page:
<?php $subject =$subject;$message =$detail;$mail_form =$email;$header ="from: $name<$mail_from>";mail("email#email.com",$subject,$message,$header);?>
To retrieve variables submitted from a POST form you must get their values from the $_POST array.
This should accomplish what you are after:
$subject = $_POST['subject'];
$message = $_POST['detail'];
$mail_from = $_POST['email'];
$header ="from: ".$_POST['name']."<$mail_from>";
mail("email#email.com",$subject,$message,$header);

Categories