How to change url and get value without refreshing page? - php

controller: Welcome.php
public function tutorial_overview()
{
$link = $_GET['link'];
echo $link;
}
view: index.php
<?php
foreach($heading as $sub)
{
echo "<li>
<a href='javascript:void(0)' class='links' id='".str_replace(" ",'-',$sub)."'>
<i class='fa fa-angle-double-right'></i> ".$sub."
</a>
</li>";
}
?>
<script>
$(document).ready(function(){
$(".links").click(function(){
link = this.id;
history.pushState(null, null, '<?php echo base_url(); ?>'+link);
$.ajax({
type:"GET",
url:"<?php echo base_url(); ?>tutorial_overview/"+link,
success:function(data){
alert(data);
}
});
});
});
</script>
In this code I have create a view file inside it I have define
<a href='javascript:void(0)' class='links' id='".str_replace(" ",'-',$sub)."'>
<i class='fa fa-angle-double-right'></i> ".$sub."
</a>
which is dynamic. Now I want to change the url link as well as change the content that means when I click on link url will change and content will also change without refreshing page. So, How can I do this ?Please help me.
Thank You

I think your jQuery code has the problem -
link = this.id;
Try to apply code below -
$(".links").click(function(){
var link = $(this).attr('id');
var newUrl = '<?php echo base_url(); ?>' + link;
history.pushState({}, null, newUrl);
$.ajax({
type:"GET",
url:"<?php echo base_url(); ?>tutorial_overview/"+link,
data: { link: link },
success:function(data){
alert(data);
}
});
});
What you need to do is - Call ajax, get data and then do other stuff. You may change URL via history pushState when need. Be aware that older browser isn't compatible.

Related

fill sortlist heart icon using ajax

i am using ajax to sort list the hotels when i click on heat icon which is coming from while query from database on that hear icon i called one function to set a session after set a session i want to fill heart icon without refreshing a page and again clicking on same heart icon i want to unset the particular id from session and show the heart icon as blank .
please provide me solution.
live url- https://www.hotelinkonkan.com
<a class="clickforact"><i class="fa fa-heart-o" id="boricon1" onclick="cart('<?php echo $id;?>')" ></i> </a>
The Function which is write on this icon click event for add and remove the is from session
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
total_cart_items:"totalitems"
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
});
function cart(id)
{
$('.clickforact').click(function(){
if($(this).find($("i")).hasClass('fa-heart-o'))
{
$(this).find($("i")).removeClass('fa-heart-o').addClass('fa-heart');
}
else if($(this).find($("i")).hasClass('fa-heart'))
{
$(this).find($("i")).removeClass('fa-heart').addClass('fa-heart-o');
}
});
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
iteam_id:id
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
}
function cart_remove(id)
{
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
iteam_id_remove:id
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
show_cart();
}
function show_cart()
{
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
showcart:"cart"
},
success:function(response) {
document.getElementById("mycart").innerHTML=response;
$("#mycart").slideToggle();
}
});
}
</script>
You are calling event multiple times so you are facing an issue
1)with javascript 'onclick' : onclick="cart('')"
2)with jquery 'click' : $('.clickforact').click(function(){}
You can achive your goal using jquery only.
with this:
//pass data-id with anchor tag
<a class="clickforact" data-id="<?php echo $id;?>"><i class="fa fa-heart-o" id="boricon1" ></i> </a>
Use only jquery event
$('.clickforact').click(function(){
if($(this).find($("i")).hasClass('fa-heart-o'))
{
$(this).find($("i")).removeClass('fa-heart-o').addClass('fa-heart');
}
else if($(this).find($("i")).hasClass('fa-heart'))
{
$(this).find($("i")).removeClass('fa-heart').addClass('fa-heart-o');
}
var id = $(this).data("id"); //<---retrive your id
$.ajax({
type:'post',
url:'./../include/store_items.php',
data:{
iteam_id:id
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
});

cant load controller function when clicking tag a on ajax in codeigniter

I have problem with my ajax in CodeIgniter, I cannot load function even I clicks on my anchor tag.
my view :
<a id="delete_article" href="#" style="margin-top:0px"><span class="label label-danger">Delete</span></a>
my ajax function :
<script type="text/javascript">
$(document).ready(function() {
$("#delete_article").click(function(event) {
event.preventDefault();
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/ui_controller/deletearticle/<?php echo $twid;?>/<?php echo $id_session; ?>",
success: function(res){
$('#list_article').load("<?php echo base_url(); ?>index.php/ui_controller/list_article_plain/<?php echo $uid; ?>");
}
});
});
});
</script>
my controller :
public function deletearticle($twid,$uid){
$this->ui_model->deletearticle($twid);
}
public function list_article_plain($uid){
$data['article'] = $this->ui_model->get_a_place_where_uid($uid);
$this->load->view('user/list_article.html' , $data);
}
my model :
public function deletearticle($twid){
$this->db->where('twid', $twid);
$this->db->delete('tempat_wisata');
}
Ajax is executed based on event, while PHP is the first to be executed.
Your 'url' in
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/ui_controller/deletearticle/<?php echo $twid;?>/<?php echo $id_session; ?>",
success: function(res){
$('#list_article').load("<?php echo base_url(); ?>index.php/ui_controller/list_article_plain/<?php echo $uid; ?>");
}
});
is invalid.
The server simply do not execute the php code in 'url'.

