jQuery .bind only working on first element - php

I am trying to create a jQuery function that will allow me to add, edit, and delete articles on my site. Right now, I am trying to write the delete functionality. Basically, when the delete button is clicked, it will send the id of that specific article to a php script via ajax request. It gets the article ID from the ID attribute of the article element.
My problem is, that when the Delete button is clicked I get an alert box with the article ID on the first article, but not on the rest.
Here is my code:
HTML/PHP:
<div id="contentLeft">
<?php
foreach($data->blogPosts as $post){
?>
<article id="<?php print $post->ID; ?>" class="article">
<div id="options">
<div id="edit"><a class="btn btn-danger" href="#" id="editButton"><i class="fa fa-pencil"> Edit</i></a></div>
<div id="delete"><a class="btn btn-danger" href="#" id="deleteButton"><i class="fa fa-trash-o">Delete</a></i></div>
</div>
<div id="articleTitle"><?php echo $post->Title; ?></div>
<div id="content">
<?php echo $post->Content; ?>
</div>
<div id="date"><?php echo date('F jS, Y', strtotime($post->Date)); ?></div>
</article>
<?php
}
?>
</div>
My jQuery:
$("#deleteButton").bind('click', function(){
var id = $("article").closest("article").attr("id");
alert(id);
});
I appreciate the help!

