我目前正在为一个网站创建一个 PHP 邮件表单。该文件称为“contactus.php”。单击表单末尾的“提交”按钮后,我需要将页面重定向到“thankyou.php”。
目前它只带我到一个名为“contactus.php/contactus.php”的页面,除了消息“电子邮件发送成功”之外完全空白。
我在代码工作中看到“电子邮件发送成功”的位置,但我很困惑如何更改它,以及如何更改一般的表单操作,以便电子邮件仍会发送,但页面将被定向到我创建的页面“thankyou.php”。我试过用“'thankyou.php'”简单地替换表单操作,但是当它将页面定向到“thankyou.php”时,电子邮件不再发送。
这是页面顶部的PHP:
<?php
require_once("./include/fgcontactform.php");
$formproc = new FGContactForm();
// config
$emailAddresses = array(
''=>'',
'Service Department'=>'fakeemail1.com',
'Sales Department'=>'fakeemail2.com',
'Parts Department'=>'fakeemail3.com',
'Customer Service Department'=>'fakeemail4.com',
'Bids Department'=>'fakeemail5.com'
// etc etc
);
$emailSubject = 'Submitted from Online Form';
// If we are dealing with a form submission, send an email
if (isset($_POST['name'])) {
// Check the email selected is valid
if (!isset($emailAddresses[$_POST['destemail']])) {
exit("Sorry, you have selected an invalid email option.");
}
// Create the body of the email message
$emailBody = "Dear {$_POST['destemail']}, \n\n {$_POST['message']} \n\n
From: {$_POST['name']} \n Company: {$_POST['company']} \n
Phone Number: {$_POST['phone']} \n E-mail: {$_POST['email']}
\n Preferred method of contact: {$_POST['method']} \n\n Submitted
from Online 'Contact Us' Form";
// Send the email and report the result
if (mail($emailAddresses[$_POST['destemail']],$emailSubject,$emailBody,"From:
{$_POST['email']}")) {exit("Email sent successfully.");
} else {exit("Email sending failed");
}
}
// Output the html form
?>
这是 PHP 的形式:
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id='contact_form_errorloc' class='err'></div>
<!-- Form Code Start -->
<form id='contactus' action='<?php echo $formproc->GetSelfScript(); echo
htmlentities($_SERVER['PHP_SELF']); ?>' method='post' accept-charset='UTF-8'>
<fieldset >
<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php
echo $formproc->GetFormIDInputValue(); ?>'/>
<div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>
<div class='container'>
<label for='name' >Your Full Name*: </label><br/>
<input type='text' name='name' id='name' value='<?php echo $formproc-
>SafeDisplay('name') ?>' maxlength="50" /><br/>
<span id='contactus_name_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='email' >Email Address*:</label><br/>
<input type='text' name='email' id='email' value='<?php echo $formproc-
>SafeDisplay('email') ?>' maxlength="50" /><br/>
<span id='contactus_email_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='phone' >Phone*:</label><br/>
<input type='text' name='phone' id='phone' value='<?php echo $formproc-
>SafeDisplay('phone') ?>' maxlength="50" /><br/>
<span id='contactus_phone_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='company' >Company Name*:</label><br/>
<input type='text' name='company' id='company' value='<?php echo $formproc-
>SafeDisplay('company') ?>' maxlength="50" /><br/>
<span id='contactus_company_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='message' >Message*:</label><br/>
<textarea rows="10" cols="50" name='message' id='message'><?php echo $formproc-
>SafeDisplay('message') ?></textarea>
<span id='contactus_message_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='method' >
How would you prefer we contact you?*</label></br>
<select name="method" id="method"><?php echo $formproc->SafeDisplay('method') ?>
<option value=""></option>
<option value="Phone" name="phone">Phone</option>
<option value="E-mail" name="email">E-Mail</option>
</select></br>
<span id='contactus_method_errorloc' class='error'></span>
</div>
<div class='container'>
<label for='destemail' > Which department are you trying to reach?*</label></br>
<select name="destemail" id="destemail">
<?php foreach ($emailAddresses as $name => $email) { ?>
<option value="<?php echo htmlspecialchars($name); ?>"><?php echo
htmlspecialchars($name) ; ?></option>
<?php } ?></select></br>
<span id='contactus_destemail_errorloc' class='error'></span>
</div>
<input type="submit" />
</fieldset>
</form>
非常感谢您的帮助!另外,我是一个相对较新的编码员,所以尽可能简单地说明一切会很有帮助。谢谢!