2013-04-14 178 views
0

我在這裏有一個用戶輸入表單。這是它的代碼!Php未定義索引

<form id="addCommentForm" method="POST" action="#"> 
    <div> 

     <input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>"/> 

     <label for="name">Your Name</label> 
     <input type="text" name="name" id="name" /> 

     <label for="email">Your Email</label> 
     <input type="text" name="email" id="email" /> 

     <label for="body">Comment Body</label> 
     <textarea name="bodytext" id="bodytext"></textarea> 

     <input type="submit" class="submit" value="Submit" /> 
    </div> 
</form> 

但提交我得到這個錯誤:

Notice: Undefined index: bodytext in /home/se212004/public_html/post-comment-mine.php on line 15 

其中提到的代碼塊:

if (isset($_POST)) 
{ 

    $username = $_POST['name']; 
    $email = $_POST['email']; 
    $content = $_POST['bodytext']; 
    $post_id=$_POST['post_id']; 

    $lowercase = strtolower($email); 
    $image = md5($lowercase); 

    //insert these values into the db as a new comment 
    //example using array syntax to insert values 
    $statement = "INSERT INTO comments (name, body, dt, email) VALUES (?, ?, now(), ?)"; 
    $sth = $db->prepare($statement); 
    $data = array($username, $content, $email); 
    $sth->execute($data); 
} 

誤差以$content = $_POST['bodytext'];指向。任何幫助解決這個將不勝感激。

這裏是JavaScript文件。

$(function() { 

$(".submit").click(function() { 

var name = $("#name").val(); 
var email = $("#email").val(); 
var comment = $("#bodytext").val(); 
var post_id = $("#post_id").val(); 
var dataString = '&name='+ name + '&email=' + email + '&comment=' + comment + '&post_id=' + post_id; 


if(name=='' || email=='' || comment=='') 
{ 
alert('Please Give Valid JOE Details'); 
} 
else 
{ 
$("#flash").show(); 
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" align="absmiddle">&  nbsp;<span class="loading">Loading Comment...</span>'); 
$.ajax({ 
    type: "POST", 
    url: "post-comment-mine.php", 
    data: dataString, 
    cache: false, 
    success: function(html){ 

    $("ol#update").append(html); 
    $("ol#update li:last").fadeIn("slow"); 
    document.getElementById('email').value=''; 
    document.getElementById('name').value=''; 
    document.getElementById('comment').value=''; 
$("#name").focus(); 

    $("#flash").hide(); 

} 
}); 
} 
return false; 
}); 



}); 
+0

你知道textarea HTML中'=>'的語法錯誤嗎? –

+1

這是一個錯字,它不能解決任何問題! –

+0

在提供的PHP塊中執行print_r $ _POST,並將輸出發送給我。 – macintosh264

回答

0

你的問題replacde

$content = $_POST['bodytext']; 

解決該錯誤後是這一行:

var dataString = '&name='+ name + '&email=' + email + '&comment=' + comment + '&post_id=' + post_id; 

comment值設置爲bodytext和PHP您檢查$_POST['bodytext']時,你應該是檢查$_POST['comment'],要麼,要麼更改行:

var dataString = '&name='+ name + '&email=' + email + '&bodytext=' + comment + '&post_id=' + post_id; 

需要注意的是,當您以這種方式提交變量時,您還需要對變量進行適當編碼。

+0

哇,我不能相信我錯過了!非常感謝!! –

1

可能是$ _POST ['bodytext']沒有設置導致php運行時錯誤。與

$content = isset($_POST['bodytext']) ? $_POST['bodytext'] : '';