I have two sets of buttons for every package, one for inserting data into my database and the other for deleting data from the database.
When I click the 'Select' button data will be inserted through AJAX and then hide the 'Select' button and display the 'Selected' button. When I click on the 'Selected' button the data will be deleted from the database through an AJAX call. The 'Selected' button will then be hidden and the 'Select' button will be shown again.
All the select & selected buttons are within a PHP while loop so the buttons have the same name but incremental IDs. When I click on one single 'Select' button, the other button is also showing me 'Selected'. I need something to track the each button ID when I click.
<div class="col-sm-2">
<a class="btn btn-primary select-btn select-btn-broadcast" id="<?php echo $id ; ?>" onClick="addintoListBroadcast(this)">Select</a>
<a class="btn btn-primary active-button active-button-broadcast" id="<?php echo $id ; ?>" onClick="deleteintoListBroadcast(this)" style="display: none;">Selected</a>
</div>
<script>
$('.select-btn-broadcast').click(function (e) {
var pkgId = this.id;
//alert(pkgId);
$('.select-btn-broadcast').hide();
$('.active-button-broadcast').show();
});
$('.active-button-broadcast').click(function(e) {
$('.select-btn-broadcast'.show();
$('.active-button-broadcast').hide();
});
</script>
The main issue with your code is because you select all the elements with the given class, not just the one related to the element which was clicked. To fix this use the this keyword to traverse the DOM to find the sibling() a element, and then amend it as required.
Also note that there's several other things you can improve in your code. Firstly, don't use inline CSS. Use the classes you've already applied to the elements to amend their styling in an external stylesheet.
Similarly, don't use inline event handlers such as onclick. Call the relevant functions within the unobtrusive event handlers you've bound through jQuery.
With all that said, here's a working example:
$('.select-btn-broadcast').on('click', function(e) {
$(this).hide().siblings('.active-button-broadcast').show();
// addintoListBroadcast(this);
});
$('.active-button-broadcast').on('click', function(e) {
$(this).hide().siblings('.select-btn-broadcast').show();
// deleteintoListBroadcast(this);
});
.active-button-broadcast {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-sm-2">
<a class="btn btn-primary select-btn select-btn-broadcast">Select</a>
<a class="btn btn-primary active-button active-button-broadcast">Selected</a>
</div>
<div class="col-sm-2">
<a class="btn btn-primary select-btn select-btn-broadcast">Select</a>
<a class="btn btn-primary active-button active-button-broadcast">Selected</a>
</div>
<div class="col-sm-2">
<a class="btn btn-primary select-btn select-btn-broadcast">Select</a>
<a class="btn btn-primary active-button active-button-broadcast">Selected</a>
</div>
Related
I have a table that is populate using AJAX. Each table row is give an id based on a record id in my database. Under the table are 3 buttons: NEW, EDIT and DELETE.
When I select a row I set the data-id of the DELETE button to the clicked row's ID. When I click DELETE my AJAX call proceeds to delete all records from my database that have the data-id of the DELETE button.
This all works perfectly. After everything is done i get an alert the the data was deleted. Which is great.
My Question is: in the If (data ==1) section of my AJAX call, how do i remove the row, fade out, from the table without refreshing the page like i have to do now.
Here is my index.html code
<div class="container-fluid inputwrapper">
<table id="workRequests" style="width: 100%;">
//table is filled by ajax call to db below is an example of the format
<tr class="table__row" id="124108" onclick="highlight_row()">
<td class="table__content" data-heading="Number">124108</td>
....
</tr>
</table>
</div>
<div class="container-fluid">
<div class="row center-block">
<div class="text-center col-sm-2 col-sm-offset-0" style="float: left;"> </div>
<div class="text-center col-sm-8" style="float: left;">
<button style="margin-right: 10px;" type="button" class="btn btn-default" id="wopnew" onClick="createNew()">New</button>
<button style="margin-right: 10px;" type="button" class="btn btn-default" disabled id="wopedit" onClick="editFile()">Edit</button>
<button type="button" class="btn btn-default" data-id="0" id="wopdelete">Delete</button>
</div>
<div class="text-center col-sm-2 col-sm-offset-0" style="float: left;"> </div>
</div>
Here is my AJAX call.
I am a novice at coding and from what I can see as i Step thought the function in my browser - the var element is set to the DELETE button data-id so when we get to the success part and data == 1 nothing happens as it is focused on the DELETE button not the Table Row.
I have tried to change element to the id of my table but that does not work either.
<script>
$(document).on("click","#wopdelete",function(){
if (confirm("Are you sure you want to delete this Record? It can NOT be recoverd!!")) {
var id = $(this).data('id');
var element = this;
$.ajax({
url :"./scripts/delRecord.php",
type:"POST",
cache:false,
data:{deleteId:id},
success:function(data){
if (data == 1) {
$(element).closest('tr').fadeOut(400, function(){
$(this).remove();
});
alert("User record deleted successfully");
}else{
alert("Record did not delete properly!");
}
}
});
}
});
</script>
Before, I had a view called "book-edit/[ID]" to edit a book but now I added the form to edit the book in the book details page. I have a button to edit in the list of books page and I want it to go to the book page and open de edit modal on click. This is what I have rn:
View: Books (datatable list with this buttons for each):
<a href="<?php setURL(); ?>./book/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="View Book">
<i class="fas fa-eye"></i>
</a>
<a href="<?php setURL(); ?>./copy-add/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Add Copy">
<i class="fas fa-plus"></i>
</a>
<a href="<?php setURL(); ?>./book-edit/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Edit Book">
<i class="fas fa-edit"></i>
</a>
View: Book/[ID] (I have modals with id "#editBookModal" and "#addCopyModal")
When you click on the books list to edit the book or add the copy, I want it to go to book/[ID] and automatically open the intended modal. But only when buttons clicked, if you go to Book/[ID] to view the book, it shouldn't open the modals automatically. That's why I can't just open it with jquery in the book/[ID] page...
Any idea of how I can do this?
Thanks!
One approach would be add hashes to the url in the links like:
....
Then on the other page check the url hash and if it matches a modal then open that modal
const modalHashes = {
'#edit-book':'#editBookModal',
'#copy-book' : '#addCopyModal'
}
let hash = window.location.hash;
// inside document.ready and after modal script has loaded
if(hash && modalHashes[hash]){
const modalSelector = modalHashes[hash];
$(modalSelector).yourModalShowMethod()
}
You could even remove the hash when the modal gets closed so if user refreshes the page it won't pop up again
// in modal close event handler
window.location.hash =''
I can show details data in a new page using details.php using anchor tag like bellow :
<a class="btn btn-info" href="details.php?view_id=<?php echo $row['studentID']; ?>" title="click for Details" onclick="details.php"><span class="glyphicon glyphicon-eye-open"></span> Show Details</a>
Now I am trying to learn it to show using bootstrap modal box like code bellow:
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Details</button>
I have put the code of "details.php" in #myModel section and how I can show details data using dynamic ID - comes from view_id= for the code I am trying?
Please help me to understand my mistake. Thank you.
I have CI framework and user, group permission control applied in controller,
But how can i apply restriction in some buttons, links, etc in view?
Example, below is code for new, edit, delete buttons, if i want to disble\hide this buttons
<button type="button" class="btn blue" id="new_group_showbtn"><i class="fa fa-plus"></i> New</button>
<button type="button" class="btn purple" id="edit_group_showbtn"><i class="fa fa-edit"></i> Edit</button>
<button type="button" class="btn yellow" id="delete_group_btn"><i class="fa fa-trash"></i> Delete</button>
I used control over controller as below
if($this->mylib->access('testcontrol'))
{
\\code.....
} //Access control END
How can i put such control over views? I am not echoing views, but loading pure html as view,
Can be done with passing some variables? How?
Thanks,
Try like this,
Store the user type in session on ligin(like admin, subadmin,...)
Suppose i want to show a button by name create sub admin only to user, i would do like this,
<?php if($this->session->userdata("user_type") == "admin") { ?>
<button>Add Sub Admin</button>
<?php } ?>
something like this.
Since session variables are global variables you can use them across any pages( even in views).
So while you allowing user to login,
set one session variable to appropriate value according to user type.
may be something like this
$this->session->set_userdata('privilege', 'user');
$this->session->set_userdata('privilege', 'manager');
then in the view,
you can check them like,
<html>
<body>
<div>
<?php if($this->session->userdata("privilege") == "manager") { ?>
<button type="button" class="btn yellow" id="delete_group_btn"><i class="fa fa-trash"></i> Delete</button>
<?php } ?>
</div>
.
.
.
</body>
<html>
above code only display the delete button to the user type manager.
like that you can check the condition.
Hey guys I am a little confused, I have a listing page that displays all of my rows in my SQL table.
An example of what I am developing can be seen here: http://www.drivencarsales.co.uk/used-cars.php
So as you can see I have developed a listing page that fetches all of the table rows, each row is equivalent to one vehicle.
I now want to let users filter the results using the form to the left, I was going to use AJAX originally however I feel as if it would take way to long to learn how to develop it that way.
Here is the code setup I am using to achieve the example I have shown:
<?php
include('database.php');
try {
$results = $db->query("SELECT Make, Model, Colour, FuelType, Year, Mileage, Bodytype, Doors, Variant, EngineSize, Price, Transmission, PictureRefs, ServiceHistory, PreviousOwners, Options, FourWheelDrive FROM import ORDER BY Make ASC");
} catch (Exception $e) {
echo "Error.";
exit;
}
try {
$filterres = $db->query("SELECT DISTINCT Make FROM import ORDER BY Make ASC");
} catch (Exception $e) {
echo "Error.";
exit;
}
?>
As you can see the first block of code has two SQL selectors, the $results is used to fetch the whole table and list all vehicles.
The second block is used to display the 'Make' column for the form.
This block of code is the actual form:
<form>
<select class="form-control select-box" name="">
<option value="make-any">Make (Any)</option>
<?php while($make = $filterres->fetch(PDO::FETCH_ASSOC))
{
echo '
<option value="">'.$make["Make"].'</option>
';
} ?>
</select>
<button href="#" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars
</button>
</form>
As you can see this block is using a while loop to display all of the 'Make's' in the 'Make' column and uses a DISTINCT clause so that it doesn't show identical options.
Here is the block that lists the results to the page:
<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
{
echo '
<div class="listing-container">
<h3 class="model-listing-title clearfix">'.$row["Make"].' '.$row["Model"].' '.$row["Variant"].'</h3>
<h3 class="price-listing">£'.number_format($row['Price']).'</h3>
</div>
<div class="listing-container-spec">
<img src="'.(explode(',', $row["PictureRefs"])[0]).'" class="stock-img-finder"/>
<div class="ul-listing-container">
<ul class="overwrite-btstrp-ul">
<li class="diesel-svg list-svg">'.$row["FuelType"].'</li>
<li class="saloon-svg list-svg">'.$row["Bodytype"].'</li>
<li class="gear-svg list-svg">'.$row["Transmission"].'</li>
<li class="color-svg list-svg">'.$row["Colour"].'</li>
</ul>
</div>
<ul class="overwrite-btstrp-ul other-specs-ul h4-style">
<li>Mileage: '.number_format($row["Mileage"]).'</li>
<li>Engine size: '.$row["EngineSize"].'cc</li>
</ul>
<button href="#" class="btn h4-style checked-btn hover-listing-btn"><span class="glyphicon glyphicon-ok"></span> History checked
</button>
<button href="#" class="btn h4-style more-details-btn hover-listing-btn tst-mre-btn"><span class="glyphicon glyphicon-list"></span> More details
</button>
<button href="#" class="btn h4-style test-drive-btn hover-listing-btn tst-mre-btn"><span class="test-drive-glyph"></span> Test drive
</button>
<h4 class="h4-style listing-photos-count"><span class="glyphicon glyphicon-camera"></span> 5 More photos</h4>
</div>
';
} ?>
So down to my question... How can I filter these results displayed in the listing block using the select element, when a user selects a 'Make' from the select element I want them to be able to submit the form and return all rows in the SQL table containing the same 'Make' string and hide other rows that are false.
Any ideas how I can achieve this or any easier ways?
Thanks
My best guess is for you to use JS to submit the form again, when select-option is selected, with a parameter stating the model.
Then in your PHP file, you should look for that parameter and edit the query-string accordingly.
see this answer on how to submit a form when option is selected.
This answer will show you how to handle parameters in PHP (using GET method)