- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 ajax 和 CodeIgniter 实现一个评论框。
我想要一个脚本,其中登录用户评论,Ajax 将 user_id
和 post
发送到 Controller ,评论被添加到 MySQL 数据库和然后刷新评论列表。下面的代码只是在执行我希望使用 ajax 完成的功能。这是我的部分 View scenery.php
<?php
$scenery_id=$scenery1['scenery_id'];
echo form_open(('display_scenery/add_comment/'.$scenery_id)); ?>
<div class="input-group" ><!-- input group starts-->
<input type="text" class="form-control" id ="Comment" name ="Comment" placeholder="Comment on Scenery..." maxlength="300" size= "70" required>
<input type ="hidden" name= "Scenery_id" value= " <?php echo $scenery_id?>" />
<button type="submit" id = "submit"class="btn btn-info regibutton" >Post</button>
</div>
</form>
<hr/>
<div id = "comment-box">
<?php
if ($comment==NULL){
//if no scenery comment echo disclaimer
echo " <ul style = ' margin-left: 0px;padding-left: 0px;'> <li style = 'list-style: none; background-color: #fff; padding : 5px 5px 5px 10px; margin: 5px 5px 5px 5px'>";
echo " No scenery Comments";
echo "</li>
</ul>";
} else{
foreach ($comment as $row){
// if the comments are availabe echo them
echo " <ul style = ' margin-left: 0px;padding-left: 0px;'> <li style = 'list-style: none; background-color: #fff; padding : 10px 5px 5px 10px; margin: 5px 5px 5px 5px'>";
echo $row->Comment;
echo "<br/>";
echo "<p style='font-size: 11px; color:#333; padding-top: 5px;'>".date(" D d M Y - H:i:s ",strtotime($row->Date_posted))."By - ". $row->Username. " </p>";
echo $row->Date_added;
echo "</li>
</ul>";
}
}
}
?>
</div>
</div>
<br>
<br>
这是我的 Controller display_scenery.php
public function add_comment(){
$this->load->library('form_validation');
$session_data = $this->session->userdata('logged_in');
$User_id= $session_data['User_id'];
$scenery_id = $_POST['Scenery_id'];
$Comment=$_POST['Comment'];
$this->form_validation->set_rules('Comment', 'Comment', 'trim|required');
if($this->form_validation->run() == FALSE)
{
///$error= form_error('Comment');
$this-> session->set_flashdata('error', form_error('Comment'));
redirect ('scenery', 'refresh');
}
else {
//loads the model image_display then redirects to scenery page
$this-> image_display->add_comment( $scenery_id, $Comment,$User);
redirect ('scenery', 'refresh');
}
}
最佳答案
如果用户已登录,假设您正在将用户的数据存储在 session 变量中,因此您可以从他们的 session 数据中获取用户的 ID(实际上,您不应该从前端接受此信息,因为用户可以很容易地改变他们的 UID 的值来假装是其他人使用 Chrome 开发者工具或 Firebug)。他们登录后,您可以使用 jQuery 的 $.ajax 方法提交 AJAX 查询:
$.ajax({
// Submitting to Controller_name->submit_comment() on your site
'url': 'https://www.example.com/controller_name/submit_comment',
// Submit as a POST request
'type': 'POST',
// comment_text should be a variable containing
// the text of the comment
'data':
{
'comment_text': comment_text
},
// Controller method will return a JSON object
'dataType': 'json',
// Success method for response
'success': function (response)
{
// If success, display comment. This is all coming
// from the PHP code below.
if (response.status === true)
{
$('#comments').append('<div class="comment"><span class="user-id">' + response.comment.user_id + '</span> at <span class="timestamp">' + response.comment.timestamp + '</span></br />' + response.comment.text + '</div>');
}
// Else failure, display error
else
{
$('#comments').append('<div class="comment-error">' + response.error + '</div>');
}
}
});
在 CodeIgniter 后端,您将向名为 submit_comment 的 Controller_name Controller 添加一个方法:
public function submit_comment()
{
$response = array(
'status' => FALSE,
);
// Get the various pieces of data that are needed
// to store the comment - text, user ID, timestamp of comment.
$comment = array(
// Get user ID from session data
'user_id' => $this->session->userdata('user_id'),
// Get comment from POST data
'text' => $this->input->post('comment'),
// Set timestamp to current epoch time
'timestamp' => time()
);
// Do your validation and database query to save the comment here.
// Store the result in a variable called $comment_posted
// (TRUE for success, FALSE for failure).
// If data validation/database query succeed:
if ($comment_posted)
{
// Set status to true to indicate success.
$response['status'] = TRUE;
// Rewrite timestamp to date/time string since humans don't speak epoch.
$comment['timestamp'] = date('m/d/Y g:ia', $comment['timestamp']);
// Include comment object in body of response.
$response['comment'] = $comment;
}
// Else the validation/query failed, return an error message.
else
{
// Return a useful error message.
$response['error'] = 'Some useful error message here.';
}
// Print the response array as a JSON-encoded string,
// which will be consumed by the success method in
// the Javascript code above.
die(json_encode($response));
}
关于php - 使用 Ajax 和 Codeigniter 的评论系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35218523/
我正在使用评论系统,现在,我想重写 url 评论的片段并附加一个符号#,我想将页面部分移动到评论列表,正好是最后一个评论用户,带有 username 我在发表评论时使用 next 重定向用户: {
这个问题在这里已经有了答案: "Rate This App"-link in Google Play store app on the phone (21 个回答) 关闭2年前。 有没有一种方法可以要
长期潜伏者第一次海报... 我们正在使用 Facebook 的 API 将其集成到我们的网络应用程序中,并且我们能够通过 {page-id}/ratings 部分中的 {open_graph_stor
我正在尝试让 Visual Studio 2012 自动格式化我的评论 block ,就像它对我的 C# block 所做的那样。我希望我的评论看起来像这样: /* * Here is my C#
在 MySQl 中创建表时对每个字段进行注释是否会影响性能?我正在处理一个包含 1000 多个表的数据库,几乎每个表中的每个字段都有注释。我只是想知道这是否会以任何方式影响 MySQL 的性能? 最佳
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
这个问题在这里已经有了答案: SQL select only rows with max value on a column [duplicate] (27 个答案) 关闭 5 年前。 我这里有 2
如何在评论中正确编写 --> 或 -->? 我正在维护一个包含许多小程序代码条目的大型 html 文件。说: a --> b. 我在 HTML 中将其编码为 -->: a --> b. 但是,我
这是一个简单的问题。有没有办法允许用户直接在我的应用程序中输入评论和/或评级,并将这些数据发回 Android Market?如果是这样,如果我使用 EditText View 允许用户输入,代码会是
注释是否表示代码中带有//或/* */的注释? 最佳答案 不,注释不是评论。使用语法 @Annotation 将注释添加到字段、类或方法。最著名的注解之一是@Override,用于表示方法正在覆盖父类
我有一个包含两个模型的 Django 应用程序:第一个是 django.contrib.auth.User,第二个是我创建的 Product。 我会为每个产品添加评论,因此每个注册用户都可以为每个产品
有没有办法评论多行......其中已经有一些评论? 即 ... Hello world! Multi-line comment end --> 看来连
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: obj.nil? vs. obj == nil 现在通过 ruby koans 工作,发现这个评论嵌入在
这是一个基本问题 .gemrc 文件中是否允许注释? 如果是,你会怎么做? 我这里查了没用 docs.rubygems.org/read/chapter/11 最佳答案 文档说:The config
有没有办法在 SASS 中添加 sass-only 注释?你知道,所以输出 .css 文件没有那些注释 例如, /* global variables */ $mainColor: #666; /*
我想搜索在任何媒体上发布的评论中的任何特定关键字或几个关键字的组合。我的要求是在 API 的帮助下获取包含该关键字的评论。我浏览了 Instagram API 的文档,发现只能通过哈希标签进行搜索,而
在 WordPress 中,您可以在页面加载之前执行以下操作来编辑文章的内容: add_filter('the_content', 'edit_content'); function edit_con
在指示要合并的内容时, checkin 合并的最佳方法是什么?我已经说过 10 个变更集我正在从我的主分支合并到一个发布分支。每一个都包含我在 checkin 主分支时写的详细注释。现在,当我合并时,
我知道如何查询常规网站的社交参与度计数。可以使用Facebook图形浏览器(https://developers.facebook.com/tools/explorer/)或throug api轻松实
我正在尝试从 YouTube 视频中获得特定评论。例如,我想从 YouTube 视频的第 34 条评论中获取详细信息。有谁知道在不阅读所有评论列表的情况下我该怎么做? 或者,如果没有任何解决方案可以仅
我是一名优秀的程序员,十分优秀!