I have a basic HTML/PHP web form. Everything works fine when I fill out all fields, except I want to make the file upload optional, not required. I have taken "required" off of the form html for the upload. I checked my handler.php and it does not say that field is required. I checked the formhandler.php and it says that attachments are null. I have looked through similar questions, but any of the more complex solutions I am probably implementing incorrectly. I've expended my knowledge of php looking for a solution. What am I missing?
Here is the html of the form:
<div class="row pb6">
<div class="col-md-6 offset-md-3">
<form role="form" method="post" id="reused_form" enctype="multipart/form-data" >
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" id="name" name="name" required maxlength="50">
</div>
<div class="form-group">
<label for="email"> Email:</label>
<input type="email" class="form-control" id="email" name="email" required maxlength="50">
</div>
<div class="form-group">
<label for="tel"> Phone: </label>
<input type="tel" class="form-control" id="tel" name="tel" maxlength="50">
</div>
<div class="form-group">
<label for="name"> Message:</label>
<textarea class="form-control" type="textarea" name="message" id="message" placeholder="We want to hear all about your performance! Also, please include the date you need the music." maxlength="6000" rows="7"></textarea>
</div>
<div class="form-group">
<label for="file">File Upload (optional)</label>
<input type="file" class="form-control" id="image" name="image">
</div>
<button type="submit" class="btn btn-lg btn-success pull-right" id="btnContactUs">Post It! →</button>
</form>
<div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Sent your message successfully!</h3> </div>
<div id="error_message" style="width:100%; height:100%; display:none; "><h3>Error</h3>We are very sorry. There was an error sending your form. Email us and we will send you a free demo.</div>
</div>
</div>
</div>
And here is the handler.php:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email','tel'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
if($_FILES['image']){ $pp->attachFiles(['image']); }
$pp->sendEmailTo('email'); //
echo $pp->process($_POST);
after code edit:
1)Chrome and Firefox will send form, Safari will not.
2)All browsers show a perpetual "Sending" message, and do not show a success or failure message for the form.
3) Forms send WITH attachments will not send the form with attachment to email, only the form alone.
You are trying to attach the image whether it exists or not with $pp->attachFiles(['image']);. You should first check if it does indeed exists and only attach it if it does like so:
if($_FILES['image']){
$pp->attachFiles(['image']);
}
Related
I have a problem, I am trying to make some forms in bootstrap however it messed up.
Once I converted the forms to bootstrap related they are no longer doing the job they're supposed to do.
What I am trying to do is to save form logs to a txt file, but they won't save.
When I add name="test" it won't work, instead it writes in the url.
URL: localhost/save.php?John
save.php
<?php
$myfile = fopen("test.txt", "a+");
$txt = "Name : ".$_POST['test451']." -> Surname: ".$_POST['loki'];
fwrite($myfile, $txt);
fclose($myfile);
?>
index
<form action="/save.php" class="needs-validation" novalidate>
<div class="row">
<div class="col-md-6 mb-3">
<label for="firstName">First name</label>
<input type="text" class="form-control" id="firstName" name="loki" placeholder="" value="" required>
<div class="invalid-feedback">
Valid first name is required.
</div>
</div>
<div class="col-md-6 mb-3">
<label for="lastName">Last name</label>
<input type="text" class="form-control" id="lastName" name="test451" placeholder="" value="" required>
<div class="invalid-feedback">
Valid last name is required.
</div>
</div>
</div>
</form>
Seeing the comment you left with this code: (edit: you added that in the question just now in an edit)
<form action="/save.php" class="needs-validation" novalidate>
Forms default to a GET method if there is no POST implied. So you're getting the ?John back because of it.
Add method="post" in your form.
I have a website AMP project and I wanted to use PHP for forms.
On the webpage form which has a post method returns no action in the backend code.
So I think that the data inn´t transfered.
edit on answer:
My form code is:
<form method="POST" class="sit-form" action-xhr="ampsend.php" data-form-title="contactForm">
<div class="sit-row">
<div submit-success="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">success!
</template>
</div>
<div submit-error="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">failed! {{error}}</template>
</div>
</div>
<div class="dragArea sit-row">
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="firstnameHidden" value="Firstname"
id="firstnameHidden"
data-form-field="">
<input type="text" name="firstname" placeholder="Firstname" data-form-field="Name"
required="required" class="field-input display-4" value=""
id="firstname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="lastnameHidden" value="Lastname"
id="lastnameHidden"
data-form-field="">
<input type="text" name="lastname" placeholder="Lastname" data-form-field="Name"
class="field-input display-4" required="required" value=""
id="lastname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="emailHidden" value="Email" id="emailHidden"
data-form-field="">
<input type="email" name="email" placeholder="Email" data-form-field="Email"
required="required" class="field-input display-4" value=""
id="email">
</div>
<div class="sit-col-md-12 sit-col-sm-12 field">
<input type="hidden" name="messageHidden" value="Message"
id="messageHidden"
data-form-field="">
<textarea name="message" placeholder="Message" data-form-field="Message"
class="field-input display-4" required="required" value=""
id="message"></textarea>
</div>
<div data-for=""
class="sit-col-md-12 sit-section-btn sit-pt-4 align-center sit-col-sm-12 field">
<button type="submit" class="btn btn-primary display-4">
send
</button>
</div>
</div>
</form>
The file_put_contents of the POST by clicking on submit is:
a:8:{
s:15:"firstnameHidden";
s:9:"Firstname";
s:9:"firstname";
s:8:"TestName";
s:14:"lastnameHidden";
s:8:"Lastname";
s:8:"lastname";
s:12:"TestLastname";
s:11:"emailHidden";
s:5:"Email";
s:5:"email";
s:13:"test#test.com";
s:13:"messageHidden";
s:7:"Message";
s:7:"message";
s:11:"testMessage";}
So does it work correctly? because as return I get the "failed!" - message from the AMP template.
I think you cannot execute PHP code on the displaying document. PHP is serving the data to show through an POST on the displaying web document as described in this post.
My solution to check if PHP is running ,and check what data was posted via form - was write everything to text file.
$serializedData = serialize($_POST);
file_put_contents('your_file_name.txt', $serializedData);
Open file and see what data was pass. If you want display message after form was submitted and pass some date like name or email. You need to use amp-mustache. And and return JSON form PHP to pass it back to your AMP page.
For example in your PHP file:
header('Content-Type: application/json');
echo json_encode(array( 'name' => $_POST['name]' ));
Of course don't forget to put appropriate CORS headers in PHP or you .htaccess file.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to send emails without using any database that's why I was trying to send an email using PHP and HTML, but the problem is that after pressing the submit button it does not sending any emails. Here are my codes:
<form role="form" name="contactform" id="contactform" method="POST" action="send.php" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="input-text form-group">
<input type="text" name="contact_name" class="input-name form-control" placeholder="Full Name" />
</div>
<div class="input-text form-group">
<input type="email" name="email" class="input-name form-control" placeholder="Enter Email-ID" />
</div>
<div class="input-text form-group">
<input type="text" name="contact_phone" class="input-name form-control" placeholder="Phone Number" />
</div>
</div>
<div class="col-md-6">
<div class="input-text form-group">
<input type="text" name="contact_sub" class="input-name form-control" placeholder="Subject" />
</div>
<div class="textarea-message form-group">
<textarea name="contact_message" class="textarea-message hight-82 form-control" placeholder="Message" rows="2"></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-default top-margin-10 hblack" name="Submit" value="Submit" />
</div>
</div>
</form>
and the PHP code are:
<?php
$strTo = $_POST["contact_name"];
$strSubject = $_POST["email"];
$strMessage = nl2br($_POST["contact_phone"]);
$strSub = nl2br($_POST["contact_sub"]);
$strmsg = nl2br($_POST["contact_message"]);
$body="Job-title:$title \r\n
Name:$strTo \r\n
Email:$strSubject \r\n
Phone:$strMessage \r\n
Subject:$strSub \r\n
Message:$strmsg";
$from=$strSubject;
$to='abc#abc.com'; // removed for spam protection
mail($to,$from,$body);
echo "<script> alert('Your Form has been Submitted successfully'); window.location.href='index.php'; </script>";
?>
Thanks in advance.
If you use the mail function then you will have to make some server side changes to your php settings. Most commonly these changes are found in a file called "php.ini". I encourage looking up changes needed for the mail function to work on your specific server and including details about your server and php version when searching
I am unable to configure my email address on the submit button in this piece of code:
<div id="contact" class="spacer">
<div class="container contactform center">
<h2 class="text-center wowload fadeInUp">Get in touch with us</h2>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<form class="cmxform" id="commentForm" method="post" action="email.php">
<fieldset>
<input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
<input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
<textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
<input class="submit btn btn-primary" type="submit" value="Submit">
</fieldset>
</form>
</div>
</div>
I tried linking it to a php page (email.php), but it says server error. I don't know what to do. Can someone please help me?
To send an email you need the mail() function http://php.net/manual/en/function.mail.php
With your code you need to name the submit like name="submit" then inside action.php file you have to write something like this (or inside the same php where you have this code):
if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";
//then here you set your email
$to = "myemail#email.com"; //change this
mail($to,$subject,$body);
}
This is only to explain some basic usage and set the variables you need, I advice you read also PHPmailer class
I have an html form where users will submit their contact information for us to get in contact with them. I would like to have all of this live on index.html, but I am not opposed to having to create another file in the folder.
<form class="row" method="post" action="contact.php">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Full Name">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Phone Number*">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Email Address*">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Company Name*">
</div>
<div class="form-group">
<textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>
</div>
</div>
<div class="buttn_outer">
<button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left;">Submit</button>
</div>
</form>
I have had php installed on the server machine. Looking at other examples I have found attempted using a php script as follows.
<?php
if($_POST["submit"]) {
$recipient = "me#me.com";
$subject = "Some Message subject";
$sender = $_POST["sender"];
$senderEmail = $_POST["senderEmail"];
$message = $_POST["message"];
$mailBody = "Name: ".$sender."\nEmail: ".$senderEmail."\n\n".$message;
mail($recipient, $subject, $mailBody, "From: ".$sender." <".$senderemail.">");
$thankYou = "<p>Thank you! Your message has been sent.</p>";
}
?>
As of right now it goes to the success message however I never get anything to go to my email.
I am extremely new to php, any help/advice is appreciated.
None of your input fields have name attributes.
<input type="text" class="form-control" placeholder="Email Address*">
<input type="text" class="form-control" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>
Should be
<input type="text" class="form-control" name="senderEmail" placeholder="Email Address*">
<input type="text" class="form-control" name="sender" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" name="message" placeholder="Questions / comments"></textarea>
Basically, the name attribute of the input field should match what the $_POST key is.
So, <input name="name" /> would be handled as $_POST['name']
Also, it looks like you're mixing up the $recipient and $senderEmail variables. PHP's mail function specifies that the first parameter is the recipient. I understand that me#me.com is clearly not a real email. But, the recipient should be the user's email that he/she has submitted in the form. Make sure the mail function works by testing it without submitting the form and if it does, then try to make it work with the form variables.
Try This May Help You...
index.html
<form class="row" method="post" action="contact.php">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control" name="sender" placeholder="Your Full Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="phno" placeholder="Phone Number*">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" placeholder="Email Address*">
</div>
<div class="form-group">
<input type="text" class="form-control" name="cmp" placeholder="Company Name*">
</div>
<div class="form-group">
<textarea class="form-control lg_textarea" name="cmt" placeholder="Questions / comments"></textarea>
</div>
</div>
<div class="buttn_outer">
<button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left";>Submit</button>
</div>
</form>
contact.php
<?php
$to='me#me.com';
$header = "From: info#".$_SERVER["SERVER_NAME"]."\n";
$header .= "Content-Type: text/html; charset=iso-8859-1\n";
$sender=$_POST['sender'];
$email=$_POST['email'];
$cmt=$_POST['cmt'];
$cmp=$_POST['cmp'];
$phno=$_POST['phno'];
$subject='Message Subject';
$body='<table width="90%" border="0">
<tr>
<td><b>Sender:</b></td> <td>'.$sender.'</td>
</tr>
<tr>
<td><b>Email:</b></td> <td>'.$email.'</td>
</tr>
<tr>
<td><b>Phone No:</b></td> <td>'.$phno.'</td>
</tr>
<tr>
<td><b>Copmany Name:</b></td> <td>'.$cmp.'</td>
</tr>
<tr>
<td><b>Comment:</b></td> <td>'.$cmt.'</td>
</tr>
<tr></table>';
mail($to,$subject,$body,$header);
header('location:index.html');
?>
Other things, I'd throw in
($_POST["submit"])
should be changed to
!empty($_POST["submit"]) - or - isset($_POST["submit"])
Never assume a variable is set - don't run any tests on it or use it's value until you check to see if it's been set (or you initialize it yourself).
Depending on your PHP config, that line can fail. So if you move your contact.php page to another server, and then it may crash with errors.
As Lance mentioned, every form field that you want to pass back to your server needs a name attribute. This is what links it to the $_POST array when it gets passed to the server.
Also, you can use the users email as the from address. However, your email will likely get flagged as spam when the receiving server sees that it's not originated from that user's domain email server. You'll have to play around with your server's email settings so you authenticate it as a legitimate from address if you're trying to "spoof" it (aka not use the server's default).
If you want to override the default from address (or reply to address), you need to do it in the header fields (you can google this one to see you options).
If you want to include HTML tags or elements in your HTML, you'll also need to indicate this in the headers parameter of the mail function. For simply alert emails, like what you're doing - I just use plain text and use carriage returns (like you have with \n's) and don't use items like <p></p>.