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.
Related
UPDATE: sorry, thanks for the solutions offered... not really clear how to implement, I think ive not been too clear... if I can understand how to implement the solutions thatd be neat...
Ive also successfully managed to get code working to create a new database table and insert test data but ommited that so its not so complex....
I REALLY want to be able to...
display some content that I can put in success.php (or else where) when the message is sent successfully
that content would say Message Sent - YAY! and then Id like to be able to add, exisiting wp content - some services or products you might be interested in and display them on that same success result page after the message is sent...
maybe there is a better way to redirect users after the message is sent...
then....I also need to save the form data (not yet done) to the new table that I have created (got the table created via plugin), and then display a table of all form submission records in admin panel (not done)
I replaced the content of my main.php file (the plugin's main php file in plugin-name root.
<?php
/** template info etc...
**/
// Find all .php files in the includes dir of my plug in folder.
foreach ( glob( plugin_dir_path( __FILE__ ) . "includes/*.php" ) as $file ) {
include_once $file;
}
?>
and all my files except the main one (which is in the plugin root) are in plugins/plugin-name/includes and are being found - YAY
so my includes/webform.php displays the form nicely it sends an email but I can't get this error or success message via success.php or error.php thing to work.
includes/success.php now looks like this... as per DK's suggestion
<?Php
$Errors = implode(' ', $_SESSION['errMsg']);
echo $Errors; ?>
includes/webform.php now looks like this...
<?php
function d6s_opp_html_form() {
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
//Form action page is the current url. the form is called by a shortcode that will run functions that are written within this plugin's files, they can be in different files in different folders within the plug in because we have told the plug in to load them in the main plug in file.php horray thay is working..
// other form fields removed to shorten this stakoverflow post
echo '<p>';
echo 'Your Name* <br />';
echo '<input type="text" name="d6s-opp-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["d6s-opp-name"] ) ? esc_attr( $_POST["d6s-opp-name"] ) : '' ) . '" size="40" placeholder="First & Last name" required />';
// Now using required - is that better than having to check if not empty in success or fail bit????
echo '<p><input type="submit" name="d6s-submitted" value="Send"/></p>';
echo '</form>';
}
//Short code function is here and works GREAT
Then below that in the same file this is the function i figured DK meant that I should put the first part of his solution 1
I Guess this is where I have it wrong still
function deliver_opp_mail() {
// if the submit button is clicked, send the email
if ( isset( $_POST['d6s-submitted'] ) ) {
//sanitise form values so that form data is readable... eg/ if users enter code/script or formatting symbols, it is not missinterpretted as code and is seen as all text.
$name = sanitize_text_field( $_POST["d6s-opp-name"] );
$email = sanitize_email( $_POST["d6s-opp-email"] );
$messagesubject = sanitize_text_field( $_POST["d6s-opp-subject"] );
$messagecontent = esc_textarea( $_POST["d6s-opp-message"] );
$phone = ( $_POST["d6s-opp-phone"] );
// Would like to consider calling form values via global Variables.
$subject = "NEW OPPURTUNITY: $messagesubject";
$d6sdir = plugin_dir_path( __FILE__ );
//Create the Email Body Message using free text and data from the form.
$message = "New Message From: $name \n MESSAGE: $messagecontent \n Return Email: $email \n Return Phone $phone ";
// get the blog administrator's email address, form data is emailed to this email address.
$to = get_option( 'admin_email' );
// Look into setting a to: Email address in WP Admin Console.
$headers = "From: $name <$email>" . "\r\n";
if ( wp_mail( $to, $subject, $message, $headers ) ) {
//maybe this is in the wrong spot, or perhaps this is not the solution I need, but I have tried this in a few different places and can't get it to work..
$Msg = array(
"You have an error",
"Your mail sent succesfully"
);
$_SESSION['errMsg'] = $Msg;
//this take the user to www.mydomain.com/.....wp-content/plugins/my-plugin/includes/success.php - the file is there, but WP theme not found is displayed.
header("Location: $d6sdir/success.php");
exit;
}
}
}
?>
*Dont want to display a message above the form on success or error... future plans for workflow need to take users to a page with no form and other content after they hit submit.
*also using error reporting - seems like something happen or flashes up before the not found from the theme bit is displayed and no other error are reported...
<?Php error_reporting(E_ALL); ini_set('display_errors', 1);
************************** FROM INITAL POST....
Really keen to learn. First post, thanks for help
Purpose of plug in: Create a plugin that I can eventually build into a custom CRM tool for my small business and Learn to code.
*Why is this so not simple...
Solution 1 passing messages to success.php
$Msg = array(
"You have an error",
"Your mail sent succesfully"
);
$_SESSION['errMsg'] = $Msg;
success.php
$Errors = implode(' ', $_SESSION['errMsg']);
echo $Errors;
Solution 2 :
$_SESSION["errMsg"] = "Your mail sent succesfully";
if(isset($_SESSION['errMsg']) AND !empty($_SESSION['errMsg']) ):
echo "<div class='alert danger'>".$_SESSION['errMsg']."</div>";
endif;
Not found solution :
You can use $_SERVER['DOCUMENT_ROOT']; to find root D:/wamp/www
and then full url to page like so :
echo $_SERVER['DOCUMENT_ROOT']."/yourFolder/test.php";
output :D:/wamp/www/yourFolder/test.php
Note : will be nice if you use exit(); right after header("Location:");
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 have my PHP set up it works fine so far with my contact form (I'm a 'Newbie' in PHP).
I am using echo to display the status of the message (if it is sent or not). once the user clicks the send button, the status message shows in a new URL - the URL of the PHP file (somesite.com/forum.php)
My Question is can I have my status message displayed with in my html file the file where my forum is (Forum.html) basicly instead of hitting the send button and then the URL changing to forum.php can I have the URL remain the same (Forum.html) and just possibly but the status text in there (the html file).
Here is my code:
<?php
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$from = 'From: Datcroft Website';
$to = 'myemail#hotmail.co.uk';
$subject = 'Datcroft Site Message';
$body = "From: $name\n E-Mail: $email\n Message: $message\n";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<center><font color="lightgray"><p>Message Sent Successfully!</p> <p>I will get back in touch as soon as possible.</p></font></center>';
} else {
echo '<center><font color="lightgray"><p>Ah! Try again, please?</p></font></center>';
}
}
?>
The Message Status part of my code(the thing i want to display in the forum.html not a new url:
echo '<center><font color="lightgray"><p>Message Sent Successfully!</p> <p>I will get back in touch as soon as possible.</p></font></center>';
} else {
echo '<center><font color="lightgray"><p>Ah! Try again, please?</p></font></center>';
Thanks to anyone who can point my in the right direction !
Thank You In Advanced.
You should change your forum.html file to forum.php, then move your PHP code to the top of your new forum.php file, and submit your form to self:
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
And then place your if where you want to show the message.
Or you can redirect back from forum.php with a flash message (A variable saved in the session and that gets deleted once used one time) and display it on html if it's set
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.