我想在不加载php文件页面的情况下将表单数据发送到我的php文件。然后,处理后发送的数据必须在提要中淡入。谁能帮我?这是我的编码:
home.php:
<script>
$(document).ready(function(){
$('#content').load('feeds.php');
}, 1000
);
</script>
feeds.php:
<?php
//after connecting to my database and all
$sql = mysql_query("SELECT * FROM `posts` ORDER BY `post_id` DESC");
while($row = mysql_fetch_assoc($sql)) {
echo '<div id="post">' . $row['post'] . '</div><br />';
}
?>
以下是提交帖子的表格
<form id="post" action="post.php" method="POST">
<textarea name="post" width=300 height=150></textarea>
<input type="submit" value="Post" id="submit">
</form>
post.php:
//after connecting to the database
$post = $_POST['post'];
if($post != '') {
mysql_query("INSERT INTO `posts` (`post`) VALUE('$post')");
}
else {
echo 'Form is empty!';
}
#内容:
<div id="content"></div>
别无其他。
请修改此代码帮助我!谢谢,谢谢!
答案 0 :(得分:0)
最好的解决方案应该是JQuery和AJAX。使用AJAX将数据发送到任何页面,并根据您可以淡化数据的响应。
答案 1 :(得分:0)
以下是示例实现:
jQuery(function($){
$("#post").submit(function(){
$.ajax({
url: $(this).attr("action"),
data: $(this).serialize(),
success: function(){
alert("data sent");
}
});
return false;
});
答案 2 :(得分:0)
从
更改此内容<form id="post" action="post.php" method="POST">
<textarea name="post" width=300 height=150></textarea>
<input type="submit" value="Post" id="submit">
</form>
到
<form id="form1">
<textarea id="post" width=300 height=150></textarea>
<input type="button" value="Post" id="submit">
</form>
并将此脚本放在javascript区域:
$('#submit').click(function(event) {
event.preventDefault(); /* Stops default form submit on click */
var post = $('#post').val();
$.ajax({
url: "post.php?",
data: 'post=' + post,
type: "POST",
success: function(data){
$('#content').empty();
$('#content').load('feeds.php');
}
});
});
我没有测试它,但它应该是这样的。 希望这有帮助。