Writing the code correctly - Javascript [duplicate] - javascript

I came across a problem which I can't solve on my own!
I have a Table which is populated using PHP/MySQL something like
foreach($users as $user){
?> <tr id="<?php echo $user['fName'].
"-".$user['lName'];?>">
<td> <?php
echo $user['fName']." ".$user['lName'];
?> </td>
<?php
for ($i = 0; $i<$count+3; $i++){
?> <td><input class="accessCheck" id="<?php
echo $user['lName']."-".$pagesArray[$i];
?>" type="checkbox"></td>
<?php } ?>
</tr> <?php
}
As you can see table would look something like
fName lName | checkbox | checkbox | checkbox | mainCheckbox
What I need to do is to check/uncheck all checkboxes with mainCheckbox, is there a way to select all rows just from checking this mainCheckbox?
EDIT: How to find class when you select something like input, is there a way of finding a class for this? For example
$("input").click(function(){
});
Is there a way to find a class for this input?

Tyr this:
$('#all').on('click', function(){
$(':checkbox').prop("checked",$(this).is(':checked'));
});
http://jsfiddle.net/Dfzdh/

I think you would need to use jQuery
Look at the sibblings method.
Here is a question close to yours.
OnClick change all sibling table rows jQuery
I havent heard about any "onChange"-equivalent function in PHP yet

Related

How I create a Read More button inside a table?

<?php $counter = 0 ?>
<table>
<?php foreach($attributes as $a): ?>
<tr>
<td><?php $a['name']?></td>
<td><?php $a['Price']?></td>
</tr>
<?php $counter++;
if ($counter > 3) ?> <!-- What to do here? -->
<?php endforeach; ?>
</table>
In a PHP Block like this, I would like to got a Read More button after three tr elements, and when I click the button show all the rest of elements...
Something to do with PHP?
No, it has nothing to do with php. You need JavaScript to implement this if you don't want to reload the whole page.
In PHP you could print the short and the long version into separate divs and then use the following JS-Code on the View-More-Button:
document.getElementById("shortId").style.display = "none";
document.getElementById("longId").style.display = "block";

JQuery Data attributes getting stripped out in Kohana ajax

I'm working on an app where they have gone with jquery data to pass variables to html
it works in one aspect of the site when the data attributes are attached to a tr tag. This code works
<tr class="js-instructions-row documents__table-row
<?=$ix.'row';?><?php //$ix==0 ? 'documents__table-row--active' : '' ?>"
data-product-title="<?= $sheet->name ?>"
data-instructions-image="<?= $serverpath.$thisImage ?>"
data-instructions-file="<?= $serverpath.'Instructions/'.$sheet->file ?>"
>
when I try to put those attributes on a select tag or option tag in another view, it doesn't come through. This code doesn't work.
<?php
foreach($instructions as $ix => $sheet) {
$thisImage = ($sheet->image?$sheet->image:'Image_holder_thumb.png');
?>
<option test="" data-product-title="<?= $sheet->name ?>" data-instructions-image="<?= Kohana::$config->load('aws.s3-baseurl-www-customercare').$thisImage ?>" data-instructions-file="<?= Kohana::$config->load('aws.s3-baseurl-www-customercare').'Instructions/'.$sheet->file ?>" value="<?=$sheet->id?>"><?=$sheet->name?></option>
<?php
}
?>
and offending javascript:
$('.js-product-selector').on('change',function(e){
var selected = $(this).find('option:selected');
console.log(selected.attr('value'))
console.log(selected.data('product-title'));
$(".documents__product-title").text(selected.data('product-title'));
$(".documents__preview img").attr('src',selected.data('instructions-image'));
$(".documents__download").attr('href',selected.data('instructions-file'));
});
value attribute comes through just fine in the log but data-product-title does not
here is how my view is called in the Controller.
$this->response->body(View::factory($this->folder."/instruction-sheets")->set('brands',ORM::factory('Brand')->with('Customercare_Instruction')->find_all())->set('postbrand',$brand));
The view that works is nested inside of a view that is called like this:
$this->page=View::factory($this->folder.'/index');
$this->page->breadcrumb = 'Instruction Sheets';
$this->page->content = View::factory($this->folder."/instruction-sheets")->set('brands',ORM::factory('Brand')->with('Customercare_Instruction')->find_all());
and the sub-view is called like this
<?= View::factory('customer-care/instruction-sheets-filtered')->set('instructions',$instructions)->render() ?>
I appreciate your input.
You wouldn't by any chance be styling your select element with something like Selectize or Select2, would you? That's probably what's stripping the data attributes from your options.

