0

我在这里有一个用户输入表单。这是它的代码!

<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;
});



});
4

2 回答 2

1

可能是 $_POST['bodytext'] 未设置导致 php 运行时错误的情况。修复错误后替换

$content = $_POST['bodytext'];

$content = isset($_POST['bodytext']) ? $_POST['bodytext'] : '';
于 2013-04-14T19:53:08.933 回答
0

你的问题是这一行:

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;

需要注意的是,当您以这种方式提交变量时,您也需要在此处正确编码变量。

于 2013-04-14T20:24:19.850 回答