0

我有这个 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);
    }});


} 

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

4

1 回答 1

0

用你的 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>
于 2012-05-02T19:40:44.297 回答