I cant figure out whats wrong with the code.
The problem occurs when i try to send form data to email.
So far its good but i cant get response code to work.
I mean when i fill form and click send it wont show predefined error when some of the forms are empty or when it was sent. Response should be in a same window or form.
Code i have at the moment:
<form name="action" method="post" onsubmit="return false;">
<?php if( isset($error_msg) && $error_msg != '' ) { echo $error_msg; } ?>
<input type="text" name="name" id="name" placeholder="Ees-ja perekonnanimi">
<input type="text" name="cname" id="cname" placeholder="Ettevõtte nimi" style="width: 55%;"> <input type="text" name="regnum" id="regnum" placeholder="Reg. number" style="width: 25%;">
<input type="text" name="email" id="email" placeholder="E-post">
<textarea name="info" placeholder="Sõnum" cols="40"></textarea>
<input type="submit" value="Esita" onclick="return getData()">
<div id="error_msg"><?php echo $error_msg; ?></div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$action=$_REQUEST['action'];
{
$name=$_REQUEST['name'];
$cname=$_REQUEST['cname'];
$regnum=$_REQUEST['regnum'];
$email=$_REQUEST['email'];
$info=$_REQUEST['info'];
if (($name=="")||($email==""))
{
$error_msg="All fields are required";
}
else{
$data="Kliendi nimi: $name \r\nEttevõtte nimi: $cname \r\nEttevõtte reg. number: $regnum \r\nE-post: $email \r\nSõnum: $info";
$subject="Tellimus";
mail("someone#somewhere.com", $subject, $data);
$error_msg="Your message was sent, Thank you!";
}
}
}
?>
</form>
any advice?
Related
I am trying to write a simple html form which requires the user to enter the correct email and password, using $_SERVER as the action for my form. I do not want to send the POST info to another php page, but I want to do it on the same page instead.
I have set two variables, $correct_email and $correct_password.
Here is the form;
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="submit" value="Submit">
</form>
Here is what I am trying to get work in PHP
$correct_email = "(my personal email)";
$correct_password = "password"];
if ($_POST['eml']) == $correct_email && ($_POST['pwd']) == $correct_password {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
It is not working, please help! I am also unclear on whether the PHP should come before or after the form.
Also, I would like to have the form be cleared from view, on the page, when the correct info is entered.
Thanks so much
Change the name of the submit button - never call anything "submit" in a form
Put the test at the top
You had issues with the ( and ) in the test
Also an issue with a ] after "password"
<?php
if ($_POST["subBut"] === "Submit") {
$correct_email = "(my personal email)";
$correct_password = "password";
if ($_POST['eml'] == $correct_email && $_POST['pwd'] == $correct_password) {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
}
else {
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="subBut" value="Submit">
</form>
<?php } ?>
I am using the code below, but want error messages when the user skips a required field. Can anyone help, please? So if a user forgets to fill the required input filed "first name", that an error message appears above the field with the text "First name is a required field".
<form id="comment_form" action="form.php" method="post">
<div class="compulsoryfield">
<input class="inputfield-radio" type="radio" name="gender" value="Mr" required><label class="label-radio">Mr.</label>
<input class="inputfield-radio" type="radio" name="gender" value="Ms"><label class="label-radio">Ms.</label>
<span class="requiredmark-radio">*</span>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="firstname" placeholder="first name" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield1" type="lastname" placeholder="last name" required>
</div>
<input class="inputfield2" type="companyname" placeholder="company name (if applicable)">
<input class="inputfield2" type="customernumber" placeholder="customer number (on invoice if available)" >
<br><br>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="email" placeholder="email address" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield1" type="emailagain" placeholder="re-enter email address (to confirm)" required>
</div>
<input class="inputfield2" type="telephonenumber" placeholder="telephone number (country code included)">
<br><br>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<input class="inputfield3" type="subject" placeholder="subject of message" required>
</div>
<div class="compulsoryfield"><span class="requiredmark">*</span>
<textarea id="textareafieldid" class="textareafield" name="message" placeholder="add your message here" rows="8" cols="39" required></textarea></div><br><br>
<p id="recaptcha-header">before sending, please show us you're real:</p>
<div><span class="requiredmark">*</span><div id="g-recaptcha-outer" class="compulsoryfield2">
<div class="g-recaptcha" data-sitekey="mySitekey"></div></div><br><br>
<input id="button-type1" type="submit" name="submit" value="SEND">
</form>
and
<?php
$email;$comment;$captcha;
if(isset($_POST['gender'])){
$email=$_POST['gender'];
}if(isset($_POST['firstname'])){
$email=$_POST['firstname'];
}if(isset($_POST['lastname'])){
$email=$_POST['lastname'];
}if(isset($_POST['companyname'])){
$email=$_POST['companyname'];
}if(isset($_POST['customernumber'])){
$email=$_POST['customernumber'];
}if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['emailagain'])){
$email=$_POST['emailagain'];
}if(isset($_POST['telephonenumber'])){
$email=$_POST['telephonenumber'];
}if(isset($_POST['subject'])){
$email=$_POST['subject'];
}if(isset($_POST['message'])){
$email=$_POST['message'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the captcha form.</h2>';
exit;
}
$secretKey = "mtSecretKey";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>You are a spammer !</h2>';
} else {
echo '<h2>Thanks for your email.</h2>';
}
?>
I suggest you to try js plugins like :
http://parsleyjs.org/
or
http://www.formvalidator.net/
You gonna see, the first is for me the best , and the second is very simple.
With the second you just have to add an attribute rules to your field like :
data-validation="required".
With this example you can required a field.
send the destination of the form to the same page the form is on.
At the top of the page before any html code is sent to the client, do your form handling.
$problemstring = "";
$firstname = "";
if(isset($_POST['submit'])){
if(!isset($_POST['firstname'])){$problemstring = "Error in firstname"}
//repeat for all fields
}
if(strlen($problemString)==0) {
//Do database actions here
//The Line below redirects to another page if form good
if(strlen($problemString)==0) die(header('Location: index.php'));
}
This code you check your form for blank data then if there are no problems run the database operations. If there is a problem the rest of the page will load.
Put the code below where you want the error message to be displayed.
<?php
if(strlen($problemString)>0) {
echo '<table><tr>';
echo '<td colspan="3">';
echo '<font color="#FF0000"><strong>There was a problem with your form</strong><br>';
echo $problemString;
echo '</font></td>';
echo '</tr>';
echo '<tr><td colspan="3" height="16"></td></tr></table>';
}
?>
In your form do this:
<input class="inputfield3" type="firstname" placeholder="first name" value="<?php echo $firstname; ?>" required>
this will populate the field with what was entered into the form before the submit, if this was the empty field it would be blank. if the page is loaded for the first time the value would be blank (or whatever you set).
Ive used this in my website and it works well.
The code below acts strange when I click the button on my website.
First it opens a new page where the form is again displayed but without any css or js. Then when I refill all the info and click send, it says its submitted but it wont actually sent the email.
I have tried to modify the code I am still new to php.
Code below displays and functions the contact form.
<?php
$action=$_REQUEST['action'];
if ($action=="")
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Personal Plate inquiry from website";
mail("mymail#hotmail.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
There are some mistakes on your code: use $_POST instead of $_REQUEST. You can try this way:
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
if (($name =="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Personal Plate inquiry from website";
mail("mymail#hotmail.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" name="submit" value="Send email"/>
</form>
I'm trying to get my email form working, but something seems to be hanging up. it's not sending out emails at all!
For reference, this uses Wordpress, and this is the code:
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="#" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="username" type="text" value="" size="30"/><br>
Department:<br>
<select id="department" class="form-control-footer">
<option value="Email_0">Sales</option>
<option value="Email_1">Support</option>
<option value="Email_2">Website Feedback</option>
<option value="Email_3">Other</option>
</select><br>
Email Subject<br>
<input name="emailsubject" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_POST['username'];
if (($department=="Email_0"))
{
$mailto=$_POST['example#website.com'];
}
if (($department=="Email_1"))
{
$mailto=$_POST['example#website.com'];
}
if (($department=="Email_2"))
{
$mailto=$_POST['example#website.com'];
}
else
{
$mailto=$_POST['example#website.com'];
}
$emailsubject=$_POST['emailsubject'];
$email=$_POST['email'];
$message=$_POST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill the form again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Webform : $emailsubject";
mail($mailto, $subject, $message, $from);
echo "Thank you for your email! Your email has been sent, and we will try to respond as soon as we can!";
}
}
?>
I've modified the base form to add in departments, which changes the recipient of the contact form. but in doing so, it seems the form no longer sends out those emails at all.
Anyone know what I've done wrong?
Here you need to add "name="department"" to the code below
<select id="department" name="department" class="form-control-footer">
Here you need to change your code as showed below:
if (($_POST['department'] == "Email_0"))
{
$mailto='example#website.com';
}
else if ($_POST['department'] == "Email_1")
{
$mailto = 'example#website.com';
}
...
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.