PHP power form not sending all fields to email - php

Im using this php script to send the contents of my form to my email when the user submits the form, but currently im only able to get the last two fields of my form to send in the email.( message and company name are the only two working) How can I get the entire form to send in the email?
<form role="form" method='post' action='backend/php_mailer.php'>
<input type="text" name='name' class="form-control" id="yourname" placeholder="Name">
<input type="email" name='email' class="form-control" id="email" placeholder="Email">
<input type="text" name='phone' class="form-control" id="phone" placeholder="Phone">
<input type="text" name='company' class="form-control" id="company" placeholder="Company Name">
<textarea name='message' class="form-control" id="message" rows="6" placeholder="Message"></textarea>
<button type="submit" class="btn btn-primary btn-lg ">SUBMIT</button>
</form>
<?php
if (isset($_POST['email']))
{
$userName = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$company = $_REQUEST['company'] ;
$message = $_REQUEST['message'] ;
mail("peaceuponelove#gmail.com", $subject,
$message,$company,$userName );
echo "Thank you";
}
?>

Each argument of mail() has a specific purpose. You cannot just continually pass in arguments and expect them to be appended to the email. You must use string concatenation.
$message = $_REQUEST['message'] . '</br>' . $email . '<br/>' . $phone . '<br/> ' . $userName . '<br/> . ' $company;
mail("peaceuponelove#gmail.com", $subject,$message);
Sidenote You never declared $subject in the code that you've shown. So here's a subject:
$subject = 'Message from '. $userName .' < ' . $email . ' > ';

Here is what i ended up doing to get all fields in email
<?php
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<input name="name" type="text" value="" size="30"/><br>
<input name="email" type="text" value="" size="30"/><br>
<input name="phone" type="text" value="" size="30"/><br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$phone=$_REQUEST['phone'];
$message=$_REQUEST['message'].$email.$phone;
$subject="$name";
mail("peaceuponelove#gmail.com", $subject, $message);
echo "Email sent!";
?>

Related

How to prevent form submission if message contain links?

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");

PHP form redirecting to PHP file itself