In a given document ids should be unique; no more than one element should have the same ID. Please change your id's to classes and change your js accordingly:
HTML
<a class="btn btn-danger deleteButton" href="#"> ....
JS
$(".deleteButton").bind('click',...

You can add only one element with the same id.
Probably you should change id to class:
<div class="delete"><a class="btn btn-danger" href="#" class="deleteButton"><i class="fa fa-trash-o">Delete</a></i></div>
and in your js code:
$(".deleteButton").bind(...)

Related

JQuery script to change css element to all assigned element printed in foreach loop

In foreach loop, I have a script that assigns id after the counter is higher than 5. I am using jquery which on click removes id. It works, but it doesn't remove all elements assigned.
CSS code I used to hide elements.
<style>
#hide{
display:none;
</style>
// PHP code I use and HTML.
<?php
$i = 0:
foreach($payemnts as $p){
$i++;
?>
<!-- In the div id is assigned -->
<div class="row" <?php if($i > 5) echo 'id="hide"'?>>
<div class="col">
<a href="<?php echo get_permalink($id); ?>">
<img src="<?php echo get_the_post_thumbnail_url($id); ?>"?>">
</a>
</div>
<div class="col">
</div>
<div class="col">
</div>
</div>
<?php } /* end foreach */?>
<div class="row">
<button type="button" onclick="showAll()" class="btn btn-sm ">READ MORE</button>
</div>
// JQUERY function I used to change CSS element of an id
<script>
function showAll() {
$("#hide").css({'display':'flex'});
}
</script>
The solution above, after clicking on the button shows only one. It changes CSS to only one element, not to all. I need to show all elements after clicking on Read More button.
I was trying also .remove() but it doesn't work.
Thanks in advance
change id='hide'to class='hide'
and then
<script>
function showAll() {
$(".hide").css({'display':'flex'});
}

How to make the button visible and hidden according to requirement

I want to display the button only when $safe_cus is 1.But the button gets displayed when the browser is resized.
<?php if($safe_cus == 1) {?>
<div class="col-sm-6 hidden-xs">
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo $action_multiple; ?>"><?php echo $button_multiple_bill_payment; ?></a>
</div>
</div>;
<?php } ?>
one more thing you can do is write CSS based on your condition for ex
if($safe==1){.css{display:none;}}else{.css{display:blockl}}
Now put CSS as a class on your button
hope this helps you.
u can use javascript:
first set an id for your div;
for example "parentdiv"
then get your div to you javascript code:
document.getElementById("parentdiv");
and set an onload function for ur body tag:
body onload="show()"
then you can do ur display stuff with php:
<?php if($safe_cus == 1) {?>
<div class="col-sm-6 hidden-xs">
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo $action_multiple; ?>"><?
php echo $button_multiple_bill_payment; ?></a>
</div>
</div>;
<script>
function show(){
var div = document.getElementById("parentdiv");
div.style.display="block";
}
</script>
<?php } ?>

How to redirect to a particular block of html page?

I'm working on a simple project which has a requirement to show a particular block of html by passing an div id to that url.
I have tried so many solutions, but none of them worked for me.
Here is my code.
<div class="row">
<a onclick="redirect_tooltip();">
<i class="fa fa-question-circle" aria-hidden="true"></i>
</a>
</div>
Whenever I click the icon it calls a Javascript function, through that function I tried to pass a url with another page division id
function redirect_tooltip()
{
var source_type = $("#_src_type").val();
if(source_type == "source")
{
window.location.replace(base_url+'/help/tooltip#rtmp_4');
//I have already difined the base url
}
}
My html code:
<div id="rtmp_4">
<h4>Some Title</h4>
<h5> <?php echo trim($tooltip_rtmp_server[1]);?></h5>
<p> <?php echo trim($tooltip_rtmp_server[3]);?> </p>
<h5> <?php echo trim($tooltip_rtmp_stream_name[1]);?></h5>
<p> <?php echo trim($tooltip_rtmp_stream_name[3]);?> </p>
</div>
I need to display only the rtmp block whenever the url executes. Please give me a suggestions like where I am doing the mistake.

sending user a mesage by passing their id to a modal box

I am trying to create a message form into a bootstrap modal by Passing the user username/id to the modal for identification.
here is an html/php code that made a list of registered users.
<div class="col-lg-4 col-md-4 col-sm-4 mb">
<div class="content" style="margin-bottom:5px;">
<ul class=" extended inbox">
<div class="notify-arrow notify-arrow-green"></div>
<li>
<p class="green">People you can follow</p>
</li>
<?php
//Show all users
foreach ($users as $row) {
?>
<li style="padding:3px; margin-bottom:3px; width:100%; background:#CF9;">
<a href="index.html#">
<span class="photo" style="float:left; padding:4px;"><img alt="avatar" src="uploads/<?php echo $row['pix']; ?>" width="50" height="50"></span>
<span class="subject" style="float:left;">
<span class="from">
<?php
echo ''.$row['user_lastname']. ' '.$row['user_firstname'].'';
?>
<a class="" data-toggle="modal" data-target="#profile_pic_modal"><span class="glyphicon glyphicon-comment"></span></a>
</span><br>
<span class="time">
<?php
echo (($row['receiver'] === $row['user_id'] && $row['sender'] === $user_id)
? '<button class="btn follow following " rel="'.$row['user_id'].'">Following</button>'
:' <button class="btn follow" rel="'.$row['user_id'].'">
<i class="fa fa-user-plus alert-info"></i> Follow </button>');
?>
</span>
</span>
</a>
</li><br>
<?php
}
?>
</ul>
</div>
When a user click on another user's message icon he/she should be able to send message. Could someone please show me how to do this using php/mysqli and ajax
First add a data-(anyName) tag like below
<?php $ID = 'The persons Username'; ?>
<a href='#' class='btn btn-warning btn-xs open-AddBookDialog' data-id='$ID' data-toggle='modal'>My Modal</a>
Then put a input tag inside your modal body with a ID of example(MyID)
<input type="text" id="MyID">
Then using the code below (jquery) this will pass the value of data-id into that input tag on modal show.
$(document).on("click", ".open-AddBookDialog", function () {
var myId = $(this).data('id');
$(".modal-body #bMyID").val(myId);
$('#myModal').modal('show');
});
Information like username (login?) and id of a logged user should be disposed by Session, unlike you have good reasons for not do that. When the user enters in the system, you will already load a bunch of data to be sure he is valid, so the practical way to not retrieving always all the data from the user logged in, that are currently using your system, it's always good to use SESSIONs to handle that (imagine a ton of users using your system and you needing to retrieve each of them to each click and routine inside the system).
In a practical way, just open your modal and validate your routine using the started session with the user information settled in.
To clarify about the OTHERS users data (those listed that the current user will click to send a message) you can have to methods:
YOUR HTML:
<a href="#" data-userid='<?= $row['user_id']; ?>'>
<span class="photo" style="float:left; padding:4px;"><img alt="avatar" src="uploads/<?php echo $row['pix']; ?>" width="50" height="50"></span>
<span class="subject" style="float:left;">
<span class="from">
<?php
echo ''.$row['user_lastname']. ' '.$row['user_firstname'].'';
?>
<a class="" data-toggle="modal" data-target="#profile_pic_modal"><span class="glyphicon glyphicon-comment"></span></a>
</span><br>
<span class="time">
<?php
echo (($row['receiver'] === $row['user_id'] && $row['sender'] === $user_id)
? '<button class="btn follow following " rel="'.$row['user_id'].'">Following</button>'
:' <button class="btn follow" rel="'.$row['user_id'].'">
<i class="fa fa-user-plus alert-info"></i> Follow </button>');
?>
</span>
</span>
</a>
#Modal (Assuming you're using v4)
<div class="modal hide fade" id="user-message">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3 class="hdrtitle">Hey <?= $_SESSION['login']; ?>, Send message to <span></span></h3>
</div>
<div class="modal-body">
<label for='msg'>Message:</label>
<textarea id='msg' rows="4" cols="50" name='msg'></textarea>
<button id='send-msg'>Send Msg</button>
</div>
</div>
and then showing it with:
$('a').on('click', function(){
//retrieve all other user data
$.get( "yourcode.php?userid=" + $(this).data('userid'), function( data ) {
$('.modal h3 span').html(data.username);
//FILL YOUR MODAL AS YOU WISH
$('.modal').modal('toggle');
});
});
But the best way is to click and mount the modal in a partial view bringing the HTML already mounted with all information you want just to trigger the modal routine, avoiding html structure that the user don't really needs at first time access the page.
$('a').on('click', function(){
//retrieve all other user data
$.get( "partialmodal-sendmsg.php?userid=" + $(this).data('userid'), function( data ) {
$('body').append(data);
//FILL YOUR MODAL AS YOU WISH
$('.modal').modal('toggle');
});
});
Some must reads about:
When should I use session variables instead of cookies?
http://cse.unl.edu/~riedesel/pub/cse413/Project%202/SESSION.pdf

Giving Twitter Bootstrap Modals Dynamic IDs with PHP

I am trying to generate unique IDs for each modal, as I will be loading in an address specific map to each modal. My issue is that when I click the link:
<a data-toggle="modal" href="#myModal_<? echo $meeting['ID'] ?>" ><? echo $meeting['Address'].", ".$meeting['City'] ?></a>
it tries to take me to mypage.php/#myModal_8 (or whatever the given ID is, instead of pulling up that specific modal. The modals work great is I don't have the ID attached, but they won't load dynamic content that way.
<a data-toggle="modal" href="#myModal_<? echo $meeting['ID'] ?>" ><? echo $meeting['Address'].", ".$meeting['City'] ?></a>
</p>
<div class="modal hide" id="#myModal_<? echo $meeting['ID'] ?>">
<div class="modal-body">
<? echo $meeting['Address'] ?>
</div>
<div class="modal-footer">
Close
</div>
</div>
Any help on this would be much appreciated. Thank you!
It's all about the ID you are giving to the modal : id="#myModal_<? echo $meeting['ID'] ?>"
The # is only needed for the jQuery selector, not the ID. Which would give :
<div class="modal hide" id="myModal_<? echo $meeting['ID'] ?>">
Further more, you should open your php tags with <?php for the reasons described here : Are PHP short tags acceptable to use?

Categories