[Edit - added a complete form snippet] In my html - I have a single checkbox - which looks like this:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet" />
<form class="contact-form row" id="feedbacks" method="POST" action="feedback.php">
<div class="col-xs-10 col-xs-offset-1">
<fieldset class="form-group">
<label for="full_name"></label>
<input type="text" class="form-control" placeholder="Your name and surname" name="full_name" id="full_name">
</fieldset>
<fieldset class="">
<label for="feedback_email"></label>
<input type="text" class="form-control" placeholder="Your Email address" name="feedback_email" id="feedback_email">
</fieldset>
<div class="checkbox">
<label class="c-input c-checkbox">
<input type="checkbox" name="subbed" id="subbed">
<span class="c-indicator"></span>
<span class="text-muted m-l-1">subscribe to <abbr class="msa"></abbr> notifcation service.</span>
</label>
</div>
<fieldset>
<label for="message"></label>
<textarea class="form-control" rows="9" placeholder="Your message here.." name="message" id="message"></textarea>
</fieldset>
<div class="row m-t-1">
<fieldset class="col-xs-4 col-xs-offset-4">
<button class="btn btn-primary btn-block btn-lg" name="submit" type="submit" id="send_feedback">Send <i class="ion-android-arrow-forward"></i>
</button>
</fieldset>
</div>
</div>
</form>
<input type="checkbox" name="subscribed" id="subscribed" value="sub_me">
In my PHP, I've created a variable $subscribe which links to the subscribed checkbox.
The PHP is supposed to send an email of "I would not like to recieve news emails" when the checkbox is left alone. To achieve this I have opted to use the following ternary inside the PHP form validation code:
[Edit - Supplying all the PHP]
<?php
$value = '';
$error = "";
$error_message = "";
$info = "";
if($_SERVER['REQUEST_METHOD'] == "POST"){
$subscribe = 'Would ' . (isset($_POST['subbed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
$admin_email = "myemail_address#gmail.com";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply#domain.ac.za>' . "\r\n";
$headers .= 'Reply-To: ' . $feedback_email . "\r\n";
'X-Mailer: PHP/' . phpversion();
$full_name = $_POST['full_name'];
$feedback_email = $_POST['feedback_email'];
$feedback = $_POST['message'];
$rep_message = "some thank you message to " . $full_name;
$message = 'another message which references ' . $subscribe;
$reply = 'some message consisting of ' . $full_name;
mail($admin_email,"Feedback",$message,$headers,"-fforwardguy#gmail.com");
mail($feedback_email,"Feedback",$reply,$headers);
}
?>
[Edit] The Problem is that the form is only acknowledged as having been sent, but no data is received along with it.
[After edit extra info - Could the following things be affecting the form submission?
`
There are 2 forms on the page in question.
Both forms are submitted via AJAX.
The first form functions as expected.
Two mail functions are being used in this single PHP file (handling only one form).
After taking taking a closer look - I have found that the email which gets sent contains none of the text entered into the form.
(The AJAX function works and the success functions are run.)
Try this:
$subscribe = 'Would ' . (isset($_POST['subscribed']) && $_POST['subscribed'] == 'sub_me' ? 'like to ' : 'not like to ') . 'receive news emails.';
Here I am checking if the $_POST variable is actually set and then we are doing a loose comparison check to see if sub_me is the submitted value.
You could also try leaving the value attribute blank on the form element, then just check if $_POST['subscribed'] is actually set by just using isset($_POST['subscribed']). If the checkbox is NOT checked, isset($_POST['subscribed']) will return false.
Ok - so after I noticed what the actual problem was - I found out that My AJAX function was collecting data from the wrong form.
Related
I have checked all the questions first. But non of them helped to solve my problem.
I have PHP E-mail responder connected to HTML contact form. I need to display selected checkbox value inside the email responder which is sent to customer.
PHP code
<?php
if(isset($_POST) && ($_POST['send'] == 1)){
$documents = array(
'document1' => 'http://www.example.com/document1.doc',
'document2' => 'http://www.example.com/document2.doc',
'document3' => 'http://www.example.com/document3.doc'
'document4' => 'http://www.example.com/document4.doc'
);
$to = 'lubosmasura#gmail.com';
$subject = 'Prihláška na školenie';
$name = $_POST['name'];
$email = $_POST['email'];
$document = implode(", ",$post['document']);
if(isset($_POST['document']) && count($_POST['document']) > 0){
foreach($_POST['document'] as $doc){
if(isset($documents[$doc])){
$document = implode(", ",$post['document']);
$message = "
ŠKOLENIE: $document
";
}
}
}
$headers = 'From: noreply#marcelaskolenia.sk' . "\r\n" .
'Reply-To: noreply#marcelaskolenia.sk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
}
?>
HTML code
<form method="post" action="test.php">
<p class="center">Vyberte školenie</p>
<label class="container riadok"><p for="document" class="dolava">§20 Poučená osoba</p>
<input type="checkbox" name="document[]" value="document1" id="document">
<span class="checkmark"></span>
</label>
<label class="container riadok"><p for="document" class="dolava">Aktualizácia: §21 AŽ §24</p>
<input type="checkbox" name="document[]" value="document2" id="document">
<span class="checkmark"></span>
</label>
<label class="container riadok"><p for="document" class="dolava">§21 Elektrotechnik</p>
<input type="checkbox" name="document[]" value="document3" id="document">
<span class="checkmark"></span>
</label>
<label class="container riadok"><p for="document" class="dolava">§24 Revízny technik</p>
<input type="checkbox" name="document[]" value="document4" id="document">
<span class="checkmark"></span>
</label>
<p class="center">Vyplňte osobné údaje</p>
<input type="text" name="name" id="name" class="form-control" placeholder="Meno">
<input type="text" name="email" id="email" class="form-control" placeholder="Email">
<input type="tel" name="phone" id="phone" class="form-control" placeholder="Telefónne číslo" pattern="[0-9]+" title="Zadajte iba čísla." required="required">
<input type="hidden" name="send" value="1" />
<button type="submit" value="SEND" id="submit" class="btn btn-primary"> ODOSLAŤ PRIHLÁŠKU</button>
Email responder works but it does not display selected checkbox value from HTML form behaind message ŠKOLENIE: CHECKBOX HERE
Any ideas? Thank you
Should be
if(isset($_POST['document']) && count($_POST['document']) > 0) {
foreach($_POST['document'] as $doc){
if(isset($documents[$doc])){
$document = implode(", ",$_POST['document']);
$message = "
ŠKOLENIE: $document
";
}
}
}
You don't have such a variable as $post, but you try to use it twice. Replace it with $_POST and your form will work.
This:
$document = implode(", ",$post['document']);
Replace with this:
$document = implode(", ",$_POST['document']);
And it should work.
Some hints:
Properly configured IDE will let you know about such errors as an undeclared variable. Use PHPStorm (commercial) or VSCode (free).
Don't close php tag (?>) at the end of your file (PSR2):
The closing ?> tag MUST be omitted from files containing only PHP.
Don't use the same identifier (id) multiple times. The id should be unique in the entire HTML document.
Edit: // As someone has mentioned in the comments, you don't have a closing tag for <form> tag, but I assumed that you pasted only a part of your HTML document. Otherwise, you should correct that as well.
I have created a form, but posts are not coming. Form Code
<form method="post" action="contact.php">
<form class="quote">
<div>
<label>Name</label><br>
<input type="text" placeholder="Name">
</div>
<div>
<label>Email</label><br>
<input type="email" placeholder="Emial Address">
</div>
<div>
<label>Message</label><br>
<textarea placeholder="Message"></textarea>
</div>
<button class="button_1" type="submit">Send</button>
</form>
contact.php code
<?php $to = 'demo#spondonit.com'; $firstname = $_POST["fname"]; $email= $_POST["email"]; $text= $_POST["message"]; $headers = 'MIME-Version: 1.0' . "rn"; $headers .= "From: " . $email . "rn"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn"; $message ='<table style="width:100%"> <tr> <td>'.$firstname.' '.$laststname.'</td> </tr> <tr><td>Email: '.$email.'</td></tr> <tr><td>Email: '.$text.'</td></tr> </table>'; if (#mail($to, $email, $message, $headers)) { echo 'The message has been sent.'; }else{ echo 'failed'; } ?>
Maybe the route of the page is wrong, try:
action="/contact.php">
if it's on the same folder
You really need to provide more detail - what does it mean that posts are not coming? Do you see the "failed" message or nothing at all?
But, most importantly: you use identifiers such as $_POST['fname'] but there is nothing in your HTML indicating that a text input value should be sent as "fname". You need to add a name attribute to all of your <input>s, for example:
<input type="text" name="fname" placeholder="Name">
Neither placeholder= or <label> suffice - they only affect how the form is displayed on the webpage and not how it is sent to the server.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am a complete novice using php. I just need to get some information from a web form sent to my email address. I wrote this php a year ago and it worked, but I have come back to re-use it now and it is not working. Can anyone help? Its for a simple wedding RSVP form.
<body>
<center>
<div id="logo"><img src="../content/Logo.png" width="400" height="100" /></div>
</center>
<div id="WebsiteContainer">
<center>
<div id="Menu">
<a href="home.html" >Home</a>
Venue
RSVP
The Day
Gifts
</div>
<div id="Content">
<p> </p>
<h1><strong>RSVP</h1>
<p>
<center>
<p>Please use the form below to RSVP.
If we don't receive an RSVP we will presume that you will not be attending. If you have any requests for our DJ, please let us know in the message box below. And should you have any particular dietary needs, please let us know these too.
<p>
<?php
// PHP creates a $_GET variable automatically which contains any URL parameters passed. We check whether the 'submit' param
// exists using isset(), which is where our form is submitting to. If it does exist, we know the form has been submitted
// and we can process it.
if( isset( $_GET['submit'] ) ){
// Get the variables from the form
// PHP automatically creates the $_POST variable for form data, and the keys are the names we gave the form fields
$name = $_POST['Name'];
$email = $_POST['Email'];
$RSVP = $_POST['RSVP'];
$guests = $_POST['ReplyFor'];
$message = $_POST['message'];
// Lets build the email body so it includes the name too
// PHP concatenates strings with .
$emailBody = $name . " has RSVP'd! \r\n\r\n";
$emailBody .= "Name: " . $name . "\r\n";
$emailBody .= "Email: " . $email . "\r\n";
$emailBody .= "Status: " . ( $RSVP == 'not' ) ? "Not attending" : "Attending" . "\r\n";
$emailBody .= "Guests: " . ( $guests > '1' ) ? "Themselves and " . ( $guests - 1 ) . " guest(s)" : "Themselves only" . "\r\n";
$emailBody .= "Message: " . $message . "\r\n";
// Lines in emails must be under 70 characters, so we can use PHP's wordwrap() function to add line breaks
$emailBody = wordwrap( $emailBody, 70, "\r\n" );
// Create a subject that's easy to browse in your email client
$extraSubject = ( $guests > '1' ) ? "(plus " . ( $guests - 1 ) . " guest(s) )" : "";
$subject = $name . " is " . ( ( $RSVP == 'not' ) ? ' not attending' : ' ATTENDING ' . $extraSubject );
// Send the mail using PHP's built-in mail() function
// See http://php.net/manual/en/function.mail.php
mail( mymail#somewhere.com', $subject, $emailBody );
// Now we can print out a message to them, depending on what they said
$outputMessage = ( $RSVP == 'not' ) ? "We're really sorry you can't make it!" : "We can't wait to see you at the venue in August!";
?>
<div>
<strong>Thanks so much for RSVPing!</strong><br>
<?=$outputMessage?><br><br>
- Sarah & Gavin
</div>
<?
}
else
{
?>
<form id="RSVP_Form" name="form1" method="post" action="?submit=true">
<div>
<label for="Name">Name:</label>
<input name="Name" type="text" id="Name" size="30">
</div>
<div>
<label for="Email">Email:</label>
<input name="Email" type="email" id="Email" size="30">
</div>
<div>
<label>RSVP:</label>
<input name="RSVP" type="radio" value='attending'>Attending
<label></label>
<br><input name="RSVP" type="radio" value='not'>Not Attending
</div>
<div>
<label for="ReplyFor">Replying For:</label>
<select name="ReplyFor" id="ReplyFor">
<option value="1" selected="selected">Yourself Only</option>
<option value="2">Yourself + 1 other invited guest</option>
<option value="3">Yourself + 2 other invited guest</option>
<option value="4">Yourself + 3 other invited guest</option>
<option value="5">Yourself + 4 other invited guest</option>
</select>
</div>
<div>
<label for="message">Message:</label>
<textarea name="message" id="message" rows="3" cols="26"></textarea>
</div>
<div></div>
<div id="submit">
<input name="Submit" type="submit" value=" Submit ">
</div>
</form>
<?
}
?>
</div>
</div>
</body>
</html>
Check out this line:
mail( mymail#somewhere.com', $subject, $emailBody );
You are missing one single quote before email adress.
i have a html form that submit to it self here is the html form
<div class="col-md-6 contact-grid">
<form name="submitted" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="text" id="name" required />
<label>Name</label>
<span></span> </div>
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="email" id="email" required />
<label>Email</label>
<span></span> </div>
<div class="styled-input wow slideInUp animated animated" data-wow-delay=".5s">
<input type="tel" id="phone" required />
<label>Phone</label>
<span></span> </div>
<div class="styled-input wide wow slideInUp animated animated" data-wow-delay=".5s">
<textarea id="message" required></textarea>
<label>Message</label>
<span></span> </div>
<div class="send wow shake animated animated" data-wow-delay=".5s">
<input name="submit" type="submit" value="Send">
</div>
</form>
</div>
and below is my php code to processes the form
<?php
session_start();
//error_reporting(0);
if(isset($_POST['submitted'])) {
$sendto = "info#davfubgroup.com";
$usermail = strip_tags($_POST['email']);
$content = nl2br($_POST['message']);
$phone = strip_tags($_POST['phone']);
$name = strip_tags($_POST['name']);
$subject = "New Feedback Message";
$headers = "From: " . strip_tags($usermail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New User Feedback</h2>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$usermail."</p>\r\n";
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n";
$msg .= "<p><strong>Name:</strong> ".$name."</p>\r\n";
$msg .= "<p><strong>Phone:</strong> ".$phone."</p>\r\n";
$msg .= "</body></html>";
if(mail($sendto, $subject, $msg, $headers)) {
$_SESSION['errormsg'] = "Mail Sent. Thank you we will contact you shortly.";
echo '<center><p style="color:green">' . $_SESSION['errormsg']. '</p></center>';
} else {
$_SESSION['errormsg'] = "Mail not Sent. Try Again.";
echo '<center><p style="color:red">' . $_SESSION['errormsg'].
'</p></center>';
}
}
?>
but my problem is when the form is submitted no message is display and no error message too any help please
just change the name of the input to
<input name="submitted" type="submit" value="Send">
it will work
It doesn't work because you are asking if $_POST has a key named submitted but there's no input element with that name.
Instead, you can check if there is any data in $_POST by asking:
if (count($_POST) > 0)
Your check for post should be: strtoupper($_SERVER['REQUEST_METHOD']) === 'POST' instead of: isset($_POST['submitted']) . Don't use count($_POST) either because the user could post an empty form (perhaps form was just a checkbox that the user didn't check).
When you submit your form, each field will be available in $_POST but the name attribute on the form tag is not submitted and is totally ignored by PHP when you submit your form (not sure about that btw).
In the given example, $_POST will only contain a key submit because this is the only input element with a name attribute.
In your code, assuming you have submitted your form, $_POST['submitted'] is undefined so isset($_POST['submitted']) is never evaluated to true.
You also need to validate user input in PHP, the required attribute doesn't guarantee in any way the validity of data on the server side. You can -at least- check if fields are empty and/or if the provided email is valid with PHP's native function filter_var():
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
// The provided email is invalid
};
You should consider using <span style="text-align: center;"></span> instead of <center> which is not valid.
Hope it helps.
Whilst answered already, in your isset() you are using the name of the form, since the rest of your code can handle the actual usefulness of the data when using your initial isset() you should use the name assigned to your submit button, in this case that would be if(isset($_POST['submit']){}
with the rest of your code then inputted, this is one of those learn from mistakes, using the name of the whole form in this scenario will get you nowhere, refer to individual input elements of the form
Best of luck, hope this helps
I have a html form, which is sending the data through POST to the php-file, and the php-file should process the data (make a good looking structure) and send it via mail.
But the $_POST variable is completely empty....
I have this small html form:
<form id="form" method="post" action="formmailer.php">
<div id="input1">
<input name="name" type="text" placeholder="Ihr Name"> <br>
<input name="email" type="email" placeholder="Ihre E-Mail"> <br>
</div>
<div id="input2">
<textarea name="nachricht" rows="10" cols="30"></textarea> <br>
<input type="submit" value="" name="submit" id="submit">
</div>
</form>
And formmailer.php uses these variables:
<?php //
if(isset($_POST['nt']) && isset($_POST['ntb']))
{
//
$an = "info#website.de";
$name = $_POST['name'];
$email = $_POST['email'];
$nachricht = $_POST['nachricht'];
// Mailheader UTF-8
$mail_header = 'From:' . $email . "n";
$mail_header .= 'Content-type: text/plain; charset=UTF-8' . "rn";
// create layout
$message = "
Name: $name
Email: $email
Nachricht: $nachricht
";
// send mail
mail($an, $message, $mail_header );
}
else {
echo("<p>Email delivery failed…</p>");
}
?>
If i use this if-statement
if (isset($_POST["submit"]))
the mail is sending, but completely empty.
Am i blind? It should be really easy, shouldn't it?
isset($_POST['nt']) && isset($_POST['ntb'])
In html I do not find nt and ntb field.
When you use isset($_POST["submit"]). In Post array php find submit value. But you did not enter any value in the form fields that is why you getting empty value in your mail.
Replace this line from formmailer.php
if (isset($_POST['nt']) && isset($_POST['ntb']))
to:
if ($_POST)
I hope this helps.