How to enable a tree view in php page? - php

I have a mysql database. In a table i have some entries of uploaded file. I want to display that on a php page. But those files are categorized by one column. So what i want is to have that categories name on that page. In front of each name i want to have a button. Initially when page loads it should not show the files. when some one click on a button then that corresponding category should expand.
But i am unable to do it. I am giving you a link to sample code.
HTML code
<span class="show"></span>
<p>category</p>
<table width='100%' class='tree'>
<tr>
<td width='70%'><b>Name</b></td>
<td width='30%'><b>Last Updated</b></td>
</tr>
</table>
http://jsfiddle.net/SumitRathore/FqcSM/
Edit
this is my sample html code where i am showing the tables. This code have some variables name and sql query don't go for that. I have found the answer here http://jsfiddle.net/JGLaa/2/ but that is not working here. I dont know why?
<span class="show"></span>
<p><b>category<b></p><br/>
<table width='100%' class='tree'>
<tr>
<td width='35%'><b>Name</b></td>
<td width='25%'><b>Last Updated</b></td>
</tr>
while($row = $result_sql->fetch_assoc())
{
<tr>
<td width='50%'><a href='http://127.0.0.1/wordpress/?page_id=464&name_file={$row['name']}&cat={$cat}&sec={$sec}' target='_blank'>{$row['title']}</a></td>
<td width='25%'>{$row['created']}</td>
<td width='25%'><input type='checkbox' class='checkbox'><input type='button' class= 'input1 input_box' value='delete' name='delete' id= '{$row['id']}'><input type='button' class='input2 input_box2' value='edit' name='edit' id= '{$row['id']}'></td>
</tr>
}
</table>

Your issue is that .next() searches the siblings, so it does not find a table. Try traversing up to the buttons parent (span), and then find the span's sibling table -
$(this).parent().nextAll('table:first').find('td').toggle();
updated JSFiddle - http://jsfiddle.net/JGLaa/1/
here is an updated JSFiddle that has multiple buttons/tables - http://jsfiddle.net/JGLaa/2/

Looking at you code the use of the next selector is flawed. From Jquery.com:
.next( [selector ] ) Description:
Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.
I would suggest to sourround your button and table with a div and use the following code:
$(document).ready(function(){
$(".show").html("<input type='button' value='Show All' class='treebtn'>");
$('.tree td').hide();
$('.treebtn ').click( function() {
$(this).parent().parent().find('table td').toggle();
if (this.value=="Show All") this.value = "Hide All";
else this.value = "Show All";
});
});
http://jsfiddle.net/FqcSM/3/

$(document).ready(function(){
$(".show").html("<input type='button' value='Show All' class='treebtn'>");
$('.tree').hide();
$('.treebtn ').on('click', function() {
$(this).parent('span').next('table').toggle();
if (this.value=="Show All") this.value = "Hide All";
else this.value = "Show All";
});
});
fiddle

Related

loading No. in mysql database into a textbox