jQuery: Change an element value after Ajax Call

I am trying to change the value of a span after using jQuery.ajax.
JavaScript
$(document).on('click', '.kssico', function(event) {
var for_uid = $(this).parents("li").attr('data'),
for_name = $(this).parents("li").attr('unme'),
dataString = "for_uid=" + for_uid + "&for_name=" + for_name;
$.ajax({
type: "POST",
url: "include/ajax.php",
data: dataString,
success: function (html) {
if(html=="300") {
$('#myModal .modal-body p').html("Error Please Try Again.");
$('#myModal').modal('show');
}
else {
$(this).parents("li span.mi-val").html(html);
$('#myModal .modal-body p').html("The Requested Action Has Been Notified To Your Friend.");
$('#myModal').modal('show');
}
}
});
});
HTML
<li class="mispan main-span" >
<div class="thumbnail">
<h3 class="miheader">Dewashree Singh</h3>
<a >
<div class="miprofile-pic-cnt" ></div>
</a>
<div class="caption">
<p>
<a href="#" class="btn kssico miclickks">
<img src="/lisps.png"><span class="mi-val">0</span>
</a>
<a href="#" class="btn bdico miclickbd">
<img src="/besd.png"><span class="mi-val">1</span>
</a>
</p>
</div>
</div>
</li>
When I click on a.miclickks it should complete the Ajax call and return a value too be placed inside the span on the button anchor. However, nothing is being changed!
jsFiddle
I don't know what your whole code looks like, but you should be able to modify this to do what you are trying to:
$('#id').on('click', function() {
var that = this;
$.ajax({
url: 'wherever.php',
success: function(html) {
$(that).find("li span.mi-val").html(html);
}
});
It looks like you have two problems. The first is that as #Barmar posted above, mi-val is a child of micclickks, not a parent. The second is that your ajax complete function is asynchronous, so $(this) isn't what you expect it will be. Both of these are solvable though.
Your code is a bit messy and your html is missing the proper attributes, but this is I think roughly what you want:
$('.kssico').on('click', function(event) {
var for_uid = $(this).parents("li").attr('data'); //the li element in your html doesn't have a data attribute
var for_name = $(this).parents("li").attr('unme'); //the li element in your html doesn't have a 'unme' attribute
var dataString = "for_uid=" + for_uid + "&for_name=" + for_name;
$.ajax({
type: "POST",
context: this,
url: "include/ajax.php",
data: dataString,
success: function (html) {
if(html=="300")
{
$('#myModal .modal-body p').html("Error Please Try Again.");
$('#myModal').modal('show');
}
else
{
$(this).parents("li span.mi-val").html(html);
$('#myModal .modal-body p').html("The Requested Action Has Been Notified To Your Friend.");
$('#myModal').modal('show');
}
}
});
});

Loading a facebox modal window after an ajax request

this is what i have in the head tags of my html page to load the facebox plugin:
<!-- This is for the facebox plugin to work -->
<link href="<?php echo base_url();?>styles/facebox/src/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="<?php echo base_url();?>styles/facebox/lib/jquery.js" type="text/javascript"> </script>
<script src="<?php echo base_url();?>styles/facebox/src/facebox.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox({
loadingImage : "<?php echo base_url();?>styles/facebox/src/loading.gif",
closeImage : "<?php echo base_url();?>styles/facebox/src/closelabel.png"
})
})
</script>
this is the code for my div
<?php if(isset($$j)) : foreach($$j as $row) : ?>
<div class="single-result" id="single-result">
<div class="name"><?php echo $row->Name; ?></div>
<div class="description"><?php echo $row->Description; ?></div>
</div>
<?php endforeach; ?>
<?php else : ?>
error here
<?php endif; ?>
what i am wanting, is some ajax code that calls a function when the user clicks on the div id="single-result" so that it makes an ajax call to a php controller that gets the data from the database and then loads the view into a facebox modal window.
The part i am unsure about is how to make it load into a facebox modal window, l know how to use ajax calls to make it send data to a file and then load a view into a certain div on the page, l just want it to load it into the modal window instead of a certain div on the page.
Is this possible at all? (also i am using codeigniter for the php framework)
here is my ajax code which would load the ajax request results into the content-right div when the user clicks on div.
<script type="text/javascript">
$("#single-result").click(function() {
var form_data = {
ajax: '1',
itemcode: "<?php echo $row->id; ?>"
};
$.ajax({
url: "<?php echo site_url('ajax/item_info_right'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
$('#content-right').html(msg);
}
});
return false;
});
</script>
<!-- END -->
how do i modify this code above to make it work with going into a facebox modal window? I know facebox provides this code below, but i have no idea what to do with it?
jQuery.facebox(function() {
jQuery.get('code.js', function(data) {
jQuery.facebox('<textarea>' + data + '</textarea>')
})
})
It's not really clear what you're after...
If what you want is to have the data returned from the ajax POST to be populated into a facebox, simply call facebox on the success callback of the ajax post:
$("#single-result").click(function() {
var form_data = {
ajax: '1',
itemcode: "<?php echo $row->id; ?>"
};
$.ajax({
url: "<?php echo site_url('ajax/item_info_right'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
jQuery.facebox(msg);
})
});
alternatively, it looks like if you want the facebox loading graphics while the post is processed, you can wrap the ajax POST in facebox function:
$("#single-result").click(function() {
jQuery.facebox(function() {
var form_data = {
ajax: '1',
itemcode: "<?php echo $row->id; ?>"
};
$.ajax({
url: "<?php echo site_url('ajax/item_info_right'); ?>",
type: 'POST',
data: form_data,
success: function(msg) {
jQuery.facebox(msg);
})
});
})
})

