SOLVED - permissions
I want to walk through my debug process so that it might help anyone else working through the same thing... 1) I wiped both pages and replaced with the code that I knew worked. 2) I then changed the form piece by piece until I got it how i wanted and continued testing 3) I then copied the current php file completely and redirected my form to it. 4) it failed... I changed the permissions to 655 and wallah it worked. Now I can go about hacking about the PHP code to get what I want. thanks for all of the suggestions, you definitely led me down the road to my solution
SOLVED
I have two separate intake forms on a site. Intake form 1 works perfectly. I takes, name, email and comment and sends it through a sendmail script.
I also wanted an intake form for lead capture to track those that want to access the demo videos so I modified the code from the form (for the new page) and then created an additional php file called videoform.php - which is basically just a modified version of my sendmail.php file.
When I fill out the form it does nothing when I click on submit. It validates, as it not let you enter a null value in any of the fields but I am not sure what I am missing. Is it something simple (I am by no means PHP reliable) or can I simply not do that?
Here is the form and the php:
<div class="message"></div>
<form action="./php/videoform.php" method="POST" id="contact-form">
<p class="column one-half">
<input name="name" type="text" placeholder="Your Name" required>
</p>
<p class="column one-half">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="column one-half">
<input name="phone" type="text" placeholder="Your Phone" required>
</p>
<p>
<input name="submit" type="submit" value="Submit">
</p>
</form>
</div>
This is the PHP
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$content = $_POST['content'];
$subject = "You've been contacted by $name";
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
header("Location: /video-form.html");
exit;
}
?>
I am comfortable with a number of coding formats but I am so weak when it comes to PHP. Any insight would be both appreciated and get me on the road to understanding PHP better.
Working scripts for comparison
Form
Send us a message
<p class="column one-half last">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="clear">
<textarea name="comment" placeholder="Your Message" cols="5" rows="3" required></textarea>
</p>
<p>
<input name="submit" type="submit" value="Comment">
</p>
</form>
</div>
PHP sendmail.php file
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$subject = "You've been contacted by $name";
$content = "$name sent you a message from your enquiry form:\r\n\n";
$content .= "Contact Reason: $comment \n\nEmail: $email \n\n";
if(#mail($to, $subject, $content, "Reply-To: $email \r\n")) {
echo "<h5 class='success'>Message Sent</h5>";
echo "<br/><p class='success'>Thank you <strong>$name</strong>, your message has been submitted and someone will contact you shortly.</p>";
}else{
echo "<h5 class='failure'>Sorry, Try again Later.</h5>";
}?>
From your php:
//...
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
//...
You never define $success. Since it doesn't have a value, if ($success) fails, and it always enters the else portion of the statement. It looks like you're missing a line that's something like $success = mail($to, $subject, $content);
Related
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
Newb here, sorry in advance. I want to check for errors in a simple html form. I want the form to email me the customers information. I have my contact.php and errorcheck.php uploaded to my hosting provider. i actually made a working script contactform.php. (working) So I have a clue that my host can process the php code, but my errorcheck.php script is not working. Can anyone check my errorcheck.php script and see what may be causing the problem. all I want errorcheck.php to do so far, is display the error in the web address bar, just so I know that its working. Thanks in advance for helping a newb. I hope I explained my self and code well enough.
contactform.php (working script will connect and send me an email of form information)
errorcheck.php (will not connect or check the form for errors, I want to display the error in web address bar)
contact.php (this is the html form)
errorcheck.php code below
<?php
if (isset($_POST['submit'])) {
$first = $_POST['name'];
$mail = $_POST['mail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//check if inputs are empty
if (empty($first) || empty($mail) || empty($subject) || empty($message)) {
header("Location:contact.php?signup=empty");
exit();
} else {
if (!preg_match("/^[a-zA-Z]*$/", $first)) {
header("Location:contact.php?signup=char");
exit();
} else {
//check if email is valid
if (!fiter_var($mail, FILTER_VALIDATE_EMAIL)) {
header("Location:contact.php?signup=invalid-email");
exit();
} else {
header("Location:contact.php?signup=signup=success");
}
}
}
}
(html code will connect to contactform.php but not errorcheck.php)
contact.php form code is below
<form class="contact-form" action="contactform.php" method="post">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full name..">
<label for="mail">Email Address</label>
<input type="text" name="mail" placeholder="Your E-Mail..">
<label for="subject">Phone Number</label>
<input type="text" name="subject" placeholder="Phone Number..">
<label for="message">Message</label>
<textarea name="message" placeholder="Message.."></textarea>
<input type="submit" value="submit" name="submit"></input>
</form>
contactform.php code is below
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "sales#screenrunners.com";
$headers = "From: ".$mailFrom;
$txt = "You have received and e-mail from ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: contact.php?mailsend");
}
?>
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.
I don't think my title does this question justice but it may get confusing and I don't want to extend the title over several lines.
Here goes:
I have a single page website which has a contact form with the below code:
<form class="move-down" name="contactform" method="post" action="contact-form-handler.php">
<div class="controls controls-row">
<input id="name" name="name" type="text" class="span3" placeholder="Name">
<input id="email" name="email" type="email" class="span3" placeholder="Email address">
</div>
<div class="controls">
<textarea id="message" name="message" class="span6" placeholder="Your Message" rows="7"></textarea>
</div>
<div class="controls pull-right">
<button id="contact-submit" type="submit" class="btn btn-default">Send it!</button>
</div>
</form>
As you can see its action is to call an external php file called "contact-form-handler", which is shown below:
<?php
$errors = '';
$myemail = 'hello#wunderful.co.uk';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= die(header('Location: #errorModal'));
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= die(header('Location: #errorModal'));
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' modal
header('Location: #thanksModal');
}
?>
<?php
?>
This has been working fine but as my new site is one page I don't want separate pages loading, nor do I just want to echo black text on a white background.
So my question is - How do I show the Bootstrap Modal window when the submit button is clicked, with php code from inside the external file AND without the page reloading?
I hope it can be done. If it can't can someone help me launch a modal that says error or thanks when the submit button is clicked?
yes you can. Since you are not using oop way you can do this
1) on page submit you will assign some $mail_send = true; after email is send
2) you fill then say <?php if ($mail_send) { ?> code for modal here <?php } ?>
3) Drop this header('Location: #thanksModal'); you do not need that.
Put $mail_send = true; instead of that.
that is all.
I'm trying to set up a simple contact form. Everything is styled correctly, but when I hit submit it doesn't take me anywhere, just attempts to open contact.php. I think there's something missing in the code that actually sends the message out. I'm sure it's something fairly simple that I'm missing, but this is a little over my head. Any help is appreciated.
<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit">
</form>
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
EDIT: I was able to receive an email finally after adding the complete url for mail.php...however none of the information except for the message was included. The sender was listed as Apache...how can I assure that information entered in the forms will be included in the email? Thanks for all the help thus far.
since you are specifying code within the same page, you can omit action in your form.
Also, put a condition to run PHP once the script has been run.
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information"
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
} }
?>
<form action = "<?= $_SERVER["PHP_SELF"]; ?>"method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit" name="submit">
</form>
Hope it helps!
EDIT: I am not sure what is the current filename but to keep it dynamic, I have mentioned $_SERVER["PHP_SELF"] which is not a recommended usage.
However, Try it! :)