Updating table information formatting based on a checkbox value - php

I have a table that contains a list of items taken from a SQL database using php. There is a checkbox, Done, that I want to tick off as the list is completed. I want the text of the associated item to update to grey or strikethrough as the box is ticked off.
I tried using one of the methods as above:
In CSS
:checked + span {
font-weight: bold;
}
In HTML
<table cellpadding="1" cellspacing="1" class="db-table">
<td><input type="checkbox" name="Done1" value="1" <?php if ($info['Done1'] ==1) echo 'checked'; ?>></td>
<span>
<td><?php echo $info['Seq1'];?></td>
<td><?php echo $info['Size1'];?></td>
</span>
</table>
It does not work. The table has further CSS formatting based on the class.
I take the same data, put it outside the table tags, and it works just fine.
Any suggestions of what to do here? Is the table class somehow overriding what I want to do?

Your markup is invalid, you can't nest a span directly within a table like that and you're also missing the <tr> tags.
However, even with valid markup, what you're asking isn't possible with CSS alone as it doesn't provide any means (yet) for selecting parent elements, which would need to be done here in order to target the sibling cells or parent row.
So, you're going to have to resort to a bit of JavaScript, toggling a class on the parent row each time a checkbox is clicked.
Before we get to the Snippet, here's what you'd need to do with your PHP in order to have the .done class applied to the table rows that require it initially:
<table cellpadding="1" cellspacing="1" class="db-table">
<tr<?php if ($info['Done1'] ==1) echo ' class="done"';?>>
<td>
<input<?php if ($info['Done1'] ==1) echo ' checked="checked"';?> type="checkbox" name="Done1" value="1">
</td>
<td><?php echo $info['Seq1'];?></td>
<td><?php echo $info['Size1'];?></td>
</tr>
</table>
And here's a working Snippet to demonstrate the solution:
var inputs=document.querySelector(".db-table").getElementsByTagName("input"),x=inputs.length;
while(x--)
inputs[x].addEventListener("click",function(){
this.parentNode.parentNode.classList.toggle("done");
},0);
.done td{
font-weight:bold;
}
/* Just some prettifying */table{border:1px solid #999;border-spacing:1px;font-family:arial;font-size:14px;margin:0 auto;width:50%;}td{line-height:20px;padding:5px;}td:first-child{text-align:center;width:20px;}tr:nth-child(odd) td{background:#eee;}tr:nth-child(even) td{background:#ddd;}
<table cellpadding="1" cellspacing="1" class="db-table">
<tr>
<td>
<input type="checkbox" name="Done1" value="1">
</td>
<td>text</td>
<td>text</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Done2" value="1">
</td>
<td>text</td>
<td>text</td>
</tr>
<tr>
<td>
<input type="checkbox" name="Done3" value="1">
</td>
<td>text</td>
<td>text</td>
</tr>
</table>

Here is a working example. You're CSS will never show because your span is never inside of your checkbox (:check).
At this point you will start getting into DOM Elements. The following example uses JavaScript/jQuery. I set up a listener to listen for checkbox changes (ie is it checked or not). If the checkbox is checked then I add a "done" class onto .info classes.
In order to know which row I'm dealing with I've added to HTML tags. 1, I've added an id to the tr. This should always be unique to the row. 2, I've added a data-row to the checkbox element. This is used to identify the row.
JavaScript is great to use in this instance because you are wanting to change elements on the fly that are outside of the hierarchy that CSS allows.
It may be a little over the top, but it definitely gets the job done.
<?php
$info['Done1'] = 0;
$info['Seq1'] = 'Hello';
$info['Size1'] = 'World';
?>
<style type="text/css">
.done{color:#ccc;}
</style>
<table cellpadding="1" cellspacing="1" class="db-table">
<tr id="row1">
<td><input type="checkbox" name="Done1" value="1" data-row="1" <?php if ($info['Done1'] ==1) echo 'checked'; ?>></td>
<td class="info"><?php echo $info['Seq1'];?></td>
<td class="info"><?php echo $info['Size1'];?></td>
</tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('input[name="Done1"]').change(function(){
if( this.checked ){
$('#row'+$(this).data('row')+' .info').addClass('done');
}else{
$('#row'+$(this).data('row')+' .info').removeClass('done');
}
});
});
</script>

