Php 未定义的索引错误

Php 未定义的索引错误,php,Php,我知道这个问题已经被问了很多次,所以我遵循了回答,但错误仍然存在 以下是错误: 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 $fiel

我知道这个问题已经被问了很多次,所以我遵循了回答,但错误仍然存在

以下是错误:

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>        

有什么建议吗?谢谢大家!

访问一个不存在的变量或索引会在PHP中触发一个通知,当您的错误报告在E_ALL上时,该通知是可见的

在本例中,将输入变量放入另一个变量的问题在于,您永远无法确定它们是否存在,因为它们依赖于用户输入。解决方案是检查它们是否存在,例如使用isset

发出通知:

<?php
$field_cf_name = $_POST['cf_name'];
<?php
if (isset($_POST['cf_name'])) {
    $field_cf_name = $_POST['cf_name'];
}
未发出通知:

<?php
$field_cf_name = $_POST['cf_name'];
<?php
if (isset($_POST['cf_name'])) {
    $field_cf_name = $_POST['cf_name'];
}

您还没有修复所有问题:

 $field_cf_name = $_POST['cf_name']; $field_cf_email =
                  ^^^^^^^^^^^^^^^^^^---- no isset() checks on this

这正是发出警告的代码行。

您可以发布表单代码吗?如何使用此脚本?是在另一页的表单中用作操作的页面,还是此表单嵌入此页面?抱歉,我现在发布我的表单代码!这段代码很难读懂。请格式化它。在复制到SO之前,用空格替换任何制表符。或者您可以使用$field\u cf\u name=@$\u POST['cf\u name']来取消警告$如果$\u POST['cf\u name']不存在,则字段\u cf\u name将为空。通知不存在,因为创建PHP的人想惹恼您。他们在那里有很好的理由。您应该非常清楚一个事实,即变量可能不存在。非常感谢!排序!:只是一个简单的问题-如果我想回应,消息已发送!当用户单击submit时,这是否有效-