I do not now why my form fails - php

I wrote this html and php code to send a form, but I do not understand why it fails.
my code :
html
<form action="formulari.php" method="post">
<p>
Nombre:<input name= "name" type="text";>
</p>
<p>
email:<input name= "email" type="text";>
</p>
<p>Comentario:<textarea rows="4" cols="50">
</textarea name= "message"></p>
<input type="submit" value="enviar" >
<input type="reset" value="borrar" >
</form>
php
<?php
$name = $_POST [ 'name'];
$email = $_POST [ 'email'];
$message = $_POST['comentari'];
$to = "email#gmail.com";
$subject = "Formulari_contacte";
mail ( $to, $subject, $message, $email);
header('Location: ../index.html?message=form_submitted');
?>
Can anyone help ?

Try the following:
HTML:
<form action="formulari.php" method="post">
<p>Nombre: <input name="name" type="text"></p>
<p>email: <input name="email" type="text"></p>
<p>Comentario: <textarea rows="4" cols="50" name="message"></textarea></p>
<input type="submit" value="enviar">
<input type="reset" value="borrar">
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#gmail.com";
$subject = "Formulari_contacte";
$mail = mail( $to, $subject, $message, $email);
// Check if email is send
if( $mail ) {
header('Location: ../index.html?message=form_submitted');
} else {
echo 'Email not send';
}
?>
I removed the ";" and unnecessary spaces in HTML and PHP. You also had the name of the textarea in the closing tag.
Also note that you are open for injection. Users are able to write some javascript code to you.

If you analyze the html code, in that way you text area will miss the name.
You need to set the name of the text area in the opening tag
<textarea name="comment">Enter text here...</textarea>
Plus your are using a different name in the server side, if you are using name ='message' you will need to use 'message' in to get the POST value
Reference:
http://www.w3schools.com/tags/att_textarea_name.asp

I guess you are unable to fetch textarea value
<form action="formulari.php" method="post">
<p>Nombre:<input name= "name" type="text"></p>
<p>email:<input name= "email" type="text"></p>
<p>Comentario:
<textarea name= "comentari" rows="4" cols="50">
</textarea>
</p>
<input type="submit" value="enviar" >
<input type="reset" value="borrar" >
</form>

Thanks to all that answered my question, with your help and google i have made this formulary that has a anti-spam protection and a system to send the user to a page if the mail has been send.
HTML:
<form action="formulari.php" method="post">
<p>Nombre: <input name="name" type="text"></p>
<p>email: <input name="email" type="text"></p>
<input name="edat" style=" display:none;">
<p>Comentario: <textarea rows="4" cols="50" name="message"></textarea></p>
<input type="submit" value="enviar">
<input type="reset" value="borrar">
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "xavicarreragimbert#gmail.com";
$subject = "Formulari_contacte";
if (!empty($_POST['edat'])){
echo "Lo sentimos el sistema antispam ha detectado que es possiblemente un envio realizado a traves de spam";
}
else{
mail( $to, $subject, $message, $email);
header('Location: ../index.html?message=form_submitted');
}
?>

Related

PHP file name added to URL but not called from html

