How will I write php code for this code? - php

I'm struggling a little to create PHP for this HTML code. I'm more of a front end designer than back end any help? here is what have it should be basic yet I have tried myself to get it to work seem to make it worse each time...
HTML CODE
<form action="form.php" method="post" enctype="multipart/form-data">
<label></label>
<input name="name" required placeholder="Your Name">
<label></label>
<input name="email" type="email" required placeholder="Your Email">
<label></label>
<input name="tel" type="tel" placeholder="Your Contact Number">
<label></label>
<select name="treatment">
<option value="">Select...</option>
<option value="">Anti-Wrinkle Injections</option>
<option value="">Dermal Fillers</option>
<option value="">The Vampire Facelift</option>
<option value="">Other Treatments</option>
</select>
<label></label>
<textarea name="message" cols="20" rows="5" required placeholder="Message"></textarea>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
PHP CODE
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'email';
$subject = 'Email Inquiry';
$body = "From: {$name}\n E-Mail: {$email}\n Message:\n {$message}";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thank you for your email! You will now be redirected to the Home Page.</p>';
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>

This is what the PHP should be:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'an email address';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";?>
Its a ? not a / at the end
I just tested the code, using this php, and it works correctly.

Try This.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'an email address';
$subject = 'Email Inquiry';
$body = "From: ".$name."<br/>E-Mail: ".$email."<br/> Message:<br/>". $message;
?>

You can use the braces style.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: website';
$to = 'an email address';
$subject = 'Email Inquiry';
$body = "From: {$name}\n E-Mail: {$email}\n Message:\n {$message}";
I hope you also have in mind the safety xss attacks. If not, I suggest a small tutorial like this.

Related

creating a contact page in HTML and PHP can getting a return error when testing (HTML 500)

So I am rather new to coding in these languages, I am getting a good understanding of how to write in them but not how to troubleshoot well. I went on and found some videos and read some sites to understand how to get this to work, and got this
HTML
<form id="Contact-form" method="post" action="contactform.php">
<input type="text" name="name" placeholder="Your Name" class="form-control" required>
<input type="text" name="Mail" placeholder="Your E-mail" class="form-control" required>
<input type="text" name="name" placeholder="Subject" class="form-control" required>
<textarea name="message" placeholder="Message" rows="7" class="form-control" required></textarea>
<button class="form-control submit" type="submit" name="sumbit"> Send message</button>
The PHP
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
I have it live and attempted to test send myself an email and I get a basic error (HTML Error 500) and no email, it changes the URL to "contactform.php" as expected. I followed the tutorials to the T, what am I doing wrong!
Thank you for the help in advance
Please change the codes from
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
to
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend");
}

Wordpress Contact Form does not work - probably due to code in functions.php

Below is the code to my wordpress contact form, probably there is some mistake because when I enter my page url http://muzykablog.pl/ the url jumps automatically to http://muzykablog.pl/page-kontakt.php?msg_sent=true ...
This is the code inside my contact page (page-kontakt.php) :
<h4>Send Us Mail</h4><br/>
<?php
if ($_GET[msg_sent]=='true' ) {
echo '<div>Your message has been sent!</div>';
}elseif ($_GET[msg_sent]=='false') {
echo '<div>An error occurred sending your message.</div>';
}else{
?>
<form method="post" action="functions.php">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<?php } ?>
This is the code inside my functions page (functions.php) :
// KONTAKT - MESSAGE SENDING FUNCTIONS FOR page-kontakt.php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: http://muzykablog.pl/';
$to = 'piterdeja#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail($to, $subject, $body, $from)){
header('Location:page-kontakt.php?msg_sent=true');
}else{
header('Location:page-kontakt.php?msg_sent=false');
}
There has to be some mistake here
It could be better to see the mail() function. But seeing the code it seems it will always return true or false so it will redirect to ?msg_sent=false or ?msg_sent=true. For some reason your page is always sending the form when loads. You must avoid that. Its rare too that the action of the form is functions.php. Are you trying to put the form in the home page?
I think there are more deep problems but it could be arranged (not very pro) with this in your functions.php
if($_POST['human']!=""){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: http://muzykablog.pl/';
$to = 'piterdeja#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail($to, $subject, $body, $from)){
header('Location:page-kontakt.php?msg_sent=true');
}else{
header('Location:page-kontakt.php?msg_sent=false');
}
}

Processing a form on the same page

