我使用电子邮件重定向创建了简单的联系表单, 但是当我刷新页面时,它总是在页面顶部显示几个通知:
注意:未定义的索引:......在线...
提交表格工作正常,我怎么能摆脱这个通知?
这是代码:
<?php
/* ========================================================
1. | FORM VALIDATION
=========================================================== */
//response generation function
$response = "";
//function to generate response
function contact_form_response($type, $message) {
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
/* ========================================================
2. | Errors
=========================================================== */
//response messages
$not_human = "Not-Human - wrong Number";
$missing_content = "Missing content";
$email_invalid = "Email - wrong adress";
$message_unsent = "Message Unsent - Try again";
$message_sent = "Thanks for Contact!" . "</br>" ."See you soon :)";
//Min/Max messages
$to_short_name = "Name: To short";
$to_long_name = "Name: To Long";
$to_short_mess = "Message: To short";
$to_long_mess = "Message: To Long";
$no_number = "Name/Message: Error - Only numbers, huh?";
$no_letter = "Only Numbers";
$no_about = "Select Subject";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$about = stripslashes($_POST['about']);
$message = $_POST['message_text'];
$human = $_POST['message_human'];
/* ========================================================
3. | E-MAIL Adress
=========================================================== */
$to ='your@email.com';
// Change the Email Addresses below to email Id where you want to get your emails.
// Reply Email Address where you want to set reply to email ID
$uploadpath='/scripts/test/';
$save_path ='http://'.$_SERVER['SERVER_NAME'].$uploadpath; // do not change this
// Change E-mail Adress, depend on chosen Subject from Dropdown List
switch ($about) {
case "Test": // Change to Value from Dropdown List
$to='test@test.com'; // change to different e-mail
break;
}// end switch
/* ========================================================
4. | SEND E-MAIL
=========================================================== */
$subject = "Page: ".get_bloginfo('name') ." | Author: " . $_POST['message_name'] .
" | Subject: " . $_POST['about'];
$headers="From: $email <br>";
$headers.="Name: $name <br>";
$headers.="Email: $email <br>";
$headers.="About: $about <br>";
$headers.="Message: $message <br>";
if(!$human == 0){
if($human != 6) contact_form_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
contact_form_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
contact_form_response("error", $missing_content);
}
// Checks About Field
if(!$about){
contact_form_response("error", $no_about );
}
// To Short Name
else if(strlen($name) < 6){
contact_form_response("error", $to_short_name );
} // To Long Name
else if(strlen($name) > 50) {
contact_form_response("error", $to_long_name );
}
// Numeric Name
else if(is_numeric($name) || (is_numeric($message)) ) {
contact_form_response("error", $no_number );
}
// To Short Message
else if(strlen($message) < 10){
contact_form_response("error", $to_short_mess );
} // To Long Message
else if(strlen($message) > 2000) {
contact_form_response("error", $to_long_mess );
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($message), $headers);
if($sent) contact_form_response("success", $message_sent); //message sent!
else contact_form_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) contact_form_response("error", $missing_content);
?>
<?php
/*
* Template Name: Contact Us
*
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<form id="contact-form"action="<?php the_permalink(); ?>" method="post">
<h2>Contact Form</h2>
<?php echo $response; ?>
<label for="name">Name:
<input type="text" placeholder="John Smith" name="message_name"
value="
<?php if(isset($_POST['message_name'])) echo $_POST['message_name'];?>">
</label>
<label for="message_email">Email:
<input type="text" placeholder="your@email.com" name="message_email"
value="
<?php if(isset($_POST['message_email'])) echo $_POST['message_email'];?>"></label>
<select name="about" class="about">
<option value="">▲ Choose Subject ▼</option>
<option value="Test">Test</option>
</select>
<label for="message_text">Message:
<textarea type="text" name="message_text">
<?php if(isset($_POST['message_text'])) echo $_POST['message_text'];?></textarea></label>
<label for="message_human">Are you Human?
<input type="text" placeholder="Write Number" name="message_human">3 + 3 = ?</label>
<input type="hidden" name="submitted" value="1">
<input type="submit" value="Send">
</form>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>