Jump to content

How do I change tr background color using checkbox AND have that color $_POST


FoxRocks

Recommended Posts

Hello,

 

Thank you for your interest in my problem. I have a basic php form using the table element consisting of over 50 rows and 4 columns. I have 2 check boxes associated with each row that, when checked, changes the background color of that row. One check box toggles red, the other yellow. I've managed to find out how to accomplish that task with the help of some internet searching as I am brand new (read: started yesterday) to the world of PHP.

So. That form, when posted, goes to a table on the same page that displays the results. What I would like to do is have the row color that was selected on the form show up on the posted table. This is do not know how to do and I have been trying to self learn with a lot of failure so far...I'm hoping you can help :)

 

Here is my script:

 

function changeColor(){
if(document.inform.chk1.checked){
document.getElementById("tr1").style.backgroundColor="#FF0000"
} 
else if(document.inform.chk2.checked){
document.getElementById("tr1").style.backgroundColor="#FFFF00"
} 
else { document.getElementById("tr1").style.backgroundColor="white"}
}

 

Here is my form:

 

<form action="index.php" method="post" name="inform">

<table cellpadding="2" cellspacing="0" border="2">

<tr class="font14white" align="center" style="background:#C0C0C0;">

<td id="cell1input">
Unit
</td>

<td id="cell2input">
Schedule
</td>

<td id="cell3input">
EMMA
</td>

<td id="cell4input">
Status
</td>

</tr>

<tr id="tr1" name="tr1">

<td id="cell1input">
OEQ
</td>

<td id="cell2input">
<input type="text" name="OEQ_Sched" size="15" value="<?php echo $_POST["OEQ_Sched"]; ?>"></input>
</td>

<td id="cell3input">
<input type="text" name="OEQ_EMMA" size="8" value="<?php echo $_POST["OEQ_EMMA"]; ?>"></input>
</td>

<td id="cell4input">
<input type="checkbox" name="chk1" onclick="changeColor()"></input><font style="background-color:#FF0000;">RED</font><br>
<input type="checkbox" name="chk2" onclick="changeColor()"></input><font style="background-color:#FFFF00;">YELLOW</font>
</td>
</tr>
</table>
</form>

 

Here is my table:

 

<table cellpadding="2" cellspacing="0" border="2">

<tr class=".font14black" align="center" style="background:#FFF;" height="18">

<td>
Unit
</td>

<td>
SCHEDULE
</td>

<td>
EMMA
</td>

</tr>

<tr>

<td id="cell1display">
OEQ
</td>

<td id="cell2display">
<?php echo $_POST["OEQ_Sched"]; ?>
</td>

<td id="cell3display">
<?php echo $_POST["OEQ_EMMA"]; ?>
</td>

</tr>

</table>

 

Thank you for any help you can provide.

 

~FOX~

Link to comment
Share on other sites

First, you would need to use a radio button since the user can only select one option and pass the object to the function because I assume you have more than on table row

<input type="radio" value="#FF0000" onclick="changeColor(this);" name="Table_Row_1" />
<input type="radio" value="#FF0000" onclick="changeColor(this);" name="Table_Row_1" />

 

If your function, you can get the table row by looking up the parents node of the element

function changeColor(obj){
    var td = obj.parentNode; // table cell
    var tr = td.parentNode; // table row
    tr.style.backgroundColor = obj.value; // change the tr background color
}

NOT TESTED

Link to comment
Share on other sites

Hello,

 

Thanks for the replies, and I apologize for the tardy response, I'll be working on this project again today and I will definitely try these suggestions and get back to you. I think the idea of a radio button is the way to go.

 

Thanks!

~FOX~

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.