transferring dynamic data

bunglebrown

New Member
Messages
157
Reaction score
0
Points
0
I'm looking to send data to another region of the page. To be more exact I would like that when an image is clicked the id of that image will be sent to a separate region of the same screen and later (with other information) will be sent on to me so that I can identify which image the user selected. I think it's not so tough but I just can't think of the best way of doing it although I'm pretty sure it requires JS. Any help will be appreciated.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
It could be done using JS, but there would be no way of getting the actual data to you. You would need either server side scripting or an outside service to email the results to you.
If there is only one choice the user has when selecting an image (ie, they click an image, and you get told which image is clicked) then you don't even need to use JavaScript.
If the user can select multiple images and then submit them as a list or w/e, then JS is the best way here.

If you only need the one image, you can use PHP in addition to your HTML image.
In the page with the images, use this code for each image:
Code:
<a href='results.php?image=1' border='0'><img src='picture1.png' alt='1st picture'></a>
Yes, it's just a picture link to a PHP page.
Next, create a new file in the same directory called results.php.
In it, put this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html><head>
<title>Image Selected.</title>
</head><body>
<p>
<?php
if (isset($_GET['image']))
{
$fh = fopen("choices.inc", 'a') or die("can't open file");

fwrite($fh, '<?php $images[] = '."'".$_GET['image']."'; ?>\n");

fclose($fh);
echo "Thank you for your selection";
}
else echo "Something funny happened, no data saved :)";
?>
</p></body></html>
Save it, then create a new file called viewresults.php
In it, put this:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html><head>
<title>Results Page</title>
</head><body>
<?php
$images = array();
if (isset($_GET['code']))
	{
	if ($_GET['code'] == 'yoursecretpassword')
		{
		if (file_exists("choices.inc"))
			{
			require_once("choices.inc");
			}
		else echo "file not found! Any data saved?";
		}
	}

$results = array_count_values($images);
ksort($results,SORT_NUMERIC);

echo "<table border='1'><tr><th>Image<th>Times Selected";
foreach ($results as $key => $value)
	{
	echo "<tr><td>$key<td>$value";
	}

echo "</table>";
?>
</body></html>
And save it. You're all done!

Now whenever you want to view the results, go to
http://yoursite.com/youdirectory/viewresults.php?code=yoursecretpassword
You will see the results in a table, with everything totalled up.

You should change the password, as well as the original HTML page with different values for the images in both the links and the actual image tags, and then you should find it works perfectly ;)
(btw, it's untested, but should work)
 
Last edited:

bunglebrown

New Member
Messages
157
Reaction score
0
Points
0
sounds unreal! Just getting a parse error at the moment:

Parse error: syntax error, unexpected T_STRING in /home/bungle/public_html/MFPOI/viewresults.php on line 11


Line 11 is this:

PHP:
require_once("choices.inc") or die("file not found!" Any data saved?");
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
PHP:
require_once("choices.inc") or die("file not found!" Any data saved?");
Wh00ps!
Change it to this:
PHP:
require_once("choices.inc") or die("file not found! Any data saved?");


EDIT: I suppose just in case you haven't figured it out... if you want to reset everything to zero, just delete the file choices.inc
You can also edit that page in the file manager to change the results as you so wish.

If you really want it emailed to you, you can also set up the cron daemon to email you the results every week and then reset it (or however often you wish).
If you want that and need help, ask :)
 
Last edited:

bunglebrown

New Member
Messages
157
Reaction score
0
Points
0
Again thanks - I guess I should have spotted that.. unfortunately that isn't quite where it ends happily. . .another parse error came back:

Parse error: syntax error, unexpected T_FOREACH, expecting ',' or ';' in /home/bungle/public_html/MFPOI/viewresults.php on line 16

Line 16:
PHP:
foreach ($results as $key => $value)

Might be obvious but not to me.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
Just found another error in that last page.
On line 15, add a semicolon onto the end so it becomes this:
PHP:
echo "<table><tr><th>Image<th>Times Selected";

EDIT: Ha! I didn't even see your post before I posted this.
Anyway, for that last file I have edited my first post. Copy paste the edited version into your file.
I have checked in on my server and it works 100%.
In addition to those few bug fixes, I have also ordered the results, so rather than having a bit of everything anywhere, they now make sense when you're looking for a particular one :)
 
Last edited:

bunglebrown

New Member
Messages
157
Reaction score
0
Points
0
ok added and yes i should have seen it again. I think sometimes that maybe you are testing me.. ..

Am i right in thinking that by going straight to the viewresults.php page I should be able to see the results because there is only "ImageTimes Selected" displayed and no data - it seems something isn't registering and I know you've tested it. But do you think that there are any variables I should be changing apart from the images.

I hope you realise my appreciation on this one.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
did you make sure that the address you visit has ?code=12345whatever on the end?
(where 12345whatever is the secret password from the code on the last page)

If so, then yes you should see the results.
If there is no data, that might be because nobody has clicked on the link yet ;)
 

bunglebrown

New Member
Messages
157
Reaction score
0
Points
0
yes I'm now doing that and getting:

Warning: require_once(1) [function.require-once]: failed to open stream: No such file or directory in /home/bungle/public_html/MFPOI/viewresults.php on line 11

Fatal error: require_once() [function.require]: Failed opening required '1' (include_path='.:/x10hosting/php2/pear/PEAR') in /home/bungle/public_html/MFPOI/viewresults.php on line 11

Not sure why. What else am I doing wrong??

Edit:
I've put up this test page to show in greater detail what I am trying to achieve:

Test Page (currently not available in IE for some idiotic reason)

I want the image ID to be sent to the 2nd slide in the middle section so the user (and later I) can identify which image was selected.

Also as you can see the yellow square changes when you scroll off the image after clicking - I would like it to stay selected (yellow).

All comments welcome!
 
Last edited:
Top