Extracting HTML Form TextArea in PHP - php

Need help in understanding why the textarea ("message" below) field in the HTML5 form is not being grabbed from the PHP code (used to create email).
HTML Code (French):
<form method="post" action="contact.php">
<fieldset>
<p class="tight" style="font-size: 10pt; text-align: center;" >Message pour des demandes de renseignements d&apos;ordre <br /> général seulement. Veuillez utiliser demande formulaire situé <br /> sur la page Service pour commencer.</p>
<label><span>Nom</span>
<input class="inputcontact" name="name" type="text" /></label>
<label><span>Courrier <br />Électronique</span>
<input class="inputcontact" name="email" type="text" /></label>
<label><span>Téléphone</span>
<input class="inputcontact" name="telephone" type="text" /></label><br />
<label class="labelleft" for="message"> MESSAGE
<textarea name="message" rows="8" cols="45"> </textarea></label>
</fieldset>
<input class="submit" type="submit" value="ENVOYER" />
</form>
PHP Code (contact.php):
<?php
/* Set e-mail recipient */
$myemail = "xxxxxx#gmail.com";
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['name']);
$email = check_input($_POST['email']);
$telephone = check_input($_POST['telephone']);
$messaage = check_input($_POST["message"]);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$subject ="Comment received from website";
$content = "Hello!
The contact form from the website has been submitted by:
Name: $yourname
E-mail: $email
Telephone: $telephone
Message:$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $content);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>

$messaage = check_input($_POST["message"]);
Message:$message.
is it ok or misspelling?

You have a typo in your variable name $message on line 9 (you wrote $messaage).

I recommend you to use filter_var($email, FILTER_VALIDATE_EMAIL); instead of your Regex. I think this would be more specific

Related

Email not sending anymore form.php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 months ago.
sinds a couple of days my form has stopped working.
I'm not really familiar with PHP and i have no idea how to get it working again.
I'm hosting my site on one.com , it has been working fine for about a year now, and suddenly it stopped working?
I've been searching the internet and i'm finding all kinds of answers 'you have to update your php code...' but i'm not familiar with PHP so please help :D
The form.php :
<?php
/* Set e-mail recipient */
$myemail = "info#kongweb.be";
/* Check all form inputs using check_input function */
$naam = check_input($_POST['naam']);
$mail = check_input($_POST['mail']);
$bericht = check_input($_POST['bericht']);
/* Let's prepare the message for the e-mail */
$message = "Hallo!
Wij ontvingen een contactformulier met de volgende gegevens:
Naam: $naam
E-mailadres: $mail
Message: $bericht
Einde van dit bericht.
";
/* Send the message using mail() function */
$headers = "From: info#kongweb.be";
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:bedankt.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
And the code for the contact form on the index.html :
<!-- contact begin -->
<div id="contact" class="container-fluid text-center tekst2">
<h1>Contact</h1>
<form name="contact" method="post" action="https://kongweb.be/form.php" onsubmit="return validateForm()" >
<input type="hidden" name="subject" value="contact">
<div class="row slideanim spatieform">
<div class="col-sm-12">
<input type="text" name="naam" size="30" id="naam" required placeholder="Name">
</div>
</div>
<div class="row slideanim spatieform">
<div class="col-sm-12">
<input type="text" name="mail" size="30" id="mail" required placeholder="Email">
</div>
</div>
<div class="row slideanim spatieform">
<div class="col-sm-12">
<textarea name="bericht" id="bericht" cols="30" rows="8" required placeholder="Message"></textarea>
</div>
</div>
<div class="row slideanim spatieform">
<div class="col-sm-12">
<input type="submit" name="submit" value="Send it" style="background-color: #F3E600">
</div>
</div>
</form>
</div>
<!-- contact einde -->
This will escape your input a bit and give you some error messages for debug. Unless you are sending the email to yourself you need a $to variable and you were missing a $subject , which is also required or will not work. This is a very simple way to do this. There are lots of other like looping through the $_POST for instance. I also suggest reading https://www.php.net/manual/en/function.mail.php
Example Fix to your question:
// you can make sure its set and escape it also. Must be cleaned when user input.
if(isset('submit')){
$naam = mysqli_real_escape_string($con, $_POST['naam']);
if(!isset($coops) || empty($coops)){
$json_array['error'] = true;
$json_array['message'] = "Coops not set.";
$json = json_encode($json_array);
echo($json);
exit;
}
$mail = mysqli_real_escape_string($con, $_POST['mail']);
if(!isset($mail) || empty($mail)){
$json_array['error'] = true;
$json_array['message'] = "mail not set.";
$json = json_encode($json_array);
echo($json);
exit;
}
$bericht = mysqli_real_escape_string($con, $_POST['bericht']);
if(!isset($bericht) || empty($bericht)){
$json_array['error'] = true;
$json_array['message'] = "bericht not set.";
$json = json_encode($json_array);
echo($json);
exit;
}
$headers = "From: info#kongweb.be";
$myemail = "info#kongweb.be";
$subject = "Your subject"; // was missing and must have
$message = "Hallo!
Wij ontvingen een contactformulier met de volgende gegevens:
Naam: $naam
E-mailadres: $mail
Message: $bericht
Einde van dit bericht.";
mail($myemail, $subject, $message);
}
header('Location:bedankt.html');
exit();
are you sending the email to yourself. Otherwise you need a $to in place of you $myemail but without $subject being intialized it would never work. Redirect visitor to the thank you page
to Mail address to which you want to send mail Required
subject Subject of the mail Required
message Message to be sent with the mail. Each line of the message should be separated with a LF (\n). Lines should not be larger than 70 characters. Required