So, I've been messing with this for a day now and I've read a lot tutorials and even posts on stackoverflow. I know it's possible to have all your php form processing steps on the same page as your form, but for some reason, my form won't send an email. Any help?
<div id="mc_embed_signup">
<form method="post" action="index.php" enctype="text/plain" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#address.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if($_POST['submit']) {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
Remove the enctype="text/plain" that's a main factor.
Plus, you also need to check if fields were left empty using (if)empty().
Not doing so, you may receive emails that either do not contain the user's email, name, message or a combination of all.
Assuming this file is called index.php if not, use action=""
<div id="mc_embed_signup">
<form method="post" action="index.php" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#example.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) ){
// check if mail was sent
if(mail ($to, $subject, $body, $headers)){ //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Mail was not sent. Check your logs.</p>";
}
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
This might help,
if(isset($_POST['submit'])) {
if(empty($_POST[name]) && empty($_POST[email]) && empty($_POST[message])){
echo 'Fields cannot be empty';
} else{
$name = $_POST['name'];
$to = $_POST['email'];
$subject = "Hello";
$message = $_POST['message'];
$from = $_POST['email'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body);
}
}else{
echo "<p>Something went wrong, go back and try again!</p>";
}

Simple HTML/PHP Contact Form Not Working

I am trying to create simple HTML/PHP Contact form, But this is not working correctly. Pl check this below given code that's I'm actually trying.
My simple form process
HTML form,
<form action="send_mail.php" method="post" onsubmit="return validateForm();" name="myform" >
<input type="hidden" name="recipient" value="email">
<input type="hidden" name="subject" value="FormMail E-Mail">`
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td align="left">Nome:
<input type="text" name="name" id="name" size="50"></td>
</tr>
<tr>
<td align="left">Email:
<input type="text" name="email" id="email" size="50"></td>
</tr>
<tr>
<td align="left">Mensagem</td></tr>
<tr>
<td><textarea style="resize:none;" cols="44" rows="8" name="message" id="message"></textarea></td>
</tr>
<tr>
<td align="left">
<input name="submit" type="submit" value="Aceder"><input type="reset" value="Recompor">
<input type="hidden" name="redirect" value="thankyou.html"></td>
</tr>
</table>
</form>
PHP Code
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>
I am using this code, plz suggest me if there is any error in my code and any changes required..
Thank you.
quotation marks end on subject?
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company; // quotation marks end?
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>
In your PHP Code on the line 4 the quotation marks are missing at the end of the line before ;.
Correct line 4:
$subject = "Email from Xyz company";
Change above PHP code to this;
<?php
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Failure!";
}
?>
Try this
<?php
if(isset($_POST['submit']))
{
$to = "xyz#abc.com";
$subject = "Email from Xyz company";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf8' . "\r\n";
if( !mail($to, $subject, $body, $headers) )
{
echo "Failure!";
}
}
?>

Form sending email on landing/refresh of website

My form is sending a blank email (field data empty) to me every time i visit my site or refresh it.
I had a look at a few posts mentioning 'Post/Redirect/Get', but that confused me more than helped, and im not sure if it pertains to my exact issue.
This was a basic responsive form i found online and had to find the PHP to actually send the form data. (Changing it to suit my needs)
I'm hoping there's just an error i've missed somewhere but i cant seem to find it.
Any help would be greatly appreciated. Thanks in advance.
Here is the basic form (minus css):
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
HTML:
<form id="contact-form" action="/" method="post">
<h3>Say Hello!</h3>
<h4>Fill in the form below, and I'll get back to you as soon as i can. Thanks!</h4>
<div>
<label>
<span>Name: (required)</span>
<input placeholder="Please enter your name" type="text" name="name" tabindex="1" required autofocus>
</label>
</div>
<div>
<label>
<span>Email: (required)</span>
<input placeholder="Please enter your email address" type="text" name="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span>Telephone: (required)</span>
<input placeholder="Please enter your number" type="text" name="phone" tabindex="3" required>
</label>
</div>
<div>
<label>
<span>Message: (required)</span>
<textarea placeholder="Include all the details you can" type="text" name="message" tabindex="5" required></textarea>
</label>
</div>
<div>
<button name="submit" type="submit" id="contact-submit">Send Email</button>
</div>
</form>
use isset
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
?>
it will post your email only when it submitted by any ..
i have post this one for other in stackoverflow link code link
this is my code.let try , i wish it will help you
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you#youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>

Categories