2014-10-04 67 views
-1

我试图做电子邮件代码,但我有我无法解决的错误。这是我的代码。PHP未定义的错误,试图做电子邮件代码

错误:

Notice: Undefined index: action in line 5 

代码:

<?php 
$Datacomment = 'data.txt'; 
$Key = '[@]'; 
$Key2 = '/n'; 

if($_POST['action'] == 'postcomment'){ 
    $Name = str_replace(array($Key,$Key2),'',$_POST['name']); 
    $Comment = str_replace(array($Key,$Key2),'',$_POST['content']); 
    $Comment = $Name.$Key.$Comment.$Key2; 


    $Modwrite = 'a'; 
    if(!file_exists($Datacomment)){ 
     $Modwrite = 'w'; 
    } 
    $File = fopen($Datacomment,$Modwrite); 
    fwrite($File,$Comment); 
    fclose($File); 

    header("Location: index.php"); /*replace comment.php by your homepage*/ 

} 

/*load coment*/ 

if(!file_exists($Datacomment)){ 
    $Showcomment = '<div class="showcomment">No comment</div>'; 
} 
else{ 
    $Cm = file_get_contents($Datacomment); 
    $Array = explode($Key2,$Cm); 
    $Total = count($Array) -1; 
    $Showcomment = '<h3 style="margin-left:1%;">'.$Total.' comments</h3>'; 
    for($i = $Total-1;$i >= 0;$i--){ 
     $Arraycm = explode($Key,$Array[$i]); 
     $Name = $Arraycm[0]; 
     $Comment = $Arraycm[1]; 
     $Showcomment .= '<div class="showcomment"><h4>'.$Name.'</h4>'.$Comment.'</div>'; 
    } 

} 
?> 

<style type="text/css"> 
    .showcomment { width:96%; float:left; border:1px solid #ccc; padding:1%; margin:0 0 10px 1%; color:#333;} 
    .showcomment h4 { color:#03C; padding:0px; margin:0px;} 
    .comment {width:400px; height:100px; outline:none;} 
    .name { width:400px;outline:none} 
</style> 
<form style="margin-left:1%;" action="comment.php" method="post"> 
    <input type="hidden" name="action" value="postcomment" /> 
    <h3>Enter your name :</h3> 
    <input type="text" name="name" class="name" /> 
    <h3>Eenter comment :</h3> 
    <textarea name="content" class="comment"></textarea> 
    <p> 
    <input type="submit" name="submit" value="SEND COMMENT" /> 
    </p> 
</form> 

<?php echo $Showcomment;?> 
+0

你发布的代码如何相关电子邮件?我什么也没有看到。 – eis 2014-10-04 14:49:56

+0

错误消息告诉你,究竟是什么错误。也许看看'isset()'。 – Sirko 2014-10-04 14:56:40

+0

尝试'if(isset($ _ POST ['submit'])&&($ _POST ['action'] =='postcomment')){...}'将'action =“comment.php”'改为' action =“”'因为你正在同一页面内运行你的整个代码。 – 2014-10-04 14:59:37

回答

0

你进入一个comment.php首次验证

if(isset($_POST['action']) && $_POST['action'] == 'postcomment'){ 

时,按提交发送参数 “动作”