So this is the first time using a php doc. My page worked fine as a .html, in the contact page is a contact form, hence why I have now changed it to a .php file, as the php for the form is at the top of the document. I have yet to send an email successfully. I am not sure if there is an error in the code, do I need to set something up through CPanel?
https://www.decisive-development.com/contact.php - this is the page that is not loading
https://decisive-development.com/ - this is the homepage
<?php
$message_sent = false;
if(ifsset($_POST['email']) && $_POST['email'] != ''){
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$messageSubject = $_POST['subject'];
$message = $_POST['message'];
$to = "tom#decisive-development.com";
$body = "";
$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";
mail($to,$messageSubject,$body);
$message_sent = true;
}
}
?>
<html lang="en" dir="ltr">
the top of the HTML is at the bottom of this snip, the file name is contact.php (from contact.html) so the htaccess rewrite isn't set up yet.
Below is the html on the same page for the form
<form class="contact-form" action="contact.php" method="POST">
<input class="contact-form-text" type="text" name="name" id="name" placeholder="Full name" tabindex="1" required>
<input class="contact-form-text" type="email" name="email" id="email" placeholder="Your E-mail" tabindex="2" required>
<input class="contact-form-text" type="text" name="subject" id="subject" placeholder="Subject" tabindex="3" required>
<textarea class="contact-form-text" name="message" placeholder="Message" tabindex="4"></textarea>
<button class="contact-form-button" type="submit" name="submit">Send</button>
</form>
Your problem is right here: if(ifsset(
Surely you mean if(isset())
In the future you can add the following two lines of code to the top of a PHP script to help you find errors:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Further, you'll notice that you get a HTTP 500 error when you visit your page. This almost always means that you have a syntax error in your PHP code.
Related
I recently made a website using HTML, CSS, and JS. Since I don't know PHP, I am stuck at building the contact form where it is vital on the website. I learned a bit from YouTube tutorials and have the following HTML & PHP code:
<div class="contact_form">
<form action="/action_page.php">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$mailTo = 'example#something.in';
$headers = 'From: '.$mailFrom;
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsent");
}
?>
Why do I need the MIME and content-type headers at the bottom as that bit I added from another tutorial.
When I use the form and try sending the message, I get the "?mailsent" after the URL but I receive no email which is a professional plan by GoDaddy.
They are also hosting my website. I contacted them to know whether the server allows me to make my contact form with the plan I have and they said yes. So, I must be missing something important here.
refer to the documentation of mail function
there are 3 required parameter : email destination (to), subject and the message, and two additional options are: headers and parameters.
your code didnt respect that, because you missing to add the subjuct as parameter.
and you get ?mailsent because you use header("Location: index.html?mailsent") without any test if the email send successfully or not.
i suggest you to replace the last two lines of your php code with this
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
// error
}
EDIT:
you can get the error message with error_get_last() function.
thanks to https://stackoverflow.com/a/20203870/195835
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
print_r(error_get_last());
}
You are missing the form action, So PHP doesn't know what to do with your variable data.Try adding method="post" inside <form> tag. Like this
<div class="contact_form">
<form action="/action_page.php" method="post">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
And also. If you use your computer as localhost(using xampp , wamp, or something without a hosting service) You have to make some changes to the config files.
Also try this modified php code
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$title = "replace this";
$mailTo = 'support#udichi.in';
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL . 'X-Mailer: PHP/' . phpversion();
mail($mailTo,$title,$txt,$headers);
header("Location: index.html?mailsent");
}
?>
I have a problem with my code. I'm creating a contact form. I don't know about php, I'm learning and I have a problem. What's wrong with this code, every time I refresh the page an email was sent and you I see "Confirm form resubmission" information which is annoying. Can you help me solve these problems?
<?php
$show = "";
if(isset($_POST['submit'])){
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'].".\n\n"."Sent from contact form.";
$to = "Test <test#justtest.com>";
$headers = "From: ".$name."<".$email.">";
mail($to,$subject,$message,$headers);
$show = "<p class='success'>Your message was sent.</p>";
}
}
?>
<form action="index.php" method="POST" class="form">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Your email" required>
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" rows="5" placeholder="Message" required></textarea>
<button type="submit" name="submit">Send</button>
<?php echo $show;?>
</form>
Generally the approach to solve the "Confirm form resubmission" is to redirect after processing a form post. So instead of just re-rendering the page, you'd do something like this:
if(isset($_POST['submit'])){
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
// the rest of the code you already have, then...
header("Location:index.html");
die();
}
}
You can of course replace "index.html" with any page you like, in this case I imagine it would be the current page.
What this does is instruct the browser to not render the current response (if there even is anything in the response) but instead to issue a new GET request to the specified page in the header. So if the user then later refreshes that page, they're only refreshing the GET request and not re-submitting the form.
Edit: You can also still show your message to the user:
$show = "<p class='success'>Your message was sent.</p>";
What you would do in this case is not show the message where you currently have it, but instead include it as a separate operation on the page invoked by a query string parameter. So you might have something like this:
$show = "";
if(isset($_GET['sent'])){
$show = "<p class='success'>Your message was sent.</p>";
}
if(isset($_POST['submit'])){
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
// unchanged code not shown here for brevity
mail($to,$subject,$message,$headers);
header("Location:index.html?sent=true");
die();
}
}
And later in the page you can output the message like you already do:
<?php echo $show;?>
The way this message gets triggered is by the query string paramter used in the redirect:
header("Location:index.html?sent=true");
Which means that technically any time somebody goes to your page with sent=true manually they would see the message without actually sending the email. But if users are tinkering like that then the behavior they get is the behavior they should expect. If you're keen on preventing this otherwise inoccuous tinkering then you could also store a flag in $_SESSION rather than in the query string. That's up to you.
when you submit the form, a post request is sent, and so by reloading the page, the same form is been resubmitted over and over.
In order to solve this, you just need to redirect the user to the same page, instead of returning the page itself, so you need to add
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";); // or paste here the url of the page where the form is located
die();
after
mail($to,$subject,$message,$headers);
So you ends up with this:
<?php
$show = "";
if(isset($_POST['submit'])){
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'].".\n\n"."Sent from contact form.";
$to = "Test <test#justtest.com>";
$headers = "From: ".$name."<".$email.">";
mail($to,$subject,$message,$headers);
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";); // or paste here the url of the page where the form is located
die();
$show = "<p class='success'>Your message was sent.</p>";
}
}
?>
<form action="index.php" method="POST" class="form">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Your email" required>
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" rows="5" placeholder="Message" required></textarea>
<button type="submit" name="submit">Send</button>
<?php echo $show;?>
</form>
php html
In order to keep the <p class='success'>Your message was sent.</p> you can use GET parameters:
<?php
$show = "";
if(isset($_POST['submit'])){
if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'].".\n\n"."Sent from contact form.";
$to = "Test <test#justtest.com>";
$headers = "From: ".$name."<".$email.">";
mail($to,$subject,$message,$headers);
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?redirect=true";); // or paste here the url of the page where the form is located
die();
} else if(isset($_GET['redirect'])){
$show = "<p class='success'>Your message was sent.</p>";
}
}
?>
<form action="index.php" method="POST" class="form">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Your email" required>
<input type="text" name="subject" placeholder="Subject">
<textarea name="message" rows="5" placeholder="Message" required></textarea>
<button type="submit" name="submit">Send</button>
<?php echo $show;?>
</form>
php html
IF (I Started Receiving Spam Bot Forms)
THEN (I Implemented New PHP Email script using a basic Honey Pot Method)
$ERROR (New PHP is not sending ALL the forms fields. Upon sending the form, my email is only receiving the, textarea id="message", field)
$LOG_FILE (My previous PHP script implemented a dynamic catch-all solution for form fields)
$FAILED_SOLUTION (Conversely I attempted to add the individual, $phone & $address fields manually on lines #6 7 & 14 of the PHP but am still only receiving the, textarea id="message", field)
$NOTES (I am self taught & typically only deal with PHP on a need to know basis. Please try to keep it simple and include a step-by-step explanation. Feel free to suggest any "best practices" i may have overlooked unrelated to my problem!)
$QUESTION = "Can someone show me how to call the other form fields in the PHP script to send to my email?"
$SUCCESS = "Thanks in Advance For Any Help That Maybe Given!";
PHP:
<?php
if($_POST){
$to = 'your-email-here#gmail.com';
$subject = 'Contact Form Submission';
$name = $_POST['name'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$email = $_POST['email'];
$message = $_POST['message'];
$robotest = $_POST['robotest'];
if($robotest)
$error = "Spam Protection Enabled";
else{
if($name && $phone && $address && $email && $message){
$header = "From: $name <$email>";
if(mail($to, $subject, $message,$header))
$success = "Your message was sent!";
else
$error = "Error_36 there was a problem sending the e-mail.";
}else
$error = "Error_09 All fields are required.";
}
if($error)
echo '<div class="msg error">'.$error.'</div>';
elseif($success)
echo '<div class="msg success">'.$success.'</div>';
}
?>
HTML FORM:
<form method="post" action="Form_Email.php">
<input type="text" id="name" name="name" placeholder="name" required>
<input type="text" id="phone" name="phone" placeholder="phone" required>
<input type="text" id="address" name="address" placeholder="address" required>
<input type="text" id="email" name="email" placeholder="email" required>
<textarea id="message" name="message" placeholder="message" required> </textarea>
<p class="robotic">
<input name="robotest" type="text" id="robotest" class="robotest" autocomplete="off"/>
</p>
<input type="submit" id="SEND" value="Submit">
</form>
Your message contains only $_POST['message'] for now. If you want to append other values, use concatenation on your $message variable.
$message .= ($name . $phone . $address . $etc)
Notice: A $foo .= $bar construction stands for $foo = $foo . $bar.
Do not forget about whitesigns such as spaces or new lines wherever you want. Simply concatenate ' ' or "\n".
After that, just send a mail using your $message as message.
This question already has answers here:
Why cant I access $_POST variable with a hyphen/dash in its key if passing key as variable?
(2 answers)
Closed 6 years ago.
I've seen dozens of posts about this issue and it basically comes down to a variable not being declared or given a value. However I'm 100% sure it's the same and declared.
I have a basic contact form in HTML and I want it to send me and e-mail when someone hits the submit button. I am debugging the code as well to see what the problem is. The only issue it can find is that there is an Undefined Index which belongs to my text area.
I know that the name of the textarea must be the same as the name on my $_POST in the PHP. Please take a look at the two sections of code and tell me if you can see why it wouldn't be fetching the information from my textarea. The name is message-area.
HTML
<form action="mail.php" method="post" name=contact-me-form >
<label name="firstname secondname">Name: * </label><br>
<input class="half-box" type="text" name="firstname" required >
<input class="half-box" type="text" name="secondname" required ><br>
<p class="first-name">First Name</p>
<p class="second-name">Last Name</p><br>
<label name="email">Email Address: * </label><br>
<input class="full-box" type="email" name="email" spellcheck="false" required><br>
<label name="subject">Subject: </label><br>
<input class="full-box" type="text" name="subject"><br>
<label name="message">Message: * </label><br>
<textarea name="message-area" form="contact-me-form" type="text" placeholder="Please enter your message"></textarea>
<button name="submit" type="submit" value="Submit">Submit</button>
</form>
PHP
<?PHP
$to = "";
$from = "";
$first_name = '';
$last_name = '';
$subject = '';
$message = null;
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
if(isset($_POST['submit'])){
$to = 'email#test.com';
$from = $_POST['email'];
$first_name = $_POST['firstname'];
$last_name = $_POST['secondname'];
$subject = $_POST['subject'];
$message = $_POST["message-area"];
if($message == null){echo "no message detected";}
$headers = "From: " . $from;
$headers = "From:" . $to;
mail($to,$subject,$message,$headers);
}
?>
As you can see the names are identical yet when I submit the data it comes up displaying the following.
int(8) string(29) "Undefined index: message-area" string(58) "/hermes/bosnaweb25a/b2294/ followed by a bit more information and my error gets displayed: ["message"]=> NULL } no message detected.
I honestly have no idea why this isn't being picked up, can anyone with more experience highlight my mistake?
EDIT 1
This is not to do with dashes/hyphens as I've edited my code as you can see below. It's also important to note that if I change this to raw text it still doesn't work, still acts as if there is no data from the textarea.
HTML
<form action="mail.php" method="post" id=contact-me-form >
<label name="firstname secondname">Name: * </label><br>
<input class="half-box" type="text" name="firstname" required >
<input class="half-box" type="text" name="secondname" required ><br>
<p class="first-name">First Name</p>
<p class="second-name">Last Name</p><br>
<label name="email">Email Address: * </label><br>
<input class="full-box" type="email" name="email" spellcheck="false" required><br>
<label name="subject">Subject: </label><br>
<input class="full-box" type="text" name="subject"><br>
<label name="message">Message: * </label><br>
<textarea name="messagearea" type="text" placeholder="Please enter your message"></textarea>
<button name="submit" type="submit" value="Submit">Submit</button>
</form>
PHP
<?PHP
$to = "";
$from = "";
$first_name = '';
$last_name = '';
$subject = '';
$message = null;
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
if(isset($_POST['submit'])){
$to = 'harry.brockley#hotmail.co.uk';
$from = $_POST['email'];
$first_name = $_POST['firstname'];
$last_name = $_POST['secondname'];
$subject = $_POST['subject'];
$message = $_POST["messagearea"];
if($message == null){echo "no message detected";}
$headers = "From: " . $from;
$headers = "From:" . $to;
mail($to,$subject,$message,$headers);
}
?>
EDIT 2
Tested it with a hard coded value works so it has to be the variable name. It's just strange that it only happens on the textarea.
Here is your solution just replace below line with your text-area code.
<textarea name="message-area" placeholder="Please enter your message"></textarea>
here it is no need form="contact-me-form" text.
Thank you for your help and I found out the problem and it seems to work. I haven't actually received the e-mail yet (Possibly terrible Vietnamese Internet).
The problem was that my variables defined as $variable = "" were not set as null as I thought. I had to go through and assign = null to all of my variables and it seemed to bypass the error.
You seem to have a random form="" attribute in the <textarea></textarea>
If you change it to the following it should work.
<textarea name="message-area" type="text" placeholder="Please enter your message"></textarea>
With respect to the check on $message not working with ($message == null), you should use empty() to check it as that will catch NULL and "".
if (empty($message)) {
echo "no message detected";
}
I am developing a site that uses javascript to handle many functions and PHP based Captcha code to validate the form field.
It works great, but the form will submit if none of the fields are filled in.
I need one of two form fields ['email' or 'phone'] filled in, but neither can be left blank.
The error message can be the same error message thrown up when the captcha field is left blank or filled in incorrectly.
I am new to PHP code and cannot figure out for the life of me how to call the function.
The function code is:
<?php
if(isset($_POST['send'])){
$emailFrom = "clientemail.com";
$emailTo = "clientemail.com";
$subject = "Contact Form Submission";
$first_name = strip_tags($_POST['first_name']);
$last_name = strip_tags($_POST['last_name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$message = strip_tags(stripslashes($_POST['message']));
$body .= "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
$headers = "From: ".$emailFrom."\n";
$headers .= "Reply-To:".$email."\n";
if($_SESSION['security_code'] == $_POST['security_code']){
$success = mail($emailTo, $subject, $body, $headers);
if ($success){
echo '<p class="yay">Your e–mail has been sent.</p>';
}
} else {
echo '<p class="oops">Something went wrong. Hit the back button and try again.</p>';
}
} else {
?>
The form field:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id="contact" name="contact">
<fieldset>
<label for="name"><span style="color:#bf252b;">*</span>First Name</label>
<input type="text" id="first_name" name="first_name" minlength="2"/>
<label for="name">Last Name</label>
<input type="text" id="last_name" name="last_name" minlength="2"/>
<label for="email"><span style="color:#bf252b;">*</span> Email</label>
<input type="text" id="email" name="email" />
<label for="phone"><span style="color:#bf252b;">*</span> Phone</label>
<input type="text" id="phone" name="phone" />
<label for="message">Message</label><div style="clear:both;"></div>
<textarea id="message" name="message" cols="40" rows="10" ></textarea>
<img src="../captcha.php" id="captcha" alt="captcha" style="padding:25px 0px 20px 0px;" />
<label for="security_code">Enter captcha</label>
<input type="text" id="security_code" name="security_code" autocomplete="off" class="required"/>
<button type="submit" id="send" name="send" style="margin:0px 0px 10px 12px;">Send!</button>
</fieldset>
</form>
<?php } ?>
There is a .php document for running captcha, but am I right in thinking there is a simple solution for this; some extra code in the existing code that will fix my issue? I really want to avoid javascript and plugins if I can help it.
Thanks in advance!!
Try this,
if($_SESSION['security_code'] == $_POST['security_code'] && (!empty($email) || !empty($phone))) {
instead of
if ($_SESSION['security_code'] == $_POST['security_code']) {
This is not convenient way to validate form but I hope this will help.
Add the required attribute to the fields you need required.
Fix your code!
Notice: Undefined variable: body in /../sendform.php on line 14
Do that:
$body = "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
Solution
if(($_SESSION['security_code'] == $_POST['security_code']) AND
(!empty($email) OR !empty($phone)) ) {
In other words:
Security code must be match
Email or Phone can not be empty