When using a html form to call php to send an email with the details in the form to an email address specified in the php. The php file name is added to the URL but no email is sent and the page shown in blank.
The website is being hosted on my.ionos.co.uk (1&1) using php 7.3.
<form method="post" action="contact-form.php">
<input class="contact" type="text" name="Name" placeholder="Name">
<input class="contact" type="text" name="Email" placeholder="Your Email Address">
<input class="contact" type="text" name="Subject" placeholder="Subject">
<textarea class="contact" name="Message" rows="10" placeholder="Message"></textarea>
<input class="contact-submit" type="submit" value="Send Message">
</form>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['Name'];
$mailFrom = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$mailTo = "X#hotmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$Message;
if(mail($mailTo, $subject, $txt, $headers)){
echo "<h1>Mail send successfully</h1>";
} else {
echo "<h1>Mail failed to send<h1>";
}
header("Location: contact.html?mailsend");
}
?>
I'd appreciate it if someone could help, thanks in advance!
Try to change:
if(isset($_POST['submit'])) {
with
if(isset($_POST['Email'])) { //or another field in your form. If you want you can also add in your submit button: name="submit"
Checking out your code seems there is not an input with name submit. So you never enter in the if case.

MAMP Email PHP difficutly

I've placed my .html file and .php file inside the htdocs (MAMP) folder but whenever I click submit on the my webpage to test it, it sends me to this error page .
I'm not sure why it does that.
HTML code:
<form action="pveemail.php" method="POST">
<p>Email Adress: <input type="text" name="email" size="30"></p>
<p>Subject: <input type="text" name="subject" size="30"></p>
<p>Message: </p>
<p><textarea rows="10" cols="50" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</form>
PHP code:
<?php
$from = "test#something.com";
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
mail($email, $subject, $message, $from);
print "Your message was sent";
?>

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
}
?>

Contact form Error when submit

Hi guys i'm having a issue i hope you guys can help with, i'm typing in all the fields and then upon pressing submit i'm getting just "Error!" on my screen.
Please see the code:
HTML
<h2 class="formhead">Contact Form</h2>
<br>
<form class="form" action="mail.php" method="POST">
<p class="name">
<input type="text" name="name" id="name" placeholder="John Doe" />
<label for="name">Name</label>
</p>
<br>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail#example.com" />
<label for="email">Email</label>
</p>
<br>
<p class="number">
<input type="text" name="number" id="number" placeholder="0774XXXXXXX" />
<label for="name">Contact Number</label>
</p>
<br>
<p class="web">
<input type="text" name="web" id="web" placeholder="www.example.co.uk" />
<label for="name">Website</label>
</p>
<br>
<p class="message">
<textarea name="message" id="message" placeholder="Write something to us" /> </textarea>
</p>
<br>
<p class="submit">
<input type="submit" value="Send"/>
</p>
</form>
PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$website = $_POST['web'];
$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries#c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Any help would be much appreciated!
Thanks
Sam
Your script always reporting 'Error!' because the mail() function always fails. That's because some index you're using in the php file doesn't match to the input names in your form:
Change these:
$website = $_POST['website'];
to:
$website = $_POST['web'];
Or change it in your form.
Also you have to specify a name for the message textarea:
<textarea name="message" id="message" placeholder="Write something to us" />
This may fail again if it can't connect to mailserver. This is probably you're case if The SMTP is Disabled.
As per my comment, here's an example of a better die statement:
<?
$your_function or die("Error! a") // Just replace the letter a with anything. It serves as a simple link to your function that only you know. so you can go back and check it

Why is this not working?

I've this little script to send email! But it's not working... Firstly it says that my variables are not defined and then, they confirm me that the message has been sent, but it doensn't happen!
Name:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="name">
</form> <br><br>
E-Mail:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="header">
</form> <br><br>
Subject:
<form action="contactus.php" method="POST">
<input type="text" size="32" value="" name="subject">
</form> <br><br>
Message: <br>
<form action="contactus.php" method="POST">
<textarea rows="10" cols="40" name="message" value=""></textarea>
</form>
<br><br>
<form action="contactus.php" method="POST">
<input type="submit" value="Submit" name="submit">
</form>
<?php
// php script to send emails
$to = 'some#email.com';
if (isset ($_POST['message'])) {
$message = "$name" . "<br><br>" . $_POST['message'];
}
if (isset ($_POST['header'])) {
$header = "From:" . $_POST['header'];
}
if (isset ($_POST['subject'])) {
$subject = ($_POST['subject']);
}
if (isset ($_POST['name'])) {
$name = ($_POST['name']);
}
if (isset($_POST['submit'])){
mail($to, $subject, $message, $header);
echo "Your message has been sent!";
}
//end of the php script
?>
If some of you can help me would be great!
Thank you.
You can't use all those different form elements. You are using 5 separate forms and the only one that is being submitted is the one with the submit button.
Thus, when the form is being submitted there $_POST['submit'] is set, but none of the other ones exist.
So you need your HTML to be:
<form action="contactus.php" method="POST">
Name:
<input type="text" size="32" value="" name="name"><br><br>
E-Mail:
<input type="text" size="32" value="" name="header"><br><br>
Subject:
<input type="text" size="32" value="" name="subject"><br><br>
Message: <br>
<textarea rows="10" cols="40" name="message" value=""></textarea><br><br>
<input type="submit" value="Submit" name="submit">
</form>
and contactus.php:
<?php
$to = 'some#email.com';
if(isset($_POST['message']) && isset($_POST['header']) &&
isset($_POST['subject']) && isset($_POST['submit']) &&
#mail($to, $_POST['subject'], $_POST['message'], $_POST['header'])) {
echo "Your message has been sent!";
}else{
echo "There has been a problem.";
}
?>
Try:
if(mail($to, $subject, $message, $header)) {
echo "Mail sent successfully.";
} else {
echo "PHP's mail() function failed!";
}
Why you have a Form element for each input elements?
You should put all of your elements in a Form together.

Categories