我知道这个问题已经被问了很多,所以我跟着回答,但错误仍然存在。
这是错误:
Notice: Undefined index: cf_name in C:\wamp\www\Mentalist Magician\contact.php on line 3
Notice: Undefined index: cf_email in C:\wamp\www\Mentalist Magician\contact.php on line 4
等。
这是我的代码:
<?php
$field_cf_name = $_POST['cf_name']; $field_cf_email =
$_POST['cf_email']; $field_cf_message = $_POST['cf_message'];
$field_cf_client = $_POST['cf_client'];
if(isset($_POST['cf_name'])){ $field_cf_name = $_POST['cf_name']; }
if(isset($_POST['cf_email'])){ $field_cf_email = $_POST['cf_email']; }
if(isset($_POST['cf_message'])){ $field_cf_message =
$_POST['cf_message']; } if(isset($_POST['cf_client'])){
$field_cf_client = $_POST['cf_client']; }
if (empty($field_cf_name) && empty($field_cf_email) &&
empty($field_cf_cellphone) && empty($field_cf_message)) {
echo 'Please fill in all required fields';
return false; }
else{ //process the rest of the form }
if($field_cf_client != ''){
echo "Submission Sent - Thank you!"; return false; }
else{ //process the rest of the form }
$mail_to = 'info@test.com';
$subject = 'New Message From Client'; $headers = "From: " .
strip_tags($field_email) . "\r\n"; $headers .= "Reply-To: ".
strip_tags($field_email) . "\r\n"; $headers .= "MIME-Version:
1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$body_message = '<html><body style="font-family:calibri,sans-serif;
font-size:18px">'; $body_message .= '<h2 style="font-weight:600;
font-size:27px; border-bottom:solid 1px #CCC;
padding-bottom:10px;">New Message From Your Website</h2>';
$body_message .= '<p><strong style="color:#000; font-weight:600;
">Client Name:</strong> '.$field_cf_name."</p>\n"."\n"; $body_message
.= '<p><strong style="color:#000; font-weight:600; ">Email:</strong>
'.$field_cf_email."</p>\n"."\n"; $body_message .= '<p><strong
style="color:#000; font-weight:600; ">Message:</strong>
'.$field_cf_message."</p>\n"."\n";
$body_message .= '</body></html>';
$mail_status = mail($mail_to, $subject, $body_message, $headers); ?>
表格代码:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" data-validate="parsley">
<label>Name</label><span class="red">*</span><br/>
<input name="cf_name" type="text" data-required="true" class="form-text2" />
<br/><br/><label>Email Address</label><span class="red">*</span><br/>
<input name="cf_email" type="text" data-required="true" data-type="email" class="form-text2" />
<br/><br/><label>Message</label><span class="red">*</span><br/>
<textarea name="cf_message" data-required="true" cols="" rows="" class="form-msg"></textarea>
<!-- Client Website -->
<input id="client" class="taken" name="cf_client" type="text" />
<!-- END -->
<p><input name="Submit" class="submit2" value="Submit" type="submit" /> echo "<div id="submitmessage">Message Sent!</div>"</p> </form>
有什么建议吗?谢谢!
答案 0 :(得分:0)
访问不存在的变量或索引会在PHP中触发通知,当您的错误报告在E_ALL上时,该通知可见。
将输入变量(在本例中为POST)放在另一个变量中的问题是您永远不确定它们是否存在,因为它们依赖于用户输入。解决方案是检查它们是否存在,例如与isset
。
发出通知:
<?php
$field_cf_name = $_POST['cf_name'];
不发出通知:
<?php
if (isset($_POST['cf_name'])) {
$field_cf_name = $_POST['cf_name'];
}
答案 1 :(得分:0)
你还没有解决所有问题:
$field_cf_name = $_POST['cf_name']; $field_cf_email =
^^^^^^^^^^^^^^^^^^---- no isset() checks on this
这是在发出警告的代码的完整行。