hi guys i am trying to get a button id that is in a for loop so whenever the button click the unique id that can be differentiated from other ids should be able to work. let me show you my code.
<?php foreach ($message as $key ) : ?>
<?php echo $senders_user_id=$key['senders_id']; ?>
<div class="senders_id" S_id="<?php echo $key['senders_id']; ?>" style="display: none;"></div>
<a class="list-group-item">
<div class="checkbox">
<label>
<?php echo form_open(,['id'=>'sender_user_id']); ?>
<input type="checkbox">
<button id="senders_id" class="Read" S_id="<?php echo $key['senders_id'];?>" onclick="readbyuser($senders_user_id);">Read</button>
<button id="senders_id" class="unread" S_id="<?php echo $key['senders_id'];?>" onclick="unreadbyuser($senders_user_id);">Unread</button>
</label>
</div>
<span class="glyphicon glyphicon-star-empty"></span><span class="name" style="min-width: 120px;
display: inline-block;"><?php echo $key['uname']; ?></span> <span class=""><?php echo $key['textdata']; ?></span>
<span class="text-muted" style="font-size: 11px;"></span> <span class="badge">12:10 AM</span> <span class="pull-right"><span class="glyphicon glyphicon-paperclip">
<?php echo form_close(); ?>
</span></span></a>
<?php endforeach; ?>
</div>
Now here there are three post meaning three 'senders_id' so when i click on button Read the id of that should pass but the same id is passing again and again no matter which button i click on
Here is my code
function readbyuser()
{
var senders_id=$('#senders_id').attr('S_id');
var Recievers_id=$('.Recievers_id').attr('R_id');
jQuery.ajax({
type:'post',
url:'<?php echo base_url("user/read_by_user"); ?>',
data:{id:senders_id,R_id:Recievers_id},
dataType:'json',
success:function(data)
{
console.log(data);
alert(data);
$('.unread').removeClass('unread_user');
$('.Read').addClass('read_user');
}
});
}
function unreadbyuser()
{
var senders_id=$('#senders_id').attr('S_id');
var Recievers_id=$('.Recievers_id').attr('R_id');
jQuery.ajax({
type:'post',
url:'<?php echo base_url("user/unread_by_user"); ?>',
data:{id:senders_id,R_id:Recievers_id},
dataType:'json',
success:function(data)
{
$('.Read').removeClass('read_user');
$('.unread').addClass('unread_user');
}
});
}
please tell me what i am doing wrong
<button id="senders_id" class="Read" S_id="<?php echo $key['senders_id'];?>" onclick="readbyuser($senders_user_id);">Read</button>
<button id="senders_id" class="unread" S_id="<?php echo $key['senders_id'];?>" onclick="unreadbyuser($senders_user_id);">Unread</button>
Two elements cannot have samei id. Use class name instead.
Also you can pass PHP values directly in this.
onclick="readbyuser($senders_user_id);"
as
onclick="readbyuser(<?php echo $key['senders_id'];?>);"
Related
-when i update with ajax , he changed but he only took to 'id' not the new variable
But I want to insert my E-Mail to database through Ajax. I don't want my page to get redirected, because every time the page got refreshed, null value got inserted to the database..
1)and ajax
function editdata(str){
var id= str;
var name = $('#nameofequipe-'+str).val();
$.ajax({
type:"post",
url: "<?php echo API;?>equipe/update.php",
data:"nameofequipe="+name+"&id="+id,
success:function(data){
alert('success update data ');}
2)and modal bootstrap
<div
class="modal fade"
id="edit-<?php echo $row['id']; ?>"
role="dialog"
aria-labelledby="updatelLabel-<?php echo $row['id']; ?>
<input
type="hidden"
id="<?php echo $row['id']; ?>"
value="<?php echo $row['id']; ?>" >
<input
type="text"
class="form-control"
id="nameofequipe-<?php echo $row['id']; ?>"
value="<?php echo $row['equipe']; ?>">
if somoene know how i can do This can He help me
//use this in script section
function editdata(str){
var id= str;
var name = $('#nameofequipe-'+str).val();
$.ajax({
dataType: "json",
type:"post",
url: "<?php echo API;?>equipe/update.php",
data:"{nameofequipe:"+name+",id:"+id+"}",
success:function(data){
alert('success update data ');
}
//whereas this one in your PHP file where you use this data nameOfEquipe and Id to update you database.
if(isset($_POST['nameofequipe'], $_POST['id']) {
$nameOfEquip=$_POST['nameofequipe'];
$id=$_POST['id'];
}
This Is all code
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
</button>
</div>
<form>
<div class="modal-body">
<input type="hidden" id="<?php echo $row['id']; ?>" value="<?php echo $row['id']; ?>" >
<input type="text" class="form-control" name="nameofequipe-<?php echo $row['id']; ?>" value="<?php echo $row['equipe']; ?>">
</div>
<button type="submit" onclick='editdata(<?php echo $row['id']; ?>)' class="btn btn-primary">Save changes</button>
</div>
</form>
ajax and js
function editdata(str)
{
var id= str;
var name= $('#nameofequipe-'+str).val();
$.ajax ({
type:"post",
url: "<?php echo API;?>equipe/update.php",
data:"nameofequipe="+name+"&id="+id,
success: function(data){
alert("Data Save: " + data);
}
}); }
update. PHP
$id = $_POST['id'];
$name=$_POST['nameofequipe'];
$result = $o->SelectQuery("UPDATE rh_equipes SET equipe='$name' WHERE id='$id'");
if($result){
echo 'data update';
}
I want to build filtering sidebar and search box . My problem is, I can't make it run searching box and filtering items together at the same time. I hope any of u can help me?
I think,the main problem is in my script "var s" php parameter where I'm getting cities from
Let's see what I've done so for,
Here's my script;
<script>
$(function(){
$('.item_filter').change(function(){
var p = multiple_values('p');
var s ='<?php echo $_POST["search"]; ?>';
var url ="product.php?product=<?php echo
htmlentities($_GET['product']) ?>&s="+s+"&b="+b+"&p="+p;
window.location=url;
});
});
function multiple_values(inputclass){
var val = new Array();
$("."+inputclass+":checked").each(function() {
val.push($(this).val());
});
return val.join('-');
}
</script>
Search Box
<form class="search" name="search" method="POST"
action="product.php?product=<?php echo
''.htmlentities($_GET['product']).''; ?>" >
<a href="javascript:void(0);"><input id="small_search"
name="searchTerm" class="item_filter s" <?php { echo "checked"; } ?>
list="local" /></a>
<?php
while($row = mysqli_fetch_assoc($result)){
$local = ($row['local']); ?>
<a href="javascript:void(0);" target="_blank"><input type="submit"
id="small_search_btn" value="Search"></a >
<datalist id="local">
<option value="<?php echo ''.htmlentities($row['local']).''; ?
>"></option>
<?php } ?>
</form >
**Filtering **
<div class="list-group" style="font-size:14px;">
<h4 style="font-size:16px; font-weight: bold; color:black;">
</h4>
<?php
some sql statement..
?>
<a href="javascript:void(0);" class="list-group-item"
id="left_filtering" >
<label >
<input type="checkbox" class="item_filter b" value="<?php echo
htmlentities($row['brand']); ?>" <?php if(in_array($row['brand'],$b
{echo "checked"; } ?> > <?php echo ucfirst($row['brand']);
?></a>
</label>
<?php } ?>
</div>
I have two cases, One when there is comments and one when there is not. I have separated two cases with php like this:
<div class="box">
<textarea required id="reviews_comment" name="comment" value="<?php echo $this->input->post('comment'); ?>" ></textarea>
<button style="display: none;" id="comment_button" class="btn btn-success" >Add Comment</button>
<div id="comment_div">
<b>Comments...</b></br>
<?php if($comment_data){ foreach ($comment_data as $data) {?>
<b>By: <span><?php echo $data->first_name.' '.$data->last_name; ?></span></b>
<p><?php echo $data->comment; ?></p>
<?php } } else{?>
<div id="no_comment_case" ><h1>No comments yet...</h1></div>
<?php } ?>
</div>
Now when there is no comment No comment case will be displayed. After user posts a comment with ajax i have tried the following code to hide the no comment case and only append the recent comment, but the no comment case is not being hidden.
The jquery code is:
success: function(msg){
console.log(msg);
if(msg=="success")
{
$('#comment_button').hide();
$('#reviews_comment',$('#comment_div')).hide();
// $('div#comment_div> div:eq(2)').css("display", "none");
html='<b>By: <span>'+username+'</span></b><p>'+review+'</p>';
$('#comment_div').append(html);
}
}
How can I do it. Any kind of suggestion are highly appreciated. Thanks.
Enclose comment_div properly,
<div class="box">
<textarea required id="reviews_comment" name="comment" value="<?php echo $this->input->post('comment'); ?>" ></textarea>
<button style="display: none;" id="comment_button" class="btn btn-success" >Add Comment</button>
<div id="comment_div">
<b>Comments...</b></br>
<?php if($comment_data){ foreach ($comment_data as $data) {?>
<b>By: <span><?php echo $data->first_name.' '.$data->last_name; ?></span></b>
<p><?php echo $data->comment; ?></p>
<?php }
} else{?>
<div id="no_comment_case" ><h1>No comments yet...</h1></div>
<?php } ?>
</div>
</div>
Ajax call:
success: function(msg){
if(msg=="success")
{
$('#comment_button').hide();
$('#reviews_comment,#comment_div,#no_comment_case').hide();
html='<b>By: <span>'+username+'</span></b><p>'+review+'</p>';
$('#comment_div').append(html);
}
}
HTML + PHP
<?php
for($i=0;$i<5;$i++){
?>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this.id, $('.copyTarget').attr('id'));">Copy</button>
<?php
}
?>
JS
<script>
function reply_click(clicked_id, target_id) {
alert(clicked_id);
alert(target_id);
}
</script>
What i want
I want to get the both values for copyTarget and copyButton as per loop cycle. It means
If current value of $i = 3
then I want alert values like,
clicked_id = copyTarget3
target_id = copyButton3
What i am currently getting is,
If current value of
$i = 3
then I want alert values like,
clicked_id = copyTarget0
target_id = copyButton3
Its taking first value of ID(copyTarget) stored initially. I want current loop cycle value.
Any help would do
Thanks
Why use JS in handler?
Try:
onClick="reply_click('copyButton<?php echo $i; ?>', 'copyTarget<?php echo $i; ?>')"
Also you should store id names (copyButton and copyTarget) in php variable, so you can change them in one place.
You could try something like below. However, I would go by Maxx's answer .. It really depends on what you plan to do with the rest of code etc.
<?php
for($i=0;$i<5;$i++){
?>
<div>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this)">Copy</button>
</div>
<?php
}
?>
<script>
function reply_click(btn) {
var clicked_id = $(btn).attr('id');
var target_id=$(btn).parent().find('span').html();
alert(clicked_id);
alert(target_id);
}
</script>
Try This
<?php
for($i=0;$i<5;$i++){
?>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this.id);">Copy</button>
<?php
}
?>
JS
<script>
function reply_click(clicked_id) {
alert(clicked_id); //clicked id
alert(clicked_id.split('copyButton').join('copyTarget')); // target id
}
</script>
This is code for generating page:
<div id="mali_oglasi">
<?php foreach ($mgs as $key) : ?>
<p class="mali_oglas">
<span class="name"><?php echo $key['name'] ?></span>
<span class="body"><?php echo $key['body'] ?></span>
<span class="contact_author"><?php echo $key['contact_author'] ?></span>
<span class="date_created"><?php echo $key['date_created'] ?></span>
<span class="date_expires"><?php echo $key['date_expires'] ?></span>
<span class="email"><?php echo $key['email'] ?></span>
<?php foreach ($lokacija as $lok) : ?>
<?php if($lok['id_location'] == $key['location_id']) : ?>
<span class="lokacija" id="<?php echo $lok['id_location'] ?>"><?php echo $lok['name'] ?></span>
<?php endif ?>
<?php endforeach; ?>
<?php foreach ($kategorija as $kat) : ?>
<?php if($kat['id_category'] == $key['category_id']) : ?>
<span class="kategorija" id="<?php echo $kat['id_category'] ?>"><?php echo $kat['name'] ?></span>
<?php endif ?>
<?php endforeach; ?>
<a class="obrisi" id="<?php echo $key['id_global_info'] ?>">Obrisi</a>
<a class="izmeni" id="<?php echo $key['id_global_info'] ?>">Izmeni</a>
</p>
<?php endforeach; ?>
</div>
which will give this result:
<div id="mali_oglasi">
<p class="mali_oglas">
<span class="name">fbsd</span>
<span class="body">sdhdsf</span>
<span class="contact_author">mirko</span>
<span class="date_created">2012-03-15 11:24:19</span>
<span class="date_expires">2012-04-14 11:24:19</span>
<span class="email">a#a.com</span>
<span class="lokacija" id="1">Pirot</span>
<span class="kategorija" id="1">Auto i Moto</span>
<a class="obrisi" id="19">Obrisi</a>
<a class="izmeni" id="19">Izmeni</a>
</p>
<p class="mali_oglas">
<span class="name">dsh</span>
<span class="body">dshg</span>
<span class="contact_author">mirko</span>
<span class="date_created">2012-03-15 11:17:52</span>
<span class="date_expires">2012-04-14 11:17:52</span>
<span class="email">a#a.com</span>
<span class="lokacija" id="1">Pirot</span>
<span class="kategorija" id="1">Auto i Moto</span>
<a class="obrisi" id="18">Obrisi</a>
<a class="izmeni" id="18">Izmeni</a>
</p>....
</div>
and JS for replaceWith and load:
$(parent).on('click', '.izmeni', function() {
var name = $(this).siblings('.name').text();
var body = $(this).siblings('.body').text();
var email = $(this).siblings('.email').text();
var contact_author = $(this).siblings('.contact_author').text();
var id_lok = $(this).siblings('.lokacija').attr("id");
var id_kat = $(this).siblings('.kategorija').attr("id");
$(this).siblings('.name').replaceWith('<input value="' + name + '" />');
$(this).siblings('.body').replaceWith('<textarea>' + body + '</textarea>');
$(this).siblings('.date_created').hide();
$(this).siblings('.date_expires').hide();
$(this).siblings('.email').replaceWith('<input value="' + contact_author + '" />');
$(this).siblings('.lokacija').replaceWith(function(){
$(this).load("<?php echo base_url() ?>form/location", {'id' : id_lok})
});
$(this).siblings('.kategorija').replaceWith(function(){
$(this).load("<?php echo base_url() ?>form/category", {'id' : id_kat})
});
});
load is calling following page (for lokacija):
<select>
<?php foreach ($lokacija as $lok) : ?>
<option
<?php if($lok['id_location'] == $id) : ?>
selected="selected"
<?php endif ?>>
<?php echo $lok['name'] ?>
</option>
<?php endforeach; ?>
</select>
Everything except lokacija and kategorija is replaced, but in the firebug I am getting positive response (page requested page is loaded without the errors but it is not displayed). What am I doing wrong?
Dosent LOAD and REPLACEWITH both replace the content, so you are replacing the replaced the content.
So can't you just either add the ID to the load method, or make a normal ajax GET and the use the replacewith function?