Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > Dynamic tables
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 3rd July 16:32
External User
 
Posts: 1
Default Dynamic tables



Hi all,
Having a slight problem with dynamic tables here.
I am trying to create a timetable using PHP, HTML, and MySQL, where all the
cells in the table are coloured and that the colour of the cells can be changed
using a drop down menu. Here's a snip of the code that I have done:


<td bgcolor=<? echo"$_POST[colour]"?> class="Devi" width="15%" align="center"
valign="middle"><a href="select1.php">2PE1012</a></td>

<select name="colour" class="textarea" align="center">
<option name= "Grey" value="#dedede">Grey</option>
<option name="Pink" value="#FFB6C1">Pink</option>
<option name="Blue" value="#87CEEB">Blue</option>
<option name=Yellow value="#FFFF00">Yellow</option>
<option name="Cyan" value="#AFEEEE">Cyan</option>
</select>

Right now, I am able to change the colour of cells using a drop down menu, and
when I clicked on "Reload" it still remains the colour that I have changed, but
when I open the page in another browser, as I echoed the value "colour", the
browser displayed a mixture of the colour code :grey, pink, blue, etc....
How can I do it in the way that when I opened up a new browser it will display
only the colour that I have changed to earlier on?....Hope to get some help
soon...thanks for all help given.

Regards,
Irin.
  Reply With Quote


 


2 3rd July 16:33
phpmail
External User
 
Posts: 1
Default Dynamic tables



You're trying to hold state. To do this, you need to use a cookie or a
session.

There are also several problems with the script you seem to be using (for
example, it's open to code injection). Hopefully, you'll be able to tease
some sense out of this: <?
$valid_colours = array ('Grey' => '#dedede', 'Pink' => '#ffb6c1',
'Blue' => '#87ceeb', 'Yellow' => '#ffff00',
'Cyan' => '#afeeee'); /* valid responses */

session_start();

if (isset ($_POST['textcolour']))
{
/* check that incoming textcolour is one of the permitted values */
if (in_array ($_POST['textcolour'], $valid_colours))
{
$_SESSION['textcolour'] = $_POST['textcolour'];
header ("Location: $PHP_SELF");
exit ();
}
}

/* if we don't have a colour, default to grey */
if (isset ($_SESSION['textcolour']) == FALSE)
{
$_SESSION['textcolour'] = $valid_colours['Grey']; }
?>
<html>
<body>
<table bgcolor="<? echo ($_SESSION['textcolour']); ?>">
<tr><td>Test</td></tr>
</table>
<form method="post" action="<? echo ($PHP_SELF); ?>">
<select name="textcolour"> <?
foreach ($valid_colours as $colour => $value)
{
if ($value == $_SESSION['textcolour']) {
echo ("<option value=\"$value\" selected>$colour</option>"); } else {
echo ("<option value=\"$value\">$colour</option>"); } }
?>
</select>
<input type="submit" name="change" value="Change">
</form>
</body>
</html>
  Reply With Quote


 


Reply


Thread Tools
Display Modes




666