Can't use [] for reading HTTP500 error - php

I'm getting a http500 error with this code:
<?php
if(isset($_POST['submit'])){
$to = ["charlestdjames1#gmail.com"];
$subject = $_POST["name"];
$message = $_POST["content"];
$headers = $_POST["email"];
mail($to, $subject, $message, $headers);
}
?>
the logs state that I cannot use [] for reading, I'm not sure if this is a error with the code or not.
The form I'm using:
<form method="post">
<span class="close">x</span>
<label class="modalFont" for="name">Name:</label>
<input class="modalFont" type="text" id="name" name="name" placeholder="Please enter your name" /><br />
<label class="modalFont" for="email">Email Address:</label>
<input class="modalFont" type="text" id="email" name="email" placeholder="Please enter your email" /><br />
<label class="modalFont" for="message">Message:</label>
<input class="modalFont" type="text" id="message" name="content" placeholder="Enter your message here" /><br />
<button type="submit" id="submit">Submit</button>
</form>

Related

HTML form Post to email address with PHP

Hi there im building a website and looking to connect a contact form to send an email/ message to an email address and im not quite sure what im doing wrong.
I'm very new so apologies and thank you in advance..
here's the code i have below!
PHP
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = 'tomasyoung0#gmail.com';
$headers = 'From: '.$mailFrom;
$txt = 'You have received an e-mail from '.$name.'. \n\n'.$message;
mail($mailTo, $subject, $txt, $headers);
header('Location: index.html?mailsend')
?>
HTML
<form class="contact-form" action="contactform.php" method="POST">
<input class="form-input" name="name" type="text" placeholder="Name" >
<input class="form-input" name="email" type="email" placeholder="Email">
<input class="form-input" name="subject" type="text" placeholder="Subject" >
<textarea class="form-input" placeholder="Message" cols="30" rows="10"></textarea>
<button class="btn"type="submit" name="submit">
<div class="button">
<i class="fa fa-paper-plane"></i><span class="send-text"></span>
</div>
</button>
</form>
You don't have mail and message at your html code, change
<input class="form-input" name="email" type="email" placeholder="Email">
To
<input class="form-input" name="mail" type="email" placeholder="Email">
And
<textarea class="form-input" placeholder="Message" cols="30" rows="10"></textarea>
To
<textarea class="form-input" name = "message" placeholder="Message" cols="30" rows="10"></textarea>
Inside [] of post variable you must put name of your input or button inside [] using ''

PHP contact form not working right

I've been working on a contact form. I have added used the POST method to send it to a set email address. It doesn't seem to be working though. It just runs and stops as if the code is broken. The HTML and PHP are below.
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="name">Your name</label> <input class=
"form-control" id="name" name="name" type="text">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email"
type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class=
"form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name=
"message" rows="6">
</textarea>
</div>
<div class="submit">
<input class="button button-small" type="submit"
value="Send">
</div>
</form>
<?php
if(isset($_POST['submit'])) {
$to = "gfrazer#hotmail.co.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$message = $name . " " . " wrote the following: " . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header('Location: http://www.google.co.uk');
}
?>
You need to add a name to your submit button. From your snippit, you have no $_POST['submit']:
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email" type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class="form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name="message" rows="6"></textarea>
</div>
<div class="submit">
<!-- ADD name="submit" -->
<input name="submit" class="button button-small" type="submit" value="Send" />
</div>
</form>

Form not showing input data

I have a running form and it's half working. The form sends to my email address correctly but i'm having issues with the inputs not showing the text in the email. The only one that spits out any data is the message section.
The name, email, phone and website inputs are not spitting out any data and I can't figure out why?
// html5 code
<form action="forms/get_form.php" method="post">
<div>
<label for="name">Name</label>
<input type="text" id="name" placeholder="enter name" required="required">
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" placeholder="email address" required="required">
</div>
<div>
<label for="phone">Phone Number</label>
<input type="tel" id="phone" placeholder="enter phone number" required="required">
</div>
<div>
<label for="website">Enter website URL if you have one (optional)</label>
<input type="url" id="website" placeholder="website address">
</div>
<div>
<label for="message">Enter your message</label>
<textarea name="message" id="message" rows="10"></textarea>
</div>
<div>
<button type="submit" class="btn-blue">Send</button>
</div>
</form>
// php code
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email \n Phone: $phone \n Website: $website \n Message: $message";
$recipient = "email goes here";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader);
if(mail) {
header('Location: /thankyou.html');
} else {echo "Email failed to send click back and check email details!";}
?>
Thanks in advance
Vizzy
Not ID but NAME param,
<input type="text" id="name" placeholder="enter name" required="required">
Should be:
<input type="text" name="name" placeholder="enter name" required="required">
Instead of ID, use name.
<input type="text" name="name" placeholder="enter name" required="required">

send email button not working, either opens desired url or sends email, not both simultaneously