How to implement horizontal navigation through multiple wordpress posts with dynamic content (slideshow-like)?

My client wants a page where one can navigate through all the available Wordpress auctions (I'm using the Auction Plugin by Sitemile). You can see how the page in question looks like here.
I'm using a table inside a div where each table column refers to a single post/auction. Every column must be 1366px wide, and the navigation through columns/posts should be made clicking the left and right yellow arrows. Is it possible? If so, how?
Or is there a better way to do this, not using a table but divs only?
Here's how I'm generating the content:
<div class="box_content">
<table>
<tr>
<?php $i = 0; if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<?php echo "<td style='min-width: 1366px; padding-right: 60px;'>
(... auction content ...)
"?></td>
<?php endforeach; ?>
<?php endif; ?>
</tr>
</table>

Add XHR to a Javascript Function

When I execute the following code, a GET message appears in the Firebug Console however the variable is undefined, and I think that is due to the scope of the variable being limited to the function only.
My goal is to be able to click the table row, and upon clicking the table row, fetch some data from the server related to the record in the table, and then populate part (not all) of a web form, which I will then add more data to, and then submit the form (see image).
Here is the Javascript function which defines a variable called $id as the given rows' data-attribute ('recordID') which itself was been passed into the <tr> tag when the table was generated.
Javascript:
$(document).on('click', 'tr', function() {
//Get the ID from the row clicked
var $id = $(this).data('recordId');
//short-hand
$('#section2').load('data_entry_form.php?id='+id);
});
PHP snippet that includes the data-attribute:
<table = "all_aifs">
<tr>
<th><b>Document ID</b></th>
<th><b>Pubco Name</b></th>
<th><b>Filing Date</b></th>
<th><b>PDF</b></th>
</tr>
<?php foreach($result as $index => $row) : ?>
<tr data-recordId="<?=$row[fee_source_id];?>"
class="<?=$row["match"] ? "match" : "";?>">
<td><?php echo $row[fee_source_id]; ?></td>
<td><?php echo $row[company_name_per_sedar]; ?></td>
<td><?php echo $row[document_filing_date]; ?></td>
<td></td>
</tr>
<? endforeach;?>
</table>
Question: How can I have my web form contain some data related to the row I clicked? I think I need to use AJAX. Also, why is the variable "undefined" in the Console? I think I need it to be defined as the $id variable (which is the recordId).
The variable id is undefined because you never have defined it.
You only defined $id not id.

tr link with php code inside

I'm trying to do something a little weird and I'm kind of lost.
I have this code:
<tr onclick="href='<?php if (isset($_SESSION['id_usuario'])){ echo 'logado.php?'; } else { echo 'index.php?'; } ?>grupo=<?php echo $row_buscainterna['id_grupo']; ?>'">
My <tr> are in hover, so when the user clicks it I would like to perform this "php action"
Can't make it work. Can somebody help me?
What do you think about it?
I think it would be easier to put a link inside your tr tag, and style it to make it the size of the tr.
You would like to use code like this:
<tr onclick="window.location.href='<?php if (isset($_SESSION['id_usuario'])){ echo 'logado.php?'; } else { echo 'index.php?'; } ?>grupo=<?php echo $row_buscainterna['id_grupo']; ?>'">
Here is an example
OnClick attribute should contain javascript code. Your example sets global variable href to some URL.
If you want to send the user to this URL, then you shoud use document.location instead of href:
<tr onclick="document.location='<?php if (isset($_SESSION['id_usuario'])){ echo 'logado.php?'; } else { echo 'index.php?'; } ?>grupo=<?php echo $row_buscainterna['id_grupo']; ?>'">

Categories