I'm trying to get a contact form working, and I'm being redirected to the PHP file itself, and I'm not sure why. I'm fairly new to PHP and I'm not entirely sure what I'm doing wrong. I'd love to be pointed in the right direction. The code is below.
Thanks
HTML:
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
PHP:
<?php
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
?>
This line tells the form what PHP file to send the data to:
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
In particular, it is this part that sends the data to a particular form:
action="form.php"
In fact, this is fine. You can process a form in the same PHP file that contains the form. Just put the PHP form processing at the top:
<?php
if (isset($_POST['Email_Address'] && $_POST['Email_Address'] != ''){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
EMAIL;
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>
Just remove enctype="text/plain" otherwise your code is correct in HTML and try to echo feedback variable other way is to write all your code above the HTML like below:
<?php
if (isset($_POST['Email_Address'] && !empty['Email_Address']){
$to = 'zack#zfisch.com';
$subject ='Dropset Work Request';
$name = $_POST['Name'];
$company = $_POST['Company'];
$email = $_POST['Email_Address'];
$message = $_POST['Description'];
$message = <<<EMAIL
From: $name
$message
Email: $email
$header = $subject;
if($_POST) {
mail($to, $subject, $message, $header);
$feedback = 'Email sent!';
}
}else{
>?
<p id='feedback'><?php echo $feedback; ?></p>
<form id="contact_us" enctype="text/plain" method="post" action="form.php">
<input class="form_field" type="text" name="Name" placeholder="Full Name">
<br>
<input class="form_field" type="text" name="Company" placeholder="Company Name">
<br>
<input class="form_field" type="email" name="Email_Address" placeholder="Email Address">
<br>
<textarea class="form_field" rows="10" cols="20" name="Description" wrap="hard" placeholder="Project Description"></textarea>
<br>
<p id="required"><i>Please fill in all the fields*</i></p>
<input class="submit" type="submit" value="SUBMIT">
</form>
<?php
}
?>

PHP mail form - only one variable not submitting

I have set up a PHP mail form set up that only correctly outputs some of the variables entered in the form. It DOES mail the $name and $email variables, but not the $message variable.
The php to send the form is here:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" ){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
//sending email
require_once("siteIncludes/class.phpmailer.php");
$mail = new PHPMailer();
$email_body = "";
$email_body = $email_body . "Name: " . $name . $message . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom("$email,$name");
$address = "foo#bar.com";
$mail->AddAddress($address);
$mail->Subject = "Form Submission | ".$name;
$mail->MsgHTML($email_body);
if(!$mail->Send() ){
echo 'There was a problem sending the email: '.$mail->ErrorInfo;
exit();
}
header("Location: myContact.php?status=thanks");
exit();
};
?>
And the HTML that sets up the form is here:
<div id="contactFormWrap" class="span6 offset3">
<form method="post" action="myContact.php" id="contactForm">
<div>
<label for="name">Please leave your name</label>
<input type="text" name="name" id="name" value="" class="required" />
</div>
<div>
<label for="email">and your email</label>
<input type="text" name="email" id="email" value="" class="required email" />
</div>
<div>
<label for="subject">What is your message about?</label>
<input type="text" name="subject" id="subject" value="" class="required" />
</div> -->
<div>
<label for="message">and your message</label>
<textarea name="message" id="message" value="" rows="10" class="required"></textarea>
</div>
<div id="messageButtons">
<input type="submit" value="Submit" name="contactSubmit" id="contactSubmit" class="sendEmail btn" />
</div>
</form>
</div>
I hope that was enough information. Does anyone know why the $message variable isn't being output to the submitted email?
thanks
I think this is your problem:
value=""
The <textarea> tag does not have a value attribute, however different browsers have different ways of handling invalid code, so whatever browser you are using must be using the value found in this invalid attribute instead of what you type in the actual text box.
Just do:
<textarea name="message" id="message" rows="10" class="required"></textarea>

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>

Why is my form validation not working?

I'm having troubles validating my form. How do I validate the form using PHP? I've tried lots of different methods and nothing has worked. I can get the inputs to display (although check-box doesn't always display) but it just won't validate.
I also want to display the user's inputs (after it has been validated) onto another page, how do I do that?
Here is my code;
Form:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="" required>
<br><br>
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" value="" required>
<br>
<br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="" required>
<br>
<br>
Recipient:
<div>
<label for="admin">
<input type="checkbox" name="recipient[]" id="admin" value="Administrator">
Administrator</label>
<br>
<label for="editor">
<input type="checkbox" name="recipient[]" id="editor" value="Content Editor">
Content Editor</label>
<br>
</div>
<br>
<label for="message">Message:</label>
<br>
<textarea name="message" id="message" cols="45" rows="5" required></textarea>
<input type="hidden" name="submitted" value="1">
<br>
<input type="submit" name="button" id="button" value="Send">
<br>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if ($_POST['submitted']==1) {
if ($_POST['name']){
$name = $_POST['name'];
}
else{
echo "<p>Please enter a name.</p>" ;
}
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
$email = $_POST['email'];
}
else{
echo "<p>Please enter a valid email.</p>";
}
if ($_POST['subject']){
$subject = $_POST['subject'];
}
else{
echo "<p>Please enter a subject.</p>";
if(empty($_POST['recipient'])){
echo "<p>Please select a recipient</p>";
}else{
for ($i=0; $i < count($_POST['recipient']);$i++) {
echo $_POST['recipient'][$i] . " ";
}
}
}
if ($_POST['message']){
$message = $_POST['message'];
}
/* go to form.php
display results
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong> ";
echo "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
*/
?>
if (preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/i", $email)
You can't use email in this condition because $email is empty!

Categories