The following code segment is used by me to load the contents in the database as a table!(each row has a button with value load!
while($row=mysqli_fetch_array($result))
{
echo"
<tr id='rowNum'>
<td >".$row['No.']."</td>
<td >".$row['NIC']."</td>
<td >".$row['DP']."</td>
<td >".$row['DPTime']."</td>
<td >".$row['Telephone']."</td>
<td> <input type='button' id='load' class='btn btn-success' value='Load number' disabled=' '></td>
</tr>";
}
I also have the following text box on the same web page(outside the above table)!
echo "<input type=text name="display number">"
when i press the load button of the the relevant row of the table i want to display the No. of that row in the text box! how can i achieve this?
There are several problems with the code you posted. I'll do my best to get you on your way... You will want to do what you have described with JavaScript which runs on the client. Many people use libraries these days to work with JavaScript but to avoid added confusion I will recommend looking into jQuery and write my solution in the native code.
while($row=mysqli_fetch_array($result)) {
echo("
<tr id='rowNum".$row['No.']."'>
<td>".$row['No.']."</td>
<td>".$row['NIC']."</td>
<td>".$row['DP']."</td>
<td>".$row['DPTime']."</td>
<td>".$row['Telephone']."</td>
<td>
<input type='button' id='load".$row['No.']."' onClick='javascript:document.getElementById(\"displayNumber\").value=this.parentNode.parentNode.firstChild.innerHTML;' class='btn btn-success' value='Load Number'>
</td>
</tr>"
);
}
One big talking point is that you need to make sure that if you assign an id to any HTML element, unless it is a collection then that ID must be unique, otherwise your HTML will be just as invalid as your id. Also: Don't use spaces in names or ids. Just don't.
Finally, to fix up the input which is outside the table where you want the number to dynamically appear:
echo("<input type='text' name='displayNumber' id='displayNumber'>");

Get hidden input value from clicked form on page

I have a list of users registered to the application, for the admin there is an option to delete any user registered. I have the list being populated from the database with PHP.
So every one of the users in this list gets a tr row in this table that has all the users in it. At the right column I have a button which only the admin sees on this table of users, and he can delete that specific user by clicking on that 'delete' button.
I would like that before deleting occurs, which is working fine, there should be a pop up alerting to the admin asking "Are you sure you would like to delete "blank" from the system?" with blank having the name of the user the admin clicked the delete button by.
Now I do have the name of the user in a hidden input field attached to the delete button, in other words I am getting a name coming out in that sentence, the problem though is that the same name of the first person on the list is showing up when I click on any one of the delete buttons on the users list. So to clarify although the deleting is happening to the correct user, so it doe's delete the user I would like to delete, the name in the confirmation alert is showing always with the name of the first user.
I believe this is a problem with the way I am writing the jquery code, so any assistance with this would be greatly appreciated, I am sitting on this for a while already trying to find an answer online for this.
So to sum it up in short:
I have a couple of "forms" on the page (i.e. meaning "delete" buttons that causes a "form" with hidden input information of the user that we would like to have deleted, is sent to the back-end in order for the back-end to know which user to delete.)
The delete function is working just fine - deleting the desired user.
However the alert function is not populating the desired message to the admin, since the name of the user in the message doe's not change from user to user, and it's grabbing always the name of the first user on the users list.
Here is the code for the users list:
<table class="table table-striped table-bordered">
<thead class="dark_grey">
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
<th>User Level</th>
<th>Actions</th>
</thead>
<tbody>
<?php
foreach ($user_rows as $row)
{
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td><a href='/ci/user/user_profile/?={$row['id']}'>{$row['first_name']} {$row['last_name']}</a></td>";
echo "<td>{$row['email']}</td>";
echo "<td>{$row['created_at_time']}". " " ."{$row['created_at_date']}</td>";
if($row['user_level'] != '9')
{
$row['user_level'] = 'normal';
echo "<td>{$row['user_level']}</td>";
}
else
{
$row['user_level'] = 'admin';
echo "<td>{$row['user_level']}</td>";
}
echo "<td class='last_td'>
<form class='float_left' action='/ci/user/edit_user' method='post'>
<input type='hidden' name='email' value='{$row['email']}'/>
<input class='btn btn-success' type='submit' value='Edit' />
</form>
**<form class='delete' action='/ci/user/delete_user' method='post'>
<input type='hidden' name='email' value='{$row['email']}'/>
<input type='hidden' name='name' value='{$row['first_name']} {$row['last_name']}' />
<input class='btn btn-danger' type='submit' value='Delete' />
</form>**
</td>";
echo "</tr>";
}
?>
</tbody>
</table>
Here is the jquery code:
<script type="text/javascript">
$(document).ready(function () {
$('.delete').submit(function () {
var name = $('input[name="name"]').val();
alert("Are you sure you would like to delete " + name + " from the system?");
return false; //I just have this here so the delete didn't take place when testing the alert function.
});
});
</script>
So I know somehow I need to have the alert on the current clicked (".delete") but when I tried using $(this) it didn't really work. Very possible I was using it wrong.
If someone can show me the right way to write the jquery to be able to get the current clicked "delete" button's hidden input name value in the message that would be great!
Thanks in advance for your help!
You need to make your selector specific like $(this).find('input[name="name"]').val().
Try:
$('.delete').submit(function(){
var name = $(this).find('input[name="name"]').val();
alert("Are you sure you would like to delete " + name + " from the system?");
return false; //I just have this here so the delete didn't take place when testing the alert function.
});
When you do $('input[name="name"]').val() it matches more than one input name=name so jquery method val() will return the value of only the first element in the collection, and hence the behavior that you are seeing.
here is a snippet from val() from jquery code
val: function( value ) {
var ret, hooks, isFunction,
elem = this[0]; //<--- It just takes first element in the collection
if ( !arguments.length ) {
if ( elem ) {
...........
I think I just finally found an answer to my question:
Here is the jquery code that worked:
<script type="text/javascript">
$(document).ready(function(){
$('.delete').submit(function(){
var name = $(this).find('input[name="name"]').val();
alert("Are you sure you would like to delete " + name + " from the system?");
});
});
</script>
I got the answer after finding this post:
jQuery get value from hidden input in column
Thanks for your help stackoverflow! :)

How to stop my jquery to listen to all rows of data?

I'm trying to make a dropdown when the reject checkbox is ticked. I have done this simply with Jquery but however because I need the function on all of the data that is pulled in, it copies the classes to the next row and so on, so when i check the reject button it opens all the dropdown text areas to input a reason. I was wondering how i would go about making this for an unlimited amount of data????? so it only ever opens one dropdown textarea.
I have read online that I can do this with php parenting? As my PHP skills are weak to say the least is there any simpler ways of going about this?
HTML
Name
Username
Email
Verify
Reject
if ($supplier['Id'] != '3') { ?>
<tr class="unverifiedSuppliers">
<td width="150"><?php echo $supplier['Name']; ?></td>
<td width="150"><?php echo $supplier['Username']; ?></td>
<td width="250"><?php echo $supplier['Email']; ?></td>
<td width="40"><input type='checkbox' name='verifySupplier[]' value='<?php echo $supplier['Id']; ?>' /></td>
<td width="40"><input class="checked" type='checkbox' name='rejectSupplier[]' value='<?php echo $supplier['Id']; ?>' /></td>
</tr>
<tr class="dropdown" style="display: none;">
<td colspan="5">
<label>Reason:</label>
<textarea name="rejectReason[<?php echo $supplier['Id']; ?>]"></textarea>
</td>
</tr>
<?php } } ?>
</table>
Jquery
$(function() {
$('.checked').change(function() {
$('.dropdown').toggle();
});
});
Try :
$('.checked').change(function() {
$(this).closest('tr').next('.dropdown').toggle();
});
$(this) is the DOM element you changed.
.closest('tr') is looking for the closest parent with the jQuery selector 'tr', wich mean the closest <tr></tr>.
.next('.dropdown') is looking for the immediate next sibling with the jQuery selector '.dropdown'.
Side note - using the selector here is facultative, it will just return an empty element if the next element doesnt have the class dropdown.

Editing a html table using jquery,php and saving records to mysql

I have a html table(grid) which displays a few records for me.I want it to be editable, i.e user can edit the values and save them on pressing enter.
My table is something like this.I display records dynamically using php.
<a class="grayBucketBtn noMargin none" id="unpublish" href="#.">Unpublish</a>
<a class="grayBucketBtn" id="publish" href="#.">Publish</a>
<a class="grayBucketBtn" id="delete" href="#.">Delete</a>
<a class="grayBucketBtn" id="modify" href="#.">Modify</a>
<?php while ()//loop through ?>
<tr>
<td class="tableRadioBtn"><input type="checkbox" class="checkRowBody" id="checkRowBody" name="check"/></td>
<td class="tableShape">Round</td>
<td class="tableCarst">0.30</td>
<td class="tableColor">j</td>
<td class="tableClarity">SI1</td>
<td class="tableDimension">4.35x4.33x2.62mm</td>
<td class="tableDeptd">60.3%</td>
<td class="tableTablePer">60.3%</td>
<td class="tablePolish">Excellent</td>
<td class="tableSymmetry">Excellent</td>
<td class="tableCut">Very Good</td>
</tr>
<?php } ?>
Each row(tr) has a check box associated.If I check the check box,I get a edit button.When I click on the edit button,the selected row will turn into editable.So I want a function on the edit button,
$("#modify").click(function(){
//check if only one check box is selected.
//make it editable.
//save the content on pressing enter of the edited row.
});
I went through some questions but did not get a solution as most suggest some plugins which don't meet my requirements.So,some help would be useful.
Thanks for the time
This should cover turning them from text to inputs and back to text
$('#modify').click(function(){
$.each($(".checkRowBody:checked"),function(){
$.each($(this).parent('td').parent('tr').children('td:not(.tableRadioBtn)'),function() {
$(this).html('<input type="text" value="'+$(this).text()+'">');
});
});
});​​​​​​​​​
$('input[type="text"]').live('keyup',function(event) {
if(event.keyCode == '13') {
// do $.post() here
$.each($('input[type="text"]'),function(){
$(this).parent('td').html($(this).val());
});
}
});
​
​
When using checkboxes the user assumes more than one can be selected, if you want only one each time then just use radio buttons
I can't give you a complete solution but I can give you a direction:
First change the markup like this:
<tr>
<td class="tableRadioBtn"><input type="checkbox" class="checkRowBody" id="checkRowBody" name="check"/></td>
<td class="tableShape">Round<input class="hidden" value="Round" name="shape"/></td>
<td class="tableCarst">0.30 <input class="hidden" value="0.30" name="tableCarst"/></td>
...
//Do the same for all the columns
</tr>
Define the hidden class to display:none so all the inputs are hidden.
Once the user clicks a row, you remove the text of all the td elements and remove the hidden class from all the inputs:
$(".tableRadioBtn").click(function(){
//find the parent tr, then find all td's under it, empty the text and remove hidden class
$(this).closest('tr').addClass('editable').find('td').each(function(){
$(this).text('').removeClass('hidden');
});
});
//set a keypress event to detect enter
$(document).keypress(function(){
//if enter was pressed , hide input and set text
if(e.which == 13) {
var $editable = $('.editable');
$editable.find('input').addClass('hidden');
$editable.find('td').each(function(){
//set the text back
$(this).text($(this).find('input').val());
});
//post data via ajax.
}
}
Please note that i haven't tested this code so there might be some mistakes there, but this is a possible solution.
UPDATE:
In order to detect if more than one checkbox is checked use this:
if ($(':checked').length > 1){//do something}
So you want to make your selections and then invoke an action on the checked rows?
$('#delete').click(function() {
$.each($('.checkRowBody:checked').parent('td').parent('tr'),function() {
// probably want to carry out a $.post() here to delete each row using an identifier for the rows related record.
//I suggest applying an id or other attribute to the tr element and using that to pass a value with your $.post() data.
$(this).hide();
});
});

Changing the src content of iframe dynamically

I am making a page which do a search result. To brief the scene is that I have a login page which will direct you to another page (let's say homepage) This page contains 1 drop-down box ( and ), 1 textbox, 1 button and 1 iframe which contains the page where I have my table. Now I have a list of user which is displayed on that table having a header of firstname, lastname and middlename which is all the data is displayed on at startup Now I selected the firstname from the drop-down box then key in my name which should result to a filter for those who have the same name as mine. I used a filter on my table page in which I add the parameters e.g. //localhost/test/table.php?fname=test
Now the thing is that the iframe should be the only one that will change making my homepage intact. Now the iframe src will be changed on click based on what I selected on the drop-down box as I already said without using a form submit and the value will be based on the text on the text box. And also if possible without using http:// on the src link.
Well I almost done the part of onclick will change the iframe page using this script:
<script type='text/javascript'>
function goTo(){
top.table.location = 'http://localhost/test/home.php;
}
</script>
It should be like this:
$dropdownvalue = dropdownselectedtext
$textvalue = valueoftextbox
//localhost/test/table.php?$dropdownvalue$textvalue
Well here it is my first work. commented for better understanding. Assume that '#' followed by text means some value. For reference.
$acct_no = "#LOG_IN_ID OF USER";
echo "<script type='text/javascript'>
function goTo(){
top.table.src = '#SOMEURLHERE';
}
</script>";
echo "<table border='0' align='center' bgcolor='#ffffff' width='800px'>
<tr>
<td style='padding-left:30px;padding-top:20px;'>
<h3>SELECTION TEST</h3>
</td>
<td align='right' style='padding-right:30px;' height='120px'>
<select name='filter' id='filter'>
<option value='fname'>fname</option>
<option value='lname'>lname</option>
<option value='mname'>mname</option>
</select>
<input type='text' value='Enter text here' id='textbox'>
<input type='button' value='Search' onClick='goTo();'> //this should give me the filters e.g: #SOMEURLHERE.php?#selectedfiltervalue=#textboxvalue
</td>
</tr>
<tr>
<td colspan='2' align='center'>
<iframe src='table.php?ref=$acct_no' name='table' width='730px' height='300px' frameborder='0'></iframe>
</td>
</tr>
</table>";
I would suggest that you keep the <script> container outside of PHP area.
<script type="text/js" language="javascript">
//Your function goes here
</script
<?php echo "<form name='someName' action='#'>
echo "<table border='0' align='center' bgcolor='#ffffff' width='800px'>
<tr>
<td style='padding-left:30px;padding-top:20px;'>
<h3>SELECTION TEST</h3>
</td>
<td align='right' style='padding-right:30px;' height='120px'>
<select name='filter' id='filter'>
<option value='fname'>fname</option>
<option value='lname'>lname</option>
<option value='mname'>mname</option>
</select>
<input type='text' value='Enter text here' id='textbox'>
<input type='button' value='Search' onClick='javascript: return goTo(\"this.parent.filter.value, this.parent.textbox.value\");'> //this should give me the filters e.g: #SOMEURLHERE.php?#selectedfiltervalue=#textboxvalue
</td>
</tr>
<tr>
<td colspan='2' align='center'>
<iframe src='' name='table' width='730px' height='300px' frameborder='0'></iframe>
</td>
</tr>
</table></form>";?>
You didn't use a <form> tag in your whole section, so managing fields is difficult. Also, try the javascript I defined in previous post.
Try using this in onclick/onchange handler in your drop down box(for each value):
javascript: return goto(\"$dropdownvalue\", \"$textvalue\");
And, to incorporate it
<script type='text/javascript'>
function goto(var1, var2){
finLink = "http://localhost/test/table.php?" + var1 + var2
top.table.location = finLink; //Not sure if it will be top.table.location or top.table.src
}
</script>
You can use jquery for this:
$('#myiframeid').attr('src', 'http://some.url.here');

Categories