How to make a submit form talk to my php file to send an email

I am trying to get a website form to send to my email. The host has provided the Path to SendMail as:
/usr/sbin/sendmail but I am not sure where to put it. Below is the form HTML and the PHP that I currently have.
This is the form HTML that I have
<form class="email" action="pages/mailer.php" method="post">
<div class="brown">
<h2>Request A FREE In-House Estimate</h2></div>
<div="boxy">
<div class="row">
<p class="col-sm-6 col-sm-offset-4">
<label><span>Name</span>
<input type="text" class="input_text" name="name" id="name"/> </label>
<label>
<span>Email</span>
<input type="text" class="input_text" name="email" id="email"/> </label>
<label>
<span>Number</span>
<input type="text" class="input_text" name="subject" id="subject"/> </label>
<label>
<span>Message</span>
<textarea class="message" name="message" id="message"></textarea>
<br/>
<input class="button" type="submit" value="Send" />
</label>
</div>
</div>
</form>
This is the PHP 'mailer.php' that lives in my pages folder.
<?php
/* Set e-mail recipient */
$myemail = "personalemail#live.com";
/* Check all form inputs using check_input function */
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: ../index.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Currently, when I hit submit the url changes to pages/mailer.php and I get an error
'Please correct the following error:
Invalid e-mail address
Hit the back button and try again'.
Any help is greatly appreciated!Thanks!

Change a field in PHP form from required to optional

