PHP 页面未保存表单和解析输出


PHP page not saving form and parsing output

我有一个PHP页面,它从表单中获取响应(我希望它能),然后将数据输入到表中。然后,我回应同一页面上表格的响应。我在表单页面上使用 ajax 发送表单值,并在 ajax 调用成功后将数据加载到div 中。 这一切都是在没有刷新的情况下完成的,但是没有发送任何信息,它只是刷新页面

我的PHP页面 -

<?php
$comment_name =  $_POST["name"];
$comment_body =  $_POST["comment"];
$film_no =  $_POST["hidden"];
echo $comment_name;
echo $comment_body;
echo $film_no; 

// Connects to your Database 
 mysql_connect("localhost", "****", "****") or die(mysql_error()); 
 mysql_select_db("ignitet1_CheckFilm") or die(mysql_error()); 
$query1 = "INSERT INTO film_comments (comments_id,film_no,name,comment) 
VALUES ('','$film_no', '$comment_name','$comment_body')";
$runquery1 = mysql_query($query1)or die(mysql_error()); 

$getComments = mysql_query("SELECT * FROM film_comments where film_no = '$film_no'") 
                 or die(mysql_error());     
                 while($info = mysql_fetch_array($getComments)) 
                 { 
                echo "<p>";
                echo $info['name']; 
                echo ":</p>";
                echo "<p>";
                echo $info['comment'];  
                echo "</p>";
                echo "<p>Comment posted on:";
                echo $info['timestamp'];    
                echo "</p>";
                echo "</div>";      
?>

我的表单和JavaScript代码

<form id="ContactForm2" onsubmit="return submitForm()" >        
<div>                                               <input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="body">Comment Body</label>                  
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="submit" id="comment" class="button" value="Submit" />
<div class="form_result"> </div>
</form> </div>
<script>
function submitForm() {
    $.ajax({type:'POST', url: 'comment.php', data:$('#ContactForm2').serialize(), success: function(response) {
        $('#ContactForm2').find('.form_result').html(response);
    }});

} 

我尝试的一切似乎都不起作用。 帮助将不胜感激! :)

用你的 php 试试这个,它对我来说工作正常。

<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
$(document).ready(function(){
    $('#sub').click(function(){
        $.ajax({type:'POST', url: 'comment.php', data:$('#ContactForm2').serialize(), success: function(response) {
        $('#ContactForm2').find('.form_result').html(response);
    }});
    });
})
    </script>
    <body>
        <div>
        <form id="ContactForm2"> >                                                       <input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="comment">Comment Body</label>                  
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="button" id="sub" class="button" value="Submit" />
<div class="form_result"> </div>
</form> </div>

    </body>
</html>