Hello everybody i'm using Codeigniter 3.0.3
And in my project there's a wall to share posts by json jquery data i saw many sites when someone publish new post it's show in the same page without refresh the page i make it by if the data post by jquery success i used window.location.reload(true);
i need jquery to add one more new foreach loop when data posted successfully i searched more and more but i didn't helpful answer
Here's my Code
Controller :
function AddPost()
{
$this->load->helper('string');
$this->load->helper('date');
$this->load->model('users_model');
$query = $this->users_model->addnewpost();
if($query == TRUE)
{
echo 'true';
}
else
{
echo 'false';
}
}
Model :
function addnewpost () {
$data = array (
'pid' => random_string('numeric', 16),
'post_author' => $this->session->userdata('username'),
'post_date' => gmt_to_local(),
'post_text' => $this->input->post('post_text'),
'author_id' => $this->session->userdata('id'),
'author_ip' => $this->input->ip_address()
);
$query = $this->db->insert('users_posts', $data);
if($query == true) {
$this->db->set('u_posts', 'u_posts+1', FALSE);
$this->db->where('uid', $this->session->userdata('id'));
$this->db->update('confirmed_users');
return true;
} else {
return false;
}
}
View :
<!-- post status panel -->
<div class="panel panel-default">
<div class="panel-body">
<form name="addnewpostajax" class="addnewpostajax" method="post" role="form">
<label for="StatusText"><strong>Update status</strong></label>
<textarea class="form-control StatusText" name="StatusText"></textarea>
<hr class="myown">
<div class="btn-group">
<a class="btn btn-default btn-sm"><span class="fa fa-image fa-lg"></span></a>
<a class="btn btn-default btn-sm"><span class="fa fa-link fa-lg"></span></a>
<a class="btn btn-default btn-sm"><span class="fa fa-video-camera fa-lg"></span></a>
</div>
<div class="pull-right">
<button type="submit" name="postnewstatus" class="btn btn-default btn-sm">Post</button>
</div>
</form>
<div class="clear"></div>
</div>
</div>
<!-- / post status panel -->
<!-- status panel -->
<?php if($allposts): ?>
<?php foreach ($allposts as $post): ?>
<div class="panel panel-default" style="border-radius:0;">
<div class="panel-body">
<div class="col-md-12" style="padding: 0;">
<img class="img-rounded pull-left" src="<? echo base_url($post->image) ?>" width=30px height=30px>
<div style="margin-top: 5px;">
<a href="<? echo base_url('home?p=Profile&u='.$post->post_author) ?>"><small style="margin-left: 5px;"><?=$post->post_author?></small></a>
<span style="font-size: 75%;" class="text-muted pull-right"><span class="fa fa-clock-o"></span>
<?php $post_date = $post->post_date; $now = time(); $units = 1; ?>
<? echo timespan($post_date, $now, $units) ?>
ago</span>
</div>
</div>
<div class="clear"></div>
<hr class="myown">
<div class="col-md-12 text-muted"><small><?=$post->post_text?></small></div>
<div class="clear"></div>
<hr class="myown">
<div class="btn-group">
<?php if($this->users_model->liked($post->pid)): ?>
<a class="btn btn-default btn-sm disabled"><span class="fa fa-check"></span> Liked</a>
<?php else: ?>
<a class="btn btn-default btn-sm like-btn" post-id="<?=$post->pid?>"><span data-toggle-tooltip="tooltip" data-placement="top" title="Like" class="fa fa-thumbs-up fa-lg"></span></a>
<?php endif; ?>
<a class="btn btn-default btn-sm" data-toggle="collapse" data-target="#<?=$post->pid?>Comment"><span data-toggle-tooltip="tooltip" data-placement="top" title="Comment" class="fa fa-ment fa-lg"></span></a>
<a class="btn btn-default btn-sm"><span data-toggle-tooltip="tooltip" data-placement="top" title="Share" class="fa fa-share fa-lg"></span></a>
</div>
<div class="pull-right-custom text-muted"><small><?=$post->post_likes?> Like - <?=$post->post_ments?> Comment</small></div>
<div class="clear"></div>
<div id="<?=$post->pid?>Comment" class="collapse">
<br>
<form name="addmentajax" post-id="<?=$post->pid?>" class="form-horizontal addmentajax" role="form" method="POST">
<div class="input-group">
<input type="text" class="form-control input-sm ment-<?=$post->pid?>" post-id="<?=$post->pid?>" placeholder="Add Comment">
<span class="input-group-btn">
<button type="submit" class="btn btn-sm btn-default">Publish</button>
</span>
</div>
</form>
</div>
</div>
<?Php if (!empty($postments)):?>
<?php foreach ($postments as $ment): ?>
<?php if (!empty($ment->post_id == $post->pid)): ?>
<? echo '<img class="img-rounded pull-left" src="'.base_url($ment->image).'" width="35px" height="35px" style="margin:3px 5px 3px 5px;">'; ?>
<? echo '<div class="panel-footer">'; ?>
<? echo '<small class="text-muted"><a href="'.base_url('home?p=Profile&u=').$ment->username.'">'.$ment->username.'</a> '.$ment->ment_text.'</small>'; ?>
<? echo '<small style="margin-top: 2.5px;" class="pull-right text-muted">' . timespan($ment->ment_date, time(), 1) . '</small>'; ?>
<? echo '</div>'; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<!-- / status panel -->
Finally Here's my javascript code :
<script type="text/javascript">
$(document).ready(function() {
$('.addnewpostajax').submit(function() {
var post_text = $('.StatusText').val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url('home/AddPost'); ?>",
dataType: "json",
data: {post_text},
success: function(posted) {
if(posted == true) {
window.location.reload(true);
} else {
alert('Faild');
}
}
});
});
});
</script>
Thanks all
Hello everybody i'm using Codeigniter 3.0.3
And in my project there's a wall to share posts by json jquery data i saw many sites when someone publish new post it's show in the same page without refresh the page i make it by if the data post by jquery success i used window.location.reload(true);
i need jquery to add one more new foreach loop when data posted successfully i searched more and more but i didn't helpful answer
Here's my Code
Controller :
function AddPost()
{
$this->load->helper('string');
$this->load->helper('date');
$this->load->model('users_model');
$query = $this->users_model->addnewpost();
if($query == TRUE)
{
echo 'true';
}
else
{
echo 'false';
}
}
Model :
function addnewpost () {
$data = array (
'pid' => random_string('numeric', 16),
'post_author' => $this->session->userdata('username'),
'post_date' => gmt_to_local(),
'post_text' => $this->input->post('post_text'),
'author_id' => $this->session->userdata('id'),
'author_ip' => $this->input->ip_address()
);
$query = $this->db->insert('users_posts', $data);
if($query == true) {
$this->db->set('u_posts', 'u_posts+1', FALSE);
$this->db->where('uid', $this->session->userdata('id'));
$this->db->update('confirmed_users');
return true;
} else {
return false;
}
}
View :
<!-- post status panel -->
<div class="panel panel-default">
<div class="panel-body">
<form name="addnewpostajax" class="addnewpostajax" method="post" role="form">
<label for="StatusText"><strong>Update status</strong></label>
<textarea class="form-control StatusText" name="StatusText"></textarea>
<hr class="myown">
<div class="btn-group">
<a class="btn btn-default btn-sm"><span class="fa fa-image fa-lg"></span></a>
<a class="btn btn-default btn-sm"><span class="fa fa-link fa-lg"></span></a>
<a class="btn btn-default btn-sm"><span class="fa fa-video-camera fa-lg"></span></a>
</div>
<div class="pull-right">
<button type="submit" name="postnewstatus" class="btn btn-default btn-sm">Post</button>
</div>
</form>
<div class="clear"></div>
</div>
</div>
<!-- / post status panel -->
<!-- status panel -->
<?php if($allposts): ?>
<?php foreach ($allposts as $post): ?>
<div class="panel panel-default" style="border-radius:0;">
<div class="panel-body">
<div class="col-md-12" style="padding: 0;">
<img class="img-rounded pull-left" src="<? echo base_url($post->image) ?>" width=30px height=30px>
<div style="margin-top: 5px;">
<a href="<? echo base_url('home?p=Profile&u='.$post->post_author) ?>"><small style="margin-left: 5px;"><?=$post->post_author?></small></a>
<span style="font-size: 75%;" class="text-muted pull-right"><span class="fa fa-clock-o"></span>
<?php $post_date = $post->post_date; $now = time(); $units = 1; ?>
<? echo timespan($post_date, $now, $units) ?>
ago</span>
</div>
</div>
<div class="clear"></div>
<hr class="myown">
<div class="col-md-12 text-muted"><small><?=$post->post_text?></small></div>
<div class="clear"></div>
<hr class="myown">
<div class="btn-group">
<?php if($this->users_model->liked($post->pid)): ?>
<a class="btn btn-default btn-sm disabled"><span class="fa fa-check"></span> Liked</a>
<?php else: ?>
<a class="btn btn-default btn-sm like-btn" post-id="<?=$post->pid?>"><span data-toggle-tooltip="tooltip" data-placement="top" title="Like" class="fa fa-thumbs-up fa-lg"></span></a>
<?php endif; ?>
<a class="btn btn-default btn-sm" data-toggle="collapse" data-target="#<?=$post->pid?>Comment"><span data-toggle-tooltip="tooltip" data-placement="top" title="Comment" class="fa fa-ment fa-lg"></span></a>
<a class="btn btn-default btn-sm"><span data-toggle-tooltip="tooltip" data-placement="top" title="Share" class="fa fa-share fa-lg"></span></a>
</div>
<div class="pull-right-custom text-muted"><small><?=$post->post_likes?> Like - <?=$post->post_ments?> Comment</small></div>
<div class="clear"></div>
<div id="<?=$post->pid?>Comment" class="collapse">
<br>
<form name="addmentajax" post-id="<?=$post->pid?>" class="form-horizontal addmentajax" role="form" method="POST">
<div class="input-group">
<input type="text" class="form-control input-sm ment-<?=$post->pid?>" post-id="<?=$post->pid?>" placeholder="Add Comment">
<span class="input-group-btn">
<button type="submit" class="btn btn-sm btn-default">Publish</button>
</span>
</div>
</form>
</div>
</div>
<?Php if (!empty($postments)):?>
<?php foreach ($postments as $ment): ?>
<?php if (!empty($ment->post_id == $post->pid)): ?>
<? echo '<img class="img-rounded pull-left" src="'.base_url($ment->image).'" width="35px" height="35px" style="margin:3px 5px 3px 5px;">'; ?>
<? echo '<div class="panel-footer">'; ?>
<? echo '<small class="text-muted"><a href="'.base_url('home?p=Profile&u=').$ment->username.'">'.$ment->username.'</a> '.$ment->ment_text.'</small>'; ?>
<? echo '<small style="margin-top: 2.5px;" class="pull-right text-muted">' . timespan($ment->ment_date, time(), 1) . '</small>'; ?>
<? echo '</div>'; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<!-- / status panel -->
Finally Here's my javascript code :
<script type="text/javascript">
$(document).ready(function() {
$('.addnewpostajax').submit(function() {
var post_text = $('.StatusText').val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url('home/AddPost'); ?>",
dataType: "json",
data: {post_text},
success: function(posted) {
if(posted == true) {
window.location.reload(true);
} else {
alert('Faild');
}
}
});
});
});
</script>
Thanks all
Share Improve this question asked Dec 28, 2015 at 19:47 AhmedElsAhmedEls 7282 gold badges10 silver badges26 bronze badges 2- create a view with single post block and call it in AddPost, this will give you html in ajax which you can append in list. – Jaya Vishwakarma Commented Dec 28, 2015 at 19:56
- @JayaVishwakarma Help me with codes please i'm noob in js ^^ – AhmedEls Commented Dec 28, 2015 at 20:14
1 Answer
Reset to default 3Basically you need to append new post just added.
Create a view with Single Post Block which seems inside your foreach:
//single-post-block.php
<div class="panel panel-default post-block" style="border-radius:0;">
<div class="panel-body">
<div class="col-md-12" style="padding: 0;">
<img class="img-rounded pull-left" src="<? echo base_url($post->image) ?>" width=30px height=30px>
<div style="margin-top: 5px;">
<a href="<? echo base_url('home?p=Profile&u='.$post->post_author) ?>"><small style="margin-left: 5px;"><?=$post->post_author?></small></a>
<span style="font-size: 75%;" class="text-muted pull-right"><span class="fa fa-clock-o"></span>
<?php $post_date = $post->post_date; $now = time(); $units = 1; ?>
<? echo timespan($post_date, $now, $units) ?>
ago</span>
</div>
</div>
<div class="clear"></div>
<hr class="myown">
<div class="col-md-12 text-muted"><small><?=$post->post_text?></small></div>
<div class="clear"></div>
<hr class="myown">
<div class="btn-group">
<?php if($this->users_model->liked($post->pid)): ?>
<a class="btn btn-default btn-sm disabled"><span class="fa fa-check"></span> Liked</a>
<?php else: ?>
<a class="btn btn-default btn-sm like-btn" post-id="<?=$post->pid?>"><span data-toggle-tooltip="tooltip" data-placement="top" title="Like" class="fa fa-thumbs-up fa-lg"></span></a>
<?php endif; ?>
<a class="btn btn-default btn-sm" data-toggle="collapse" data-target="#<?=$post->pid?>Comment"><span data-toggle-tooltip="tooltip" data-placement="top" title="Comment" class="fa fa-ment fa-lg"></span></a>
<a class="btn btn-default btn-sm"><span data-toggle-tooltip="tooltip" data-placement="top" title="Share" class="fa fa-share fa-lg"></span></a>
</div>
<div class="pull-right-custom text-muted"><small><?=$post->post_likes?> Like - <?=$post->post_ments?> Comment</small></div>
<div class="clear"></div>
<div id="<?=$post->pid?>Comment" class="collapse">
<br>
<form name="addmentajax" post-id="<?=$post->pid?>" class="form-horizontal addmentajax" role="form" method="POST">
<div class="input-group">
<input type="text" class="form-control input-sm ment-<?=$post->pid?>" post-id="<?=$post->pid?>" placeholder="Add Comment">
<span class="input-group-btn">
<button type="submit" class="btn btn-sm btn-default">Publish</button>
</span>
</div>
</form>
</div>
</div>
<?Php if (!empty($postments)):?>
<?php foreach ($postments as $ment): ?>
<?php if (!empty($ment->post_id == $post->pid)): ?>
<? echo '<img class="img-rounded pull-left" src="'.base_url($ment->image).'" width="35px" height="35px" style="margin:3px 5px 3px 5px;">'; ?>
<? echo '<div class="panel-footer">'; ?>
<? echo '<small class="text-muted"><a href="'.base_url('home?p=Profile&u=').$ment->username.'">'.$ment->username.'</a> '.$ment->ment_text.'</small>'; ?>
<? echo '<small style="margin-top: 2.5px;" class="pull-right text-muted">' . timespan($ment->ment_date, time(), 1) . '</small>'; ?>
<? echo '</div>'; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
</div>
You can render view through ajax from controller:
function AddPost()
{
$this->load->helper('string');
$this->load->helper('date');
$this->load->model('users_model');
$query = $this->users_model->addnewpost();
// here you need to get post details to pass in view
$data = $this->users_model->postDetails();
if($query == TRUE)
{
echo $this->load->view('signle-post-block',$data,TRUE);
}
else
{
echo 'false';
}
}
Your javascript function should look like:
<script type="text/javascript">
$(document).ready(function() {
$('.addnewpostajax').submit(function() {
var post_text = $('.StatusText').val();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url('home/AddPost'); ?>",
dataType: "html",
data: {post_text},
success: function(posted) {
if(posted != false) {
$('.post-block').last().after(posted);
} else {
alert('Faild');
}
}
});
e.preventDefault();
});
});
</script>
I have just copied your code to make it what you want with less efforts that you can change according to you.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745458882a4628615.html
评论列表(0条)