I am a complete novice when it comes to PHP. I downloaded a form I found online to include in my Bootstrap site. It is a very simple form anyone can use to send me a message. I managed to set up Wamp to test out the PHP but when I leave the Phone field blank it gives me an error message telling me please go back and correct the error. I want to make it so if someone leaves out the phone number it still sends the email. Thank you.
HTML
<form name="contactform" method="post" action="index.php" class="form-vertical">
<div class="form-group">
<label for="inputName" class="control-label">Name</label>
<input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last">
</div>
<div class="form-group">
<label for="inputEmail" class="control-label">Email*</label>
<input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required">
</div>
<div class="form-group">
<label for="inputPhone" class="control-label">Phone Number</label>
<input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional">
</div>
<div class="form-group">
<label for="inputMessage" class="control-label">Message</label>
<textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-custom pull-right hvr-underline-from-left">Send</button>
</div>
</form>
PHP
<?php
/* Set e-mail recipient */
$myemail = "myaccount#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = check_input($_POST['inputPhone'], "Optional");
$message = check_input($_POST['inputMessage'], "Brief Description");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Contact Message from mywebsite.net";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Phone: $phone
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
Please change this line:
$phone = check_input($_POST['inputPhone'], "Optional");
to:
$phone = check_input($_POST['inputPhone']);
This way show_error($problem); won't be called.
Because function check_input include function show_error.
And function show_error have exit() when have errors.
So, your phone input == NULL => so have error and call to exit().
Solution for this case
Don't check require with phone number input.
PHP
<?php
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
/* Set e-mail recipient */
$myemail = "myaccount#gmail.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "First and Last");
$email = check_input($_POST['inputEmail'], "Required");
$phone = $_POST['inputPhone'];
$message = check_input($_POST['inputMessage'], "Brief Description");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Contact Message from mywebsite.net";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Phone: $phone
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:contact.html');
exit();
?>
You are facing this error because you are validating the phone number i.e $_POST['inputPhone'] using the check_input function in the line
$phone = check_input($_POST['inputPhone'], "Optional");
You can avoid this error in multiple ways:
$phone = $_POST['inputPhone']; //not secure
OR
$phone = check_input($_POST['inputPhone'], "");
OR
$phone = filter_var($_POST['inputPhone'],FILTER_SANITIZE_STRIPPED);

How to send form to self email?

This is my contanct.html
<form action="sendmail.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>
<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>
<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>
<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send it!"></p>
<p> </p>
<p>Powered by PHP form</p>
</form>
This is my php
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: confirmation.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
I got a problem when i upload in domain
It's working but didn't not send to my email of Form
I don't know what is my problem?
I'm newbie in web.dev
sorry for my bad english thanks.
I found and figured out what the problem is.
The function show_error($myError) was unfinished.
Plus you also needed to add $myemail = "email#example.com"; replacing it with your E-mail address.
Also make sure this page exists on your server header('Location: confirmation.htm');
Tested, working.
<?php
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$website = check_input($_POST['website']);
$likeit = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");
$myemail = "email#example.com"; // Replace with your E-mail address.
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $yourname
E-mail: $email
URL: $website
Like the website? $likeit
How did he/she find it? $how_find
Comments:
$comments
End of message
";
/* Send the message using mail() function */
$headers="From: $name<$email>\r\nReturn-path: $email\r\n";
mail($myemail, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: confirmation.htm');
// echo "ok"; // For testing purposes only
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<b>We apologize for the inconvenience, an error occurred.</b><br />
<?php echo $myError; ?>
<?php
exit();
}

Contact Form with recaptcha producing 500 error

I have a contact form with recaptcha that i am trying to set up but produces a 500 error when information is entered and the submit button is clicked. I'm going to bet money that I have done something stupid as I'm not the greatest with PHP, so if I post my code here, would anyone be able to spot anything wrong?
This is the Contact Form itself.
<form class="email" action="mailer.php" method="post">
<p>Name:</p>
<input type="text" name="name" />
<p>E-mail:</p>
<input type="text" name="email" />
<p>Subject:</p>
<input type="text" name="subject" />
<p>Message:</p>
<textarea name="message"></textarea></p>
<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6Le8_t4SAAAAACIxacT6Xn8NVvDa93loylG-L6mk"></script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=PUBLICKEYGOESHERE" height="300" width="500" frameborder="0"></iframe>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript>
<input class="send" type="submit" value="Send">
</form>
And this is the mailer.php
<?php
require_once('recaptchalib.php');
if ($_POST['email'] != '')
{
$privatekey = "6Le8_t4SAAAAAME8kuqO1bvzcSWmGytwISUYLo3w";
$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
die("The reCAPTCHA wasn't entered correctly.
Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")");
}
else
{
/* Set e-mail recipient */
$myemail = "mandy#smarterbookkeeping.com.au";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
$captcha = check_input($_POST['captcha']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.php');
exit();
/* Functions we used */
function check_input($data, $problem = '')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php
echo $myError;
?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
}
}
?>
I have the recaptchalib in the right place, same goes for the thanks.php. I'm not very good at PHP, but this one has me stumped.

Categories