Hi, I want to make a page that has a code that can make my image change into another image.

Lets say I arrive at work one morning. I go to my "website" click on a red image, and in turns green. Now I want it to stay green all day long until I'm going home for the day. Then I want to be able to press on the green image and it will change to red.
Kinda like a check/uncheck button.

Can this be done? I want it to stay green the whole day no matter what computer I am on.


Can someone help me with this?

Recommended Answers

All 7 Replies

You'll need to store the in/out status somewhere, if you already have a mysql database set up that's probably the best route but if not storing the information on disk might be an easier alternative.

So the process is basically just this:

- Code to change the status of the information
- Load the information
- Display a link back to the same page and the state of the information

example.php

<html>
<head>
<title>Time Clock</title>
</head>
<body>

<?php

$Current_State=file_get_contents("clocked.txt");


if (isset($_POST['submit'])) //if the button at the bottom of the code is clicked
 {                           //then execute this code.
 if ($Current_State=="In")   // Writes either In or Out to the file clocked.txt
  {
  file_put_contents("clocked.txt","Out");
  } // if the file says 'In' write 'Out' and vice versa
 else
  {
  file_put_contents("clocked.txt","In");
  }
 $Current_State=file_get_contents("clocked.txt"); //since we changed the state, we have to reload the variable
 }

if ($Current_State=="In")    //Set the string $image_val to the image we want to display
 {
 $image_val="in.jpg";
 }
else
 {
 $image_val="out.jpg";
 }

echo "<form action='example.php' method='post'>";
echo "<input type='image' src='images/$image_val' value='submit' name='submit' alt='Submit' />";
// in the above line pay attention to the src='' when the page loads, the code will set it to either images/in.jpg or images/out.jpg depending on the contents of clocked.txt
// you normally don't want to put variables ($image_val) inside single quotes because it won't evaluate the variable, but because we're wrapping the whole line in double quotes it works just fine.  A lot of people still shun putting variables inside quotes and prefer using this method.
echo "<input type='image' src='images/".$image_val."' value='submit' name='submit' alt='submit' />";
// in this line we add the first string + variable + last string to create one line to send to the browser.
echo "</form>";

Hope that helps.

David

I have a mysql database available. And also thought maybe this was the best way to store it.

But how can I edit the value of the table to something else?

You can give this Effect with the help of Mouse over or Mouse Click Event. It's very easy to create this kind of effect.

You can give this Effect with the help of Mouse over or Mouse Click Event. It's very easy to create this kind of effect.

??? What post are you responding to? edit: Oh, I get it. To change the image. But Siege is wanting it to stay that way even if the page reloads or you shut the computer down and reboot. For that you have to store the value somewhere.

How well do you know mySQL? If you aren't terribly familiar with it then the text file is the way to go. Setting up an entire database, table, and field just for one value is like using a sledgehammer to drive a nail.

Just for yucks, though, you use UPDATE to edit the value.

Assuming you haven't already been there, you might try:
http://www.w3schools.com/sql/sql_update.asp

You can also get the other commands to create a table, add a field and everything else there.

What you are saying is that I should use some sort of Mouse Event? To engage the UPDATE in the database?

You'll need to store the in/out status somewhere, if you already have a mysql database set up that's probably the best route but if not storing the information on disk might be an easier alternative.

So the process is basically just this:

- Code to change the status of the information
- Load the information
- Display a link back to the same page and the state of the information

example.php

<html>
<head>
<title>Time Clock</title>
</head>
<body>

<?php

$Current_State=file_get_contents("clocked.txt");


if (isset($_POST['submit'])) //if the button at the bottom of the code is clicked
 {                           //then execute this code.
 if ($Current_State=="In")   // Writes either In or Out to the file clocked.txt
  {
  file_put_contents("clocked.txt","Out");
  } // if the file says 'In' write 'Out' and vice versa
 else
  {
  file_put_contents("clocked.txt","In");
  }
 $Current_State=file_get_contents("clocked.txt"); //since we changed the state, we have to reload the variable
 }

if ($Current_State=="In")    //Set the string $image_val to the image we want to display
 {
 $image_val="in.jpg";
 }
else
 {
 $image_val="out.jpg";
 }

echo "<form action='example.php' method='post'>";
echo "<input type='image' src='images/$image_val' value='submit' name='submit' alt='Submit' />";
// in the above line pay attention to the src='' when the page loads, the code will set it to either images/in.jpg or images/out.jpg depending on the contents of clocked.txt
// you normally don't want to put variables ($image_val) inside single quotes because it won't evaluate the variable, but because we're wrapping the whole line in double quotes it works just fine.  A lot of people still shun putting variables inside quotes and prefer using this method.
echo "<input type='image' src='images/".$image_val."' value='submit' name='submit' alt='submit' />";
// in this line we add the first string + variable + last string to create one line to send to the browser.
echo "</form>";

Hope that helps.

David

Could you do this but with a database? If so, how ?

Yes you can do it with a database, I'm not going to write the page for you because database access is something you need to know and if I write it for you it I won't be doing you any favor.

To use a mySQL database you need to write some code that connects to the database and put it at the top of the page, then you use mysql_query("$query") where $query is the mySQL command to SELECT,UPDATE or any other command you need to modify your database.

Go back a couple of posts and follow that link I gave you, or you can follow this one if you've never used mySQL.

http://www.w3schools.com/sql/default.asp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.