我在這裏有一個用戶輸入表單。這是它的代碼!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;
});
});
你知道textarea HTML中'=>'的語法錯誤嗎? –
這是一個錯字,它不能解決任何問題! –
在提供的PHP塊中執行print_r $ _POST,並將輸出發送給我。 – macintosh264