i'm stuck in between the two desired functions which I can't manage to occur simultaneously; I have an email submission form and I need it to redirect the user to a thank you page after hitting the "Submit" button. I'm providing you with 3 html alternatives and 1 php file.
When the html is like this, the email gets sent but the redirection doesn't work.
<form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
<div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
<label for="email">Email:</label>
<input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
<label for="message">Message:</label>
<textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
<input type="submit" value="Submit" class="button" />
</form>
When i change the last line to this, it sends the email again but redirection still doesn't work:
<form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
<div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
<label for="email">Email:</label>
<input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
<label for="message">Message:</label>
<textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
<input type="submit" value="Submit" class="button" />
</form>
And finally when I change the last line to this, the redirection works but no email gets sent.
<form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
<div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
<label for="email">Email:</label>
<input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
<label for="message">Message:</label>
<textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
<input type="button" value="Submit" class="button" />
</form>
This is the php file:
<?php
$to = "info#domain.com";
$subject = "Hi Nick, this is ".$_POST['name'];
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: '.$_POST['email'].'' . "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = $message;
mail($to, $subject, $body, $headers );
header('Location: http://www.domain.com/thank-you') ;
?>
Any help will be highly appreciated. Thanks!
In the third html block, the redirection works because it is a simple hyperlink because <input type=button> defines a clickable button (mostly used with a JavaScript to activate a script).
Therefore no form will be submitted but the hyperlink will work.
In the second html block, the hyperlink wont work because <input type=submit> will make it a submit button with a default behavior of submitting the form to the action file and so the hyperlink won't work.
I know this doesn't answer the question but just felt like telling you the reasons.
✓ EDIT (actual code used for successful submission)
HTML form
<form id="contact_form" action="mailer.php" method="post">
<div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
<label for="email">Email:</label>
<input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
<label for="message">Message:</label>
<textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
<input type="submit" value="Submit" class="button" />
</form>
Mail handler
<?php
$to = "email#example.com";
$subject = "Hi Nick, this is ".$_POST['name'];
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: '.$_POST['email'].'' . "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = $message;
mail($to, $subject, $body, $headers );
header('Location: http://www.example.com/thank-you');
// commented out for testing purposes.
// Email was sent and received successfully
// echo "Success";
?>
✓ Original answer (Aug. 14, 2013)
This => enctype="text/plain" should not be used inside <form> elements, and that is why you were having problems.
Try it with this reformatted form:
<form id="contact_form" action="mailer.php" method="post">
<div class="message" style="display:none"><div id="contact_alert" class="alert"></div></div>
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="full-width help" title="Insert your name" />
<label for="email">Email:</label>
<input type="email" name="email" id="email" class="full-width help" title="Insert your email" />
<label for="message">Message:</label>
<textarea name="message" id="message" cols="30" rows="7" class="full-width help" title="Insert your message"></textarea>
<input type="submit" value="Submit" class="button" />
</form>
I replaced this:
<form id="contact_form" action="mailer.php" method="post" enctype="text/plain">
/--------------------/
with this:
<form id="contact_form" action="mailer.php" method="post">
And this is invalid:
<input type="button" value="Submit" class="button" />
This is valid:
Click here
As is this is also valid:
<input type="button" value="Submit" class="button" />

What is needed in php file to send this html form from a template

I know very little about contact forms and php.
I am building a website from a template and it has this form.
Can you please tell me what code to put in contact.php to send over entered information.
And also what email will it send from?
And where do I determine what email address it sends to?
THANK YOU!
<form method="post" action="contact.php" id="contactform">
<div>
<p>Send us a message</p>
</div>
<div>
<label>Name <span class="required">*</span></label>
<input name="name" type="text" id="name" value="" />
</div>
<div>
<label>Email <span class="required">*</span></label>
<input name="email" type="text" id="email" value="" />
</div>
<div>
<label>Phone Number <span class="required">*</span></label>
<input name="Phone Number" type="text" id="Phone Number" value="" />
</div>
<div>
<label>Subject</label>
<input name="subject" type="text" id="subject" value="" />
</div>
<div>
<label>Message <span class="required">*</span></label>
<textarea name="message" rows="20" cols="50" id="message" ></textarea><br /><br />
</div>
<div>
<input type="submit" value="Submit" class="button">
<input type="reset" value="Reset" class="button">
</div>
</form>
Here you go..
<form method="get" action="contact.php" id="contactform" name="contactform">
<div>
<p>Send us a message</p>
</div>
<div>
<label>Name <span class="required">*</span></label>
<input name="name" type="text" id="name" value="" />
</div>
<div>
<label>Email <span class="required">*</span></label>
<input name="email" type="text" id="email" value="" />
</div>
<div>
<label>Phone Number <span class="required">*</span></label>
<input name="PhoneNumber" type="text" id="Phone Number" value="" />
</div>
<div>
<label>Subject</label>
<input name="subject" type="text" id="subject" value="" />
</div>
<div>
<label>Message <span class="required">*</span></label>
<textarea name="message" rows="20" cols="50" id="message" ></textarea><br /><br />
</div>
<div>
<input type="submit" value="Submit" class="button">
<input type="reset" value="Reset" class="button">
</div>
</form>
Your php file
<?php
if (isset($_REQUEST['contactform']))
{
echo $name = $_REQUEST['name'];
echo $email = $_REQUEST['email'];
echo $PhoneNumber = $_REQUEST['PhoneNumber'];
echo $subject = $_REQUEST['subject'];
echo $message = $_REQUEST['message'];
}
?>
use this code to send email
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Once you have validated the form input (if necessary), you should generate a string and then use the mail function to send an email.
// contact.php
if (isset($_REQUEST['contactform'])) {
# validate the form's input if necessary
$message = "Name: " . $_POST['name'] . "\nPhone No.:" . $_POST['Phone Number'] . "\nMessage:" . $_POST['message'];
mail ('you#example.com', 'Contact form:' . $_POST['subject'], $message, "From: {$_POST['email']}");
}

Categories