Deleting a list item <li> with jquery?

I'm trying to delete a li item using jquery, but its not working. Here's my code:
the html file:
<li>
<img class="avatar" src="images/$picture" width="48" height="48" alt="avatar" />
<div class="tweetTxt">
<strong>$username</strong> $auto
<div class="date">$rel</div>
$reply_info
<div class="date"></div>
<a class ="delbutton" href="#" id = $id> Delete </a>
</div>
<div class="clear"></div>
</li>
The jquery file:
$(function () {
$(".delbutton").click(function () {
var del_id = element.attr("id");
var info = 'id=' + del_id;
if (confirm("Sure you want to delete this update? There is NO undo!")) {
$.ajax({
type: "POST",
url: "delete.php",
data: info,
success: function () {}
});
$(this).parents(".record").animate({
backgroundColor: "#fbc7c7"
}, "fast")
.animate({
opacity: "hide"
}, "slow");
}
return false;
});
});
the delete.php file:
<?php
include("includes/connect.php");
if($_POST['id'])
{
$id=$_POST['id'];
$sql = "delete from {$prefix}notes where id='$id'";
mysql_query( $sql);
}
?>
There is no element in your HTML with a class of record. I would try something like this:
<li class="record">
<!-- a bunch of other stuff -->
<a class="delbutton" href="#">Delete</a>
</li>
then in the JS:
$(function ()
{
$(".delbutton").click(function ()
{
if (confirm("..."))
{
$.ajax({ /* ... */});
$(this).closest(".record").fadeOut();
}
return false;
});
});
If your div look like this:
<ul>
<li>One | <a href='#' class='delete'>Delete</a></li>
<li>Two | <a href='#' class='delete'>Delete</a></li>
<li>Three | <a href='#' class='delete'>Delete</a></li>
<li>Four | <a href='#' class='delete'>Delete</a></li>
</ul>
jQuery:
jQuery(document).ready(function(){
jQuery('.delete').live('click', function(event) {
$(this).parent().fadeOut()
});
});​
Check: http://jsfiddle.net/9ekyP/
EDIT:
You can remove your li after getting response in success function something like this:
jQuery(document).ready(function(){
jQuery('.delbutton').live('click', function(event) {
$.ajax({
type: "POST",
url: "delete.php",
data: info,
success: function(){
$(this).parent().parent().fadeOut();
}
});
});
});​
There are several issues with your code...
element.attr("id") references undeclared element but this should probably be $(this).attr("id")
The <li> block has no class ".record" either
EDIT: You only fade your <li> but do not actually remove it from the DOM (don't know if this was deliberate though)
The <a>'s ID is not quoted (and not escaped either... as are the other strings you insert in PHP (EDIT) and the ID you use in your delete script - this is very dangerous as it allows cross-site scripting / XSS and SQL injection as TokIk already pointed out)
PHP:
echo '<li class="record">
<img class="avatar" src="images/'.htmlentities($picture).'" width="48" height="48" alt="avatar" />
<div class="tweetTxt">
<strong>'.htmlentities($username).'</strong> '.htmlentities($auto).'
<div class="date">'.htmlentities($rel).'</div>'.htmlentities($reply_info).'<div class="date"></div> <a class="delbutton" href="#" id="'.htmlentities($id).'"> Delete </a>
</div>
<div class="clear"></div>
</li>';
JavaScript:
$(document).ready(function() {
$(".delbutton").click(function(){
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
if(confirm("Sure you want to delete this update? There is NO undo!")) {
$.ajax({
type: "POST",
url: "delete.php",
data: info,
success: function(){
alert('success');
},
error: function(){
alert('error');
}
});
$(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
});
EDIT: Delete script (note the additional error check that $_POST['id'] exists and the pseudo-function for quoting the ID):
<?php
include("includes/connect.php");
if( isset($_POST['id']) ) {
$id = quote($_POST['id']);
$sql = "delete from {$prefix}notes where id='$id'";
mysql_query( $sql);
}
?>
I am assuming that '.records' is the container.
You can pass through your ID value to make <li> unique, result being:
<li id='record_12'>
//CONTENT
</li>
<li id='record_13'>
//CONTENT
</li>
And change your SUCCESS script to the following:
$(".delbutton").click(function(){
var del_id = element.attr("id");
var info = 'id=' + del_id;
if(confirm("Sure you want to delete this update? There is NO undo!"))
{
$.ajax({
type: "POST",
url: "delete.php",
data: info,
success: function(){
//Getting the unique LI and fading it out
$('#record_' + del_id).fadeOut();
}
});
Your code works fine well.I was trying to code a form that has multiple textbox, then after each textbox threre will be link called add which POST call the other php called addnew.php.
In addnew.php data will we added to database(postgres). But I am geting problem while getting the post variable itself.
this is my code for form (I will chage for multiple textbox once it works fine)
script:
<script type='text/javascript'>
$(window).load(function() {
jQuery(document).ready(function() {
jQuery('.add').live('click', function(event) {
var da = $('form#myform').serialize();
alert(da);
$.ajax({
type: "POST",
url: "addnew.php",
data:da,
success: function(data) {
if (data === "ok") {
$(this).parent().fadeOut();
$('#results').html(data);
}
else {
alert(data);
}
}
});
});
});
});
Form
<form name='myform' id='myform'>
<input name='da' type='text' id='da' value='none'>
<a href='#' class='add'>Add</a>
</form>
addnew.php code here
<? php
if( isset($_POST['da']) )
{
echo (da);
}
?>

Categories