John Davidson

How can you change a table td background in php?

0 comments
Message:


I just started learning PHP and I made a webpage that has a table with 3 columns and it has a submit style button that should be able to change the background color of the first and third column if the user clicks on the button. I know this would be a lot easier with Javascript.


Here's my code


<table>
<tr><td bgcolor="blue">
<p> First Column </p>
</td>
<td>
<p> Second Column </p>
</td>
<td bgcolor="green">
<p> Third Column </p>
<form action="" method="post">
<input type="submit" name="color" value="Color">
</form>
</td>
</tr>
</table>
<!--html-->

<?php
if(isset($_POST['color']))
{
echo "<td background-color:blue> </td>";
}
?>

I apolgize if my code is completely off



My Answer:
You can achieve this by using PHP in combination with HTML. Here's an example code snippet that demonstrates how you can change the background color of the first and third column of a table when the user clicks on a submit button:

php



Change Table TD Background Color



// Check if the form is submitted
if(isset($_POST['submit'])){
// Define the background color for the first and third columns
$bgColor = 'lightblue';
} else {
// Default background color
$bgColor = 'white';
}
?>








Column 1 Column 2 Column 3








In this code snippet, we first check if the form is submitted by checking if the 'submit' button is clicked. If the button is clicked, we set the background color to 'lightblue' for the first and third columns. Otherwise, the default background color is set to 'white'.

When the user clicks on the submit button, the form is submitted and the background color of the first and third columns will change accordingly.

Please note that this approach is not as interactive as using JavaScript, but it demonstrates how you can achieve this functionality using PHP.

Rate this post

4 of 5 based on 4615 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us