My php mail form sends 2 same mails each time.
I'm new to php. I've tried things I can do, but couldn't find the solution by myself so far...
PHP version is 5.2.17
It somehow works right on IE, Firefox and Safari on Windows 8.
Use mail() instead of mb_send_mail(), but it was the same.
The contents of the mails I received from the form seems OK.
Uploaded on another server, but was the same.
Simple code without any other code, with very simple HTML also didn't work properly.
example: mb_send_mail( "[email address]", "Test", "It's a test mail", "From:[email address]" );
Here's my code from the form.
form.html
<form action="./confirm.php" method="post">
xxxxxxxxxxxxxxxxxxxxxxxx
<input type="submit" value="Confirm">
</form>
*Uses Angular to validate.
confirm.php
<?php
session_start();
if(!$_POST){
header('Location: ./form.html');
}
foreach ($_POST as $key => $value) {
${$key} = $value;
$_SESSION[$key] = $value;
}
?>
<form action="./send.php" method="post">
xxxxxxxxxxxxxxxxxxxxxxxx
<input type="submit" name="submit" value="Send">
</form>
send.php
session_start();
$email_address = "xxxxx#gmail.com";
$message = "xxxxxxxxxxxxxxxxxxxxxxx";
$message .= "xxxxxxxxxxxxxxxxxxxxxxx";
mb_language("ja");
mb_internal_encoding("UTF-8");
$header = "From:".$email_address."\n";
$header .= "Reply-to:".$email_address."\n";
$header .= "Content-Type: text/plain\n";
$header .= "X-Mailer:PHP/". phpversion();
mb_send_mail($_SESSION['email'],"subjecct",$message,"From:$email_address");
session_destroy();
Why is my code sending each mail twice, and how can I fix it?
After session_destroy();, add some debug marks to check if this PHP runs twice.
e.g. file_put_contents('debug.txt', file_get_contents('debug.txt')+1);
It can help.
Thanks #zairwolf for your advice to add a debug line.
Now it's working correctly.
Not sure which made solve it because I've tried few things to fix it.
I think it could be one of these:
Added session token which is like:
confirm.php
$token = sha1(uniqid(mt_rand(), true));
send.php
$key = array_search($_POST['token'], $_SESSION['token']);
if ($_SESSION['xxxxx']=="" || $key == false) {
header('Location: ./error.php');
}
Separate "thankyou.php" from "send.php"
I put both "sending program" and "showing thank you html" in "send.php" actually. So noticed might should separate it somehow.
Related
I need some help with my code as I have got a problem with get pass on the if statement. I am working on the clean url to create a function like create_newsletter.php?template=new when I am on the same page.
When I try this:
if(isset($_POST['submit']))
{
sleep(2)
header("Location: http://example.com/newsletters/create_newsletter.php?template=new");
if(isset($_GET['template'])
{
echo "hello robert now you are working on the template";
}
}
It will not get pass on this line:
if(isset($_GET['template'])
Here is the full code:
<?php
$template = "";
if(isset($_GET['template']))
{
$template = $_GET['template'];
}
if(isset($_POST['submit']))
{
sleep(2)
$messagename = $_POST['messagename'];
$subject = $_POST['subject'];
header("Location: http://example.com/newsletters/create_newsletter.php?template=new");
if(isset($_GET['template'])
{
echo "hello robert now you are working on the template";
}
}
?>
<form method="post">
<input type="text" name="messagename" value="">
<input type="text" name="subject" value="">
<input type="submit" name="submit" name="submit" class="btn btn-primary" value="Next Step">
</form>
I have got no idea how I can get pass on the if statement when I am using header("Location:). I have also tried if ($template) but it doesn't get pass.
What I am trying to do is to connect to my php page create_newsletter.php. I want to input my full name the textbox called messagename and input the subject in the subject textbox then click on a button. When I click on a button, I want to redirect to create_newsletter.php?template=new as I want to disable on two textbox controls messagename and subjectthen add the iframe to allow me to get access to another php page so I could write the newsletter in the middle of the screen.
Can you please show me an example what is the best way forward that I could use to get pass on the if statement when I click on a submit button to redirect me to create_newsletter.php?template=new so I could disable these controls and add the iframe?
Thank you.
You are checking if(isset($_GET['template']) inside the if(isset($_POST['submit'])) condition, but the redirect doesn't send a post request.
This should work:
if(isset($_POST['submit']))
{
sleep(2)
$messagename = $_POST['messagename'];
$subject = $_POST['subject'];
header("Location: http://example.com/newsletters/create_newsletter.php?template=new");
}
if(isset($_GET['template'])
{
echo "hello robert now you are working on the template";
}
But if you need to make a POST request in the redirect, you would need to print a <form> and submit it in the client side, or use $_SESSION in the example bellow:
session_start();
if(isset($_POST['submit']))
{
sleep(2)
$_SESSION['messagename'] = $_POST['messagename'];
$_SESSION['subject'] = $_POST['subject'];
header("Location: http://example.com/newsletters/create_newsletter.php?template=new");
}
if(isset($_GET['template'])
{
// $_SESSION['messagename'] and $_SESSION['subject'] are available here
echo "hello robert now you are working on the template";
}
When you are checking if(isset($_POST['submit'])), you are redirecting before you can reach the if(isset($_GET['template']).
But I am assuming you would expect this to run because $_GET['template'] will be set. Although, the problem with your code is that when you redirect, $_POST['submit'] will not be set, therefor it will not execute anything in the if(isset($_POST['submit'])) block, including if(isset($_GET['template']).This is because a POST request is not persistant, and will not remain if you reload, or redirect
You should consider the following:
if(isset($_POST['submit']))
{
sleep(2)
$messagename = $_POST['messagename'];
$subject = $_POST['subject'];
header("Location: http://example.com/newsletters/create_newsletter.php?template=new");
}
if(isset($_GET['template'])
{
echo "hello robert now you are working on the template";
}
?>
Accessing the $messagename and $subject in the if(isset($_GET['template'])
If you want to access the $messagename and $subject in the if(isset($_GET['template']), you can pass them in the URL. Because when you redirect, no $_POST variables will be set, they will go away. You can accomplish this by doing:
if(isset($_POST['submit']))
{
sleep(2)
$messagename = $_POST['messagename'];
$subject = $_POST['subject'];
header("Location: http://example.com/newsletters/create_newsletter.php?template=new&messagename=".$messagename."&subject=".$subject);
}
if(isset($_GET['template'])
{
$messagename = $_GET['messagename'];
$subject = $_GET['subject'];
echo "hello robert now you are working on the template";
}
?>
There are two errors in the OP's code which unfortunately the officially accepted answer reflects as well. A semi-colon needs to be appended to the statement that uses sleep() and an extra parenthesis is needed in the statement that tests for $_GET['template'].
In truth, one does not need to complicate the code with signal processing offered by sleep() in order to delay submission of the POSTed data just to determine the value of $_GET['template']. One could omit sleep() and alter the the code slightly, as follows:
<?php
if( isset($_POST['submit']) )
{
$mess = htmlentities($_POST['mess']);
$subject = htmlentities($_POST['subject']);
header("Location: http://localhost/exp/create_newsletter.php?template=new");
exit;
}
else
if( isset( $_GET['template']))
{
echo "hello robert now you are working on the template";
exit;
}
Also, instead of using $_GET another alternative is to use $_SERVER['QUERY_STRING'], as follows:
<?php
$qs = parse_url($_SERVER['PHP_SELF'], PHP_URL_QUERY);
if( $qs == 'template=new' ){
$template = split("=",$qs)[1];
echo "hello robert now you are working on the template";
exit;
}
else
if(isset($_POST['submit']))
{
sleep(2);
$mess = htmlentities($_POST['mess']);
$subject = htmlentities($_POST['subject']);
header("Location: http://localhost/exp/create_newsletter.php?template=new");
exit;
}
?>
<html>
<head><title>test</title></head>
<body>
<form method="post" action="">
<input type="text" name="mess" value="">
<input type="text" name="subject" value="">
<input type="submit" name="submit" class="btn btn-primary" value="Next Step">
</form>
</body>
</html>
The component parameter of parse_url() enables this function to return the query string. One may also opt instead to employ parse_str(), as follows:
<?php
$queries = "";
parse_str($_SERVER['QUERY_STRING'], $queries);
if( isset($queries['template']) && ($queries['template'] == 'new'))
{
$template = $queries;
echo "hello robert now you are working on the template";
exit;
}
else
if(isset($_POST['submit']))
{
sleep(2);
$mess = htmlentities($_POST['mess']);
$subject = htmlentities($_POST['subject']);
header("Location: http://localhost/exp/create_newsletter.php?template=new");
exit;
}
?>
Note: it is very important to always treat data from a POST or GET as tainted instead of directly assigning the data to a variable and using that variable. Using htmlentities() is one way to attempt to prevent possible security issues.
I'm trying to add a PHP form to a website I'm working on. Not real familiar with PHP, but I've put the file in the upload folder in the CMS.
I think I've linked the jQuery and other files correctly and I've edited the PHP file putting in emails etc. This one also calls on another PHP validation file.
Anyway it shows up normally and I can fill it out but it goes to a 404 page and doesn't work.
My question is, what linking convention do I use to link to the php file and is it in the right place? I use cPanel where the CMS is installed.
Instead of putting: action="url([[root_url]]/uploads/scripts/form-to-email.php"
should I just put: action="uploads/scripts/form-to-email.php"?
The page in question is here: www.edelweiss-web-design.com.au/captainkilowatt/
Also, anyone know a good captcha I can integrate with it...? Thanks!
<div class="contact-form">
<h1>Contact Us</h1>
<form id="contact-form" method="POST" action="uploads/scripts/form-to-email.php">
<div class="control-group">
<label>Your Name</label>
<input class="fullname" type="text" name="fullname" />
</div>
<div class="control-group">
<label>Email</label>
<input class="email" type="text" name="email" />
</div>
<div class="control-group">
<label>Phone (optional)</label>
<input class="phone" type="text" name="phone" />
</div>
<div class="control-group">
<label>Message</label>
<textarea class="message" name="message"></textarea>
</div>
<div id="errors"></div>
<div class="control-group no-margin">
<input type="submit" name="submit" value="Submit" id="submit" />
</div>
</form>
<div id='msg_submitting'><h2>Submitting ...</h2></div>
<div id='msg_submitted'><h2>Thank you !<br> The form was submitted Successfully.</h2></div>
</div>
Here is the php:
<?php
/*
Configuration
You are to edit these configuration values. Not all of them need to be edited.
However, the first few obviously need to be edited.
EMAIL_RECIPIENTS - your email address where you want to get the form submission.
*/
$email_recipients = "contact#edelweiss-web-design.com.au";//<<=== enter your email address here
//$email_recipients = "mymanager#gmail.com,his.manager#yahoo.com"; <<=== more than one recipients like this
$visitors_email_field = 'email';//The name of the field where your user enters their email address
//This is handy when you want to reply to your users via email
//The script will set the reply-to header of the email to this email
//Leave blank if there is no email field in your form
$email_subject = "New Form submission";
$enable_auto_response = true;//Make this false if you donot want auto-response.
//Update the following auto-response to the user
$auto_response_subj = "Thanks for contacting us";
$auto_response ="
Hi
Thanks for contacting us. We will get back to you soon!
Regards
Captain Kilowatt
";
/*optional settings. better leave it as is for the first time*/
$email_from = ''; /*From address for the emails*/
$thank_you_url = 'http://www.edelweiss-web-design.com.au/captainkilowatt.html';/*URL to redirect to, after successful form submission*/
/*
This is the PHP back-end script that processes the form submission.
It first validates the input and then emails the form submission.
The variable $_POST contains the form submission data.
*/
if(!isset($_POST['submit']))
{
// note that our submit button's name is 'submit'
// We are checking whether submit button is pressed
// This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!".print_r($_POST,true);
exit;
}
require_once "http://edelweiss-web-design.com.au/captainkilowatt/upload/scripts/formvalidator.php";
//Setup Validations
$validator = new FormValidator();
$validator->addValidation("fullname","req","Please fill in Name");
$validator->addValidation("email","req","Please fill in Email");
//Now, validate the form
if(false == $validator->ValidateForm())
{
echo "<B>Validation Errors:</B>";
$error_hash = $validator->GetErrors();
foreach($error_hash as $inpname => $inp_err)
{
echo "<p>$inpname : $inp_err</p>\n";
}
exit;
}
$visitor_email='';
if(!empty($visitors_email_field))
{
$visitor_email = $_POST[$visitors_email_field];
}
if(empty($email_from))
{
$host = $_SERVER['SERVER_NAME'];
$email_from ="forms#$host";
}
$fieldtable = '';
foreach ($_POST as $field => $value)
{
if($field == 'submit')
{
continue;
}
if(is_array($value))
{
$value = implode(", ", $value);
}
$fieldtable .= "$field: $value\n";
}
$extra_info = "User's IP Address: ".$_SERVER['REMOTE_ADDR']."\n";
$email_body = "You have received a new form submission. Details below:\n$fieldtable\n $extra_info";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
#mail(/*to*/$email_recipients, $email_subject, $email_body,$headers);
//Now send an auto-response to the user who submitted the form
if($enable_auto_response == true && !empty($visitor_email))
{
$headers = "From: $email_from \r\n";
#mail(/*to*/$visitor_email, $auto_response_subj, $auto_response,$headers);
}
//done.
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest')
{
//This is an ajax form. So we return success as a signal of succesful processing
echo "success";
}
else
{
//This is not an ajax form. we redirect the user to a Thank you page
header('Location: '.$thank_you_url);
}
?><?php
/*
Configuration
You are to edit these configuration values. Not all of them need to be edited.
However, the first few obviously need to be edited.
EMAIL_RECIPIENTS - your email address where you want to get the form submission.
*/
$email_recipients = "contact#edelweiss-web-design.com.au";//<<=== enter your email address here
//$email_recipients = "mymanager#gmail.com,his.manager#yahoo.com"; <<=== more than one recipients like this
$visitors_email_field = 'email';//The name of the field where your user enters their email address
//This is handy when you want to reply to your users via email
//The script will set the reply-to header of the email to this email
//Leave blank if there is no email field in your form
$email_subject = "New Form submission";
$enable_auto_response = true;//Make this false if you donot want auto-response.
//Update the following auto-response to the user
$auto_response_subj = "Thanks for contacting us";
$auto_response ="
Hi
Thanks for contacting us. We will get back to you soon!
Regards
Captain Kilowatt
";
/*optional settings. better leave it as is for the first time*/
$email_from = ''; /*From address for the emails*/
$thank_you_url = 'http://www.edelweiss-web-design.com.au/captainkilowatt.html';/*URL to redirect to, after successful form submission*/
/*
This is the PHP back-end script that processes the form submission.
It first validates the input and then emails the form submission.
The variable $_POST contains the form submission data.
*/
if(!isset($_POST['submit']))
{
// note that our submit button's name is 'submit'
// We are checking whether submit button is pressed
// This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!".print_r($_POST,true);
exit;
}
require_once "http://www.edelweiss-web-design.com.au/captainkilowatt/upload/scripts/formvalidator.php";
//Setup Validations
$validator = new FormValidator();
$validator->addValidation("fullname","req","Please fill in Name");
$validator->addValidation("email","req","Please fill in Email");
//Now, validate the form
if(false == $validator->ValidateForm())
{
echo "<B>Validation Errors:</B>";
$error_hash = $validator->GetErrors();
foreach($error_hash as $inpname => $inp_err)
{
echo "<p>$inpname : $inp_err</p>\n";
}
exit;
}
$visitor_email='';
if(!empty($visitors_email_field))
{
$visitor_email = $_POST[$visitors_email_field];
}
if(empty($email_from))
{
$host = $_SERVER['SERVER_NAME'];
$email_from ="forms#$host";
}
$fieldtable = '';
foreach ($_POST as $field => $value)
{
if($field == 'submit')
{
continue;
}
if(is_array($value))
{
$value = implode(", ", $value);
}
$fieldtable .= "$field: $value\n";
}
$extra_info = "User's IP Address: ".$_SERVER['REMOTE_ADDR']."\n";
$email_body = "You have received a new form submission. Details below:\n$fieldtable\n $extra_info";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
#mail(/*to*/$email_recipients, $email_subject, $email_body,$headers);
//Now send an auto-response to the user who submitted the form
if($enable_auto_response == true && !empty($visitor_email))
{
$headers = "From: $email_from \r\n";
#mail(/*to*/$visitor_email, $auto_response_subj, $auto_response,$headers);
}
//done.
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest')
{
//This is an ajax form. So we return success as a signal of succesful processing
echo "success";
}
else
{
//This is not an ajax form. we redirect the user to a Thank you page
header('Location: '.$thank_you_url);
}
?>
I've added the php file.
So, in the action part, when I submit the form, it no longer gives me a 404 but takes me to a blank page with the 'form-to-email.php' page. However, the script is not working from what I can tell. Again, I know html and css, and little javascipt, but how php is meant to work...?
What am I doing wrong?
I would suggest using one of the modules for CMS instead of trying to build form in PHP from scratch. It is much more safer to use CMS buildin functions and that is the point of using the CMS in the first place. For CMS made simple the formbuilder module is here:
http://dev.cmsmadesimple.org/projects/formbuilder
Thanks for all the comments.
I found another form with a captcha (PHP) and preserved the whole structure by uploading it as is into CMSMS uploads folder.
I then used iframe to embed the form on my page, changed a couple of little details with the CSS and wording, and bob's your uncle, it works just fine.
For anyone interested, I used: www.html-form-guide.com/contact-form/creating-a-contact-form.html
This is free and I am certainly not trying to spam as I am in no way affiliated with this site or any sites associated with it.
I'm doing a bit of web development for a client of mine, and I've run into a roadblock.
**Here's what's going on: **
-We have multiple copies of the same contact form on our website, but we want each to redirect to a different landing page upon completion in order to track conversion rates.
-In this example, I'm working on the about page. When a user submits their request for more information, the page would ideally redirect to a unique Thank-You page (e.g., www.sample.com/thank-you-about.html); however, it's not.
-The email will get sent off perfectly, but the redirection will never take place. No errors are thrown - nothing.
BEFORE I show the code, here's what I've tried in order to solve it on my own:
-I've created a body of HTML code underneath the .php code that the form sends its input to. To my knowledge, no redirect is taking place, so I've obviously not seen it yet.
-I've used the call "Header(Location:www.sample.com/thank-you-about.html)", but that's not doing anything either.
-I've tried using Javascript's "frame.location" method, and it doesn't seem to get any further than the php attempt.
-I've saved the source page and destination page as .php pages on the off-chance that using an .html extension is at the root of the problem; same outcome.
Really, the only thing left that I can think of is the connection type not satisfying the requirements for a Header() call. But wouldn't that output an error message?
Here's the contact form in question:
<form id="contactForm" class="get-in-touch contact-form light" action="thank-you-about.php" method="post">
<input type="hidden" id="contact_nonce" name="contact_nonce" value="68592e213c"/><input type="hidden" name="" value="/"/>
<input type="hidden" name="contact-form-value" value="1"/>
<div class="iconic-input">
<input class="name" type="text" name="name" placeholder="Name*">
<i class="icons icon-user-1"></i>
</div>
<div class="iconic-input">
<input class="email" type="text" name="email" placeholder="Email*">
<i class="icons icon-email"></i>
</div>
<textarea class="msg" name="msg" placeholder="Message"></textarea>
<input type="submit" value="Send Message">
<div class="iconic-button">
<input type="reset" value="Clear">
<i class="icons icon-cancel-circle-1"></i>
</div>
</form>
Here's the current .php file, thank-you-about.php:
<?php
// Define some constants
define( "RECIPIENT_NAME", "Sample" );
define( "RECIPIENT_EMAIL", "writewoodcopy#gmail.com" );
define( "EMAIL_SUBJECT", "New request from site sample webpage" );
// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\#a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$message = isset( $_POST['msg'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['msg'] ) : "";
$messageInterests = "";
$subject = "";
$currentURL = isset($_POST['current-URL'])? $_POST['current-URL'] : "";
//Parse checkboxes, include them in message body
$interestArray = $_POST['interest'];
if(!empty($interestArray)){
$messageInterests .= "INFORMATION OF INTEREST: ";
$N = count($interestArray);
for($i = 0; $i < $N; $i++)
{
$messageInterests .= "\r\n" . $interestArray[$i];
}
$subject .= $messageInterests . "\r\n \r\n";
}
else {
$subject = "GENERAL INQUIRY \r\n \r\n";
}
$subject .= $message;
$message = $subject;
// If all values exist, send the email
if ( $senderName && $senderEmail && EMAIL_SUBJECT && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
mail( $recipient, EMAIL_SUBJECT, $message, $headers );
header("Location: www.sample.com/thank-you-about.html"); /* Redirect browser */
}
?>
EDIT:: Before anyone asks, I know that no checkboxes are being parsed from this particular contact form. They come in handy on my client's service pages, while general inquiries are handled by the about page, contact us page, and homepage.
SECOND EDIT:: So, I fixed the absolute URL problem; however, the redirect problem is still persisting. Thank you all for being so patient!
Does a PHP Header(Location:) redirect need to point to a website
starting with “http://”?
Yes, if you need to redirect to a file on a different domain, you'll have to specify the full absolute URL, this includes the protocol (http, https, etc.), based on that, this doesn't work:
header("Location: www.sample.com/thank-you-about.html");
use:
header("Location: http://www.sample.com/thank-you-about.html");
If you need to redirect to a file on the same domain you can use a relative path:
header("Location: thank-you-about.html"); //thank-you-about.html is located on the same dir as the current script
or
header("Location: /thank-you-about.html"); //thank-you-about.html is located on the root dir of the domain
header() is used to send a raw HTTP header. See the » HTTP/1.1
specification for more information on HTTP headers.
Read more about the php header() function.
My suggestion would be to pass the redirect parameter as part of the action attribute, i.e
...action="sendEmail.php?redirect=thank-you-about" method="post">
Then read it out server side and redirect accordingly.
// do your sending email logic here, then redirect:
$r = $_GET['redirect];
switch($r){
case: 'thank-you-about':
header("Location: /thank-you-about.html"); die();
// it might be tempting to go 'header("Location: /$r")' directly, but this would introduce quite a security vulnerability.
break;
case: 'otherpage':
header("Location: /thank-you-other.html"); die();
break;
default:
header("Location: /thank-you-default.html"); die();
break;
}
I am trying to migrate our site to a new host that uses PHP5. Previously we were on PHP4.
I have a form that is not submitting and redirecting to the thankyou page after a user fills out a survey which worked previously on php4. I'm sure it's probably something obvious that I'm missing but I can't see why it doesn't work.
When I click submit, the survey page reloads, the URL gets ?submit=t added to the end and the email is not sent to me.
The code from our survey.php is shown below with my email address and most of the HTML form feilds removed. Can somebody point me in the right direction?
Thanks!
<?
$APP_ROOT = "../";
$FILE = __FILE__;
$TITLE="Service Survey";
if(!isset($submit)) {
include($APP_ROOT."include/header.php");
include($APP_ROOT."include/nava.php");
?>
<div class="bodymargin">
<img src="<?=$WEB_ROOT;?>images/titles/<?=$SECTION."_".$FILE;?>.gif" width="400" height="36"><br>
<br>
<form method="POST" action="<?=$FILE;?>.php?submit=t">
<?
if(isset($error)) {
while(list($key, $value) = each($HTTP_GET_VARS)) {
$$key = stripslashes($value);
}
print("<p class=\"alert\">". urldecode($error) . "</p>\n");
}
?>
<input type="text" name="name" value="<?=$name;?>">
</form>
<?
include($APP_ROOT."include/navb.php");
include($APP_ROOT."include/footer.php");
} else {
include_once($APP_ROOT . "functions/index.php");
include_once($APP_ROOT . "functions/form_validation.php");
$CONTINUE = TRUE;
$valid = new Validation($HTTP_POST_VARS);
if($CONTINUE = $valid->success) {
$to = "myemailaddress";
$subject = "Service Survey";
$from_email = $to;
$from_name = "mysite.com";
$headers = "From: $from_name<$from_email>\n";
$headers .= "Reply-To: <$email>\n";
$headers .= "Return-Path: <$from_email>\n";
$body = "The following information was just posted \n";
unset($HTTP_POST_VARS['required_fields']);
reset($HTTP_POST_VARS);
while(list($key, $value) = each($HTTP_POST_VARS)) {
if(!empty($value)) {
$body .= proper_form($key) . ": " . stripslashes($value) ."\n";
}
}
$body .= "\nThis is an automated message, please do not respond.";
mail($to,$subject,$body,$headers);
$URL = $WEB_ROOT . "/customer/thanks.php?form=" . $FILE;
server_redirector($URL);
} else {
while(list($key, $value) = each($HTTP_POST_VARS)) {
$rebound .= "&$key=" . urlencode(stripslashes($value));
}
$URL = $WEB_ROOT . "customer/survey.php?error=". urlencode(nl2br($valid->errors)) . $rebound;
server_redirector($URL);
die();
}
}
?>
regsiter_globals is not active in newer PHP versions.
So instead of using if(!isset($submit)) you have to use if(!isset($_GET['submit'])). And for posted values, you use $_POST['parameter'].
The __FILE__ constant is possibly not returning what you expect, or not what it previously did. from the docs
Since PHP 4.0.2, __ FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
So you may be getting an absolute path now, instead of a relative one.
Also check if your new installation allows for short_open_tags as explained in the docs
$HTTP_POST_VARS is deprecated. You will want to replace it with $_POST.
Alternatively, you can just add $HTTP_POST_VARS = $_POST; at the beginning of the script to avoid editing every line (Not really recommended).
I am using this code on my website (It is included in 404.php):
<?php
if (isset($_POST["op"]) && ($_POST["op"]=="send")) {
/******** START OF CONFIG SECTION *******/
$sendto = "myemail#gmail.com";
$subject = "Website Contact Enquiry";
/******** END OF CONFIG SECTION *******/
$message = $HTTP_POST_VARS['message'];
$headers = "From: $email\n";
$headers . "MIME-Version: 1.0\n"
. "Content-Transfer-Encoding: 7bit\n"
. "Content-type: text/html; charset = \"iso-8859-1\";\n\n";
$request = $_SERVER["REQUEST_URI"];
$self = $_SERVER["PHP_SELF"];
// Build the email body text
$emailcontent = "
-----------------------------------------------------------------------------
WEBSITE CONTACT ENQUIRY
-----------------------------------------------------------------------------
$request
$self
$message
";
if (!trim($message)) {
echo "<p>Please go back and type a Message</p><p><a href='javascript: history.go(-1)'>Click here to go back</a>.</p>";
}
// Sends out the email or will output the error message
elseif (mail($sendto, $subject, $emailcontent, $headers)) {
echo "<br><br><p><b>Thank You </b></p><p>We will be in touch as soon as possible.</p>";
}
}
else {
?>
And this is my form
<form action="http://www.ahornblume.ch/404.php" method="post">
<INPUT NAME="op" TYPE="hidden" VALUE="send">
<textarea name="message" cols="25" rows="4" placeholder="Wenn Sie eine Antwort wünschen, vergessen Sie bitte ihre email Adresse nicht."></textarea><br>
<input name="submit" type="submit" value="Send Message">
</form>
I am redirecting all 404 errors in my htaccess file like so:
ErrorDocument 404 /404.php
If I go to ahornblume.ch/404.php everything works and I can use the form to send emails.
But, if I go to ahornblume.ch/something the contact form doesn't work.
Your form does not have an action specified so it will submit to the URL that was used to display the form.
In case of ahornblume.ch/404.php it will submit to 404.php and it will work.
In case of ahornblume.ch/something it will result in a 404 error that will just display the 404.php page, but the form post will have been discarded.
To fix, specify an action with an absolute path/404.php
If you are including the contact form you might want to check the links first! It's probably just a wrong link. Avoid using stuff like action="../php/contact_form.php". With include/require/require_once, I always opt for $root = realpath($_SERVER["DOCUMENT_ROOT"]);require_once($root."/path/to/file/myfile.php");
So if the contact form is displaying but when you click submit nothing happens, the cause is most likely that the server cannot locate the "action" php file. You have two options:
1: instead of <form action="../your_file.php" use the full link. <form action="http://yoursite.com/path/to/your/file/yourfile.php"
2. Always make use of root for include, require and require_once and use an additional variable like $url=http://www.yoursite.com and whenever you need to insert links, use PHP, as follows:This is a link. It may look like a lot of trouble, but it will make your life much much easier in some cases. Switching to https or any kind of global modification will mean that you only have to change 1 variable, $url. Every single link will then automatically be updated. If you know for a fact that you don't need this, option number 1 will do.
Are you using javascript/ajax/jquery for the form submission? If you are send a post request like that, check the link in the .js file or wherever you have the code and use an absolute path for the post request.