Related

How to get submit button in different table rows to perform different SQL queries?

So I'm having a problem that I've been trying to work on for quite a while now but I don't really know how to do it.
I have multiple submit buttons in a table (one per row). How do I get each submit button to perform a different SQL task? To explain further...
So I have a table that looks something like this (This is the HTML code from View Source, without the underlying PHP code that generates the table contents):
<table class='table table-striped'>
<thead>
<tr>
<th>Date/Time</th>
<th>Exchange</th>
<th>Amount</th>
<th>Current reversed ex. rate</th>
<th>Arbitrage opportunity</th>
<th>Change</th>
</tr>
</thead>
<tbody>
<form action='arbitrage.php' method='post'>
<tr>
<td> 2015-12-24 22:32:50 </td>
<td>JPY to GBP</td>
<td>¥18,000.00 --> £100.80</td>
<td>179.48 GBP / JPY</td>
<td><font color='green'>¥<b>91.584</b> <b><u>PROFIT</u></b> on original ¥18,000.00 if you exchange now. <br/> (Total after change: ¥<b>18,091.584</b>).
</font></td>
<td><button type='submit'>Change</button></td>
</tr></form></tbody>
<tbody>
<form action='arbitrage.php' method='post'>
<tr>
<td> 2015-12-24 22:33:07</td>
<td>GBP to EUR</td>
<td>£15,000.00 --> €20,359.50</td>
<td>0.7365 EUR / GBP</td>
<td><font color='red'>£<b>5.228</b> <b><u>LOSS</u></b> on original £15,000.00 if you exchange now. <br> (Total after change: £<b>14,994.772</b>).
</font></td>
<td><button type='submit'>Change</button></td>
</tr></form></tbody>
To understand even better, this is an image of what the table looks like:
http://i.imgur.com/niyuT14.jpg
Now, when the 'Change' button is pressed, I want the information FROM THAT SPECIFIC ROW to be deleted from my SQL table called 'history'.
At the moment, I have tried things such as:
$query = "DELETE FROM history WHERE currency = ?", $row["currency"];
//row["currency"] is the stuff below the 'Exchange' header in the table.
When I press the 'Change' button however, all the existing rows of the HTML table get deleted from the 'history' SQL table. How do I get only one row to be deleted (the one where the button is pressed) while the others stay exactly where they are?
Please help!
Many thanks guys!
you can add hidden inputs with name='currency' for each row or more easily you can use ajax to submit your query
Change the form action form:
<form action='arbitrage.php' method='post'>
To
<form action='arbitrage.php?id=99' method='post'>
And update your query as:
$id = isset($_GET['id']) ? $_GET['id'] : ''; //This will give you--> id = 99;
$query = "DELETE FROM history WHERE currency = ?", $id;
The value 99 will be of course dynamic from db table.
I see that values are hardcoded and for both the buttons or forms you are using the same arbitrage.php action page. Use different pages for each form or button and write your mysql code as per your wish. And if it is dynamic then you will have to use the JS onClick event.
Instead of having so many forms, have just one. And then in the arbitrage.php, delete the submitted row and come back to the same page, may be..
<form action='arbitrage.php' name='changeForm' method='post'>
<input type="hidden" name="currency">
<table class='table table-striped'>
<thead>
<tr>
<th>Date/Time</th>
<th>Exchange</th>
<th>Amount</th>
<th>Current reversed ex. rate</th>
<th>Arbitrage opportunity</th>
<th>Change</th>
</tr>
</thead>
<tbody>
<tr>
<td> 2015-12-24 22:32:50 </td>
<td>JPY to GBP</td>
<td>¥18,000.00 --> £100.80</td>
<td>179.48 GBP / JPY</td>
<td><font color='green'>¥<b>91.584</b> <b><u>PROFIT</u></b> on original ¥18,000.00 if you exchange now. <br/> (Total after change: ¥<b>18,091.584</b>).
</font></td>
<td><button type='button' onclick="chg_currency('GBP / JPY');">Change</button></td>
</tr></tbody>
<tbody>
<tr>
<td> 2015-12-24 22:33:07</td>
<td>GBP to EUR</td>
<td>£15,000.00 --> €20,359.50</td>
<td>0.7365 EUR / GBP</td>
<td><font color='red'>£<b>5.228</b> <b><u>LOSS</u></b> on original £15,000.00 if you exchange now. <br> (Total after change: £<b>14,994.772</b>).
</font></td>
<td><button type='button' onclick="chg_currency('EUR / GBP');">Change</button></td>
</tr></tbody>
</table>
</form>
<script>
function chg_currency(c) {
document.forms['changeForm'].currency.value = c;
document.foms['changeForm'].submit();
}
</script>

How to echo a variable into a div in php?

Im trying to echo a variable into an already existing DIV within a table.
EG:
<table>
<tr>
<td class="error"> </td>
</tr>
</table>
<?php
echo "<td class='error'>".$variable."</td>";
?>
Note: the error td tag is already style and positioned within a table.
The method above causes a lone td tag to be created and added to the top of
the page.
I realise it's probably best to echo php in HTML rather than vice versa but
I need echo HTML in this instance.
Is this possible ?
The simplest solution, if I am understanding correctly:
<table>
<tr>
<td class="error"><?php echo $variable; ?></td>
</tr>
</table>
You have to echo in-place.

Disable button in a row table

I have a table with each rows have a Send Button:
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Address</th>
<th>Button</th>
</tr>
</thead>
<tbody>
<tr>
<td id="name"></td>
<td>20</td>
<td>Street AA</td>
<td><a name="sendName" id="sendId" href="www.google.com">Send</a>
</td>
</tr>
<tr>
<td>Mr.XX</td>
<td>20</td>
<td>Street XX</td>
<td><a name="sendName" id="sendId" href="www.google.com">Send</a>
</td>
</tr>
<tr>
<td>Mr.YY</td>
<td>20</td>
<td>Street YY</td>
<td><a name="sendName" id="sendId" href="www.google.com">Send</a>
</td>
</tr>
</tbody>
</table>
Preview
All values in that table I get from database. So what I want to do is, when the NAME value is empty, the send button will be disabled based on its row table.
Since you have no input values i do not quite understand why you want to use javascript for a task like this. If the value is empty simply do not display the link at all. Or remove the href-attribute add a css class and style the link different.
//disabled link
<a name="sendName" id="sendId">Send</a>
I don't know if this was a copy/paste error but you use the same ID sendID on all links.
You really need to not put the button there if the database returns an empty name instead of removing it after. Something like
<a name="sendName" id="<? echo "id".&name; ?>"
<? if (&name!="") echo 'href="#"'; ?>>Send</a>
or
<? if (&name=="") { ?>
<span>Send</span>
<? } else { ?>
<a name="sendName" id="<? echo "id".&name"; ?>" href="#">Send</a>
<? } ?>
Try the Below example code on send link :
<?php if($row['NAME']==''){ echo 'Send' } else{?>
<a name="sendName" id="sendId" href="www.google.com">Send</a>
<?php }?>
you can try this jquery code for that you you need to include jquery library
<script>
$(document).ready(function(e){
var objTR = $("table tr");
for(var i = 0; i < objTR.length; i++)
{
var objCols = $(objTR[i]).children();
var name = objCols[0].innerHTML;
if(name == "")
$(objCols[3]).find("a").removeAttr("href");
}
});
</script>

How to display filled information of appended row in another page? [duplicate]

This question already has answers here:
why the appended row's data didn't displayed after being submitted?
(2 answers)
Closed 9 years ago.
This page got a button ADD to append the row.Once the appended row is filled,when submit,it will connect/link to another page ,then the filled information will display at that page.But the problem is how to display the filled information of appended row on 2nd page had been done in 1st page by using PHP.(Sorry for my poor English and hope for understand).
<table width="600px" id="project">
<tr>
<td>1</td>
<td><textarea name="pro_1" cols="100" rows="2"></textarea></td>
</tr>
<tr>
<td>2</td>
<td><textarea name="pro_2" cols="100" rows="2"></textarea></td>
</tr>
<tr>
<td>3</td>
<td><textarea name="pro_3" cols="100" rows="2"></textarea></td>
</tr>
</table>
<input id="addbtn" type="button" name="addbtn" value="ADD">
Jquery script(for append the row):
$(document).ready(function() {
$("#addbtn").click(function(){
var num=parseInt($("#project tr:last").text());
num+=1;
$("#project").append("<tr><td>"+num+"</td><td><textarea cols='100' rows='2'></textarea></td></tr>");
});
PHP source code(for 2nd page):
<table width="600px" id="pub">
<tr>
<td>1</td>
<td><?php echo $_post["pro_1"]; ?></td>
</tr>
<tr>
<td>2</td>
<td><?php echo $_post["pro_2"]; ?></td>
</tr>
<tr>
<td>3</td>
<td><?php echo $_post["pro_3"]; ?></td>
</tr>
</table>
Give the dynamically-added textarea a name, then you following page will see it as a parameter & can render it back out in the HTML.
In the Javascript: (PLEASE don't try and munge all your code, on one line!):
$("#project").append("<tr>);
$("#project").append("<td>"+num);
$("#project").append("<td><textarea name='pro_"+num+"' cols='100' rows='2'></textarea>");
See how a pinch of readability, makes it comprehensible? I'm following the HTML spec that says </td> and </tr> are OPTIONAL, not mandatory -- I never emit them myself.
And in your PHP second page, you need to pick up the correct parameter name. (You're looking for pub_N, not pro_N which you appear to post under).
Tidy your code up, get your names right.
Then put a PHP loop around it, to render Project Names with ascending numbers until it runs out of parameters. My PHP code is rough, but..
<?php
$rowNum = 1;
while (true) {
$rowValue = $_post["pro_"+$rowNum];
if (! isset($rowValue))
break; // Reached the end.
?>
<tr>
<td><?= htmlentities( $rowValue) ?></td>
<?php
}
?>
Hope this helps!

How to make my output text scrollable using PHP?

The text that I am trying to echo from $data is way too long and it is going off the screen beyond the boundaries of the table. In addition to that all the text is getting displayed without line breaks (or blank lines) or proper spacing.
My simple PHP code:
<div id="sampleid1" class="tabcontent" style="margin-left:48px;">
<table width="510" border="0" cellspacing="4" cellpadding="4" class="SampleClass">
<tr>
<td>
<?php echo Sample1_LABEL;?>
</td>
<th align="left"><strong>:</strong>
</th>
<td>
<?php echo $data[ 'Sample1'];?>
</td>
</tr>
<tr>
<td>
<?php echo Sample2_LABEL;?>
</td>
<th align="left"><strong>:</strong>
</th>
<td>
<?php echo $data[ 'Sample2'];?>
</td>
</tr>
</table>
</div>
In Summary:
I need the text that I am retrieving using the $data to get formatted in such a way that the line breaks are displayed in the output upon echoing.
I need that output text to be scrollable so no text will go off the screen beyond the boundaries.
you need to set CSS to the TD tag where you echo that $data...
<td style="height:150px; overflow-y:scroll;">
My mistake.. TD dont accept overflow so you may do this::
<td style="height:150px"><div style="height:100%; overflow-y:scroll;">..PHPCODE...</div></td>
You could echo a <pre> tag around output where whitespace is significant. Something like:
<?php echo "<pre>".Sample1_LABEL."</pre>";?>
You can try this
<td>
<div style="height:100px; overflow:auto">
<?php echo $data['Sample2'];?>
</div>
</td>
You can also try
echo(nl2br($data['Sample2'])); //converts newline to <br /> html tag

Categories