如何加载内容而不重新加载? [英] How to load content without reloading?
本文介绍了如何加载内容而不重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好。如标题所示。我想写一个这样的'系统'。有人知道我该怎么做吗?我不知道如何使用Ajax。请有人帮帮我好的。
我想发送数据到addcomment.php
< 表格 方法 = 发布 操作 = addcomment.php >
< 输入 类型 = hidden 名称 = < span class =code-keyword> nick id = nick_ajax value = '。$ _ SESSION ['user']。' / >
< 输入 类型 = text id = content_ajax 占位符 = 撰写评论。 .. 名称 = content / >
< 输入 type = 隐藏 名称 = id id = id_content_ajax value = '。$ article ['id']。' / >
< input type = submit id = o_sl name = 已发送 value = 发送 / >
< /表格 >
脚本addcomment.php
<跨度class =code-pagedirective><?php
require_once(' config / config .PHP'跨度>);
if($ _ POST [' sent']){
$ nick = $ _POST [' nick'];
$ content = $ _POST [' content 跨度>];
$ postID = $ _POST [' id 跨度>];
$ sql_comment = $ PDO-> prepare(' 插入`comments`(`nick`,`content`,`postID`)值(:nick,:content,:id)');
$ sql_comment->执行(数组(' :nick' => $ nick ,' :content' => $ content,' :id' => $ postID));
header(' Location:home.php?success');
}
其他 {
header(' 位置:home.php?fail');
die();
}
?>
< b>我尝试了什么:
$( function (){
$( #o_sl)。click( function (){
var nick_ajax = $(' #nick_ajax')。val();
var content_ajax = $(< span class =code-string>' #content_ajax')。val();
var id_content_ajax = $(' #id_content_ajax')。val();
var dataString = ' & nick =' + nick_ajax + ' & content =' + content_ajax + ' & ; id =' + id_content_ajax;
$ .ajax({
type:' POST' ,
url:' add_comment.php',
data:dataString,
成功: function (数据){
if (data == ' 0')
alert(' 错误');
else
window 。 location = window 。 location ;
}
});
});
});
解决方案
_SESSION ['user']。' / >
< 输入 type = text id = content_ajax 占位符 = 撰写评论...... 名称 = 内容 / >
< 输入 < span class =code-attribute> type = hidden 名称 = id id = id_content_ajax value = '。
article ['id']。' < span class =code-attribute> / >
< input type = submit id = o_sl name = 发送 value = 发送 / >
< / form >
脚本addcomment.php < pre lang =PHP> <?php
require_once(' 配置/共nfig.php'跨度>);
if(
_POST [' sent' ]){
Hello. As in the title.I would like to write such a 'system'. Someone knows how I can do it? I don't know how I can use Ajax. Please somebody help me. Okay to the point.
I want send data to addcomment.php
<form method="post" action="addcomment.php">
<input type="hidden" name="nick" id="nick_ajax" value="'.$_SESSION['user'].'" />
<input type="text"id="content_ajax" placeholder="Write a comment..." name="content" />
<input type="hidden" name="id" id="id_content_ajax" value="'.$article['id'].'" />
<input type="submit" id="o_sl" name="sent" value="Send" />
</form>
Script addcomment.php
<?php
require_once('config/config.php');
if($_POST['sent']){
$nick = $_POST['nick'];
$content = $_POST['content'];
$postID = $_POST['id'];
$sql_comment = $PDO->prepare('insert into `comments` (`nick`,`content`,`postID`) values (:nick,:content,:id)');
$sql_comment->execute(array(':nick' => $nick, ':content' => $content, ':id' => $postID));
header('Location: home.php?success');
}
else {
header('Location: home.php?fail');
die();
}
?>
What I have tried:
$(function() {
$("#o_sl").click(function() {
var nick_ajax = $('#nick_ajax').val();
var content_ajax = $('#content_ajax').val();
var id_content_ajax = $('#id_content_ajax').val();
var dataString = '&nick='+ nick_ajax + '&content=' + content_ajax +'&id=' +id_content_ajax;
$.ajax({
type: 'POST',
url: 'add_comment.php',
data: dataString,
success: function(data) {
if( data == '0' )
alert( 'Error' );
else
window.location = window.location;
}
});
});
});
解决方案
_SESSION['user'].'" /> <input type="text"id="content_ajax" placeholder="Write a comment..." name="content" /> <input type="hidden" name="id" id="id_content_ajax" value="'.
article['id'].'" /> <input type="submit" id="o_sl" name="sent" value="Send" /> </form>
Script addcomment.php<?php require_once('config/config.php'); if(
_POST['sent']){
这篇关于如何加载内容而不重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文