Replace

Hello,

I am looking for a script that can replace the standard image on a page into one selected on an other (please see the code below).
The first page (CHANGE IMAGE) is a page with three images a person can  select. This can be done by checking the radio button in front of one of the  images. If a person clicks the "Change" button, the standard image on the  "PAGE WITH IMAGE" page should be replaced with the selected image.


<HTML>
<HEAD><TITLE>CHANGE IMAGE</TITLE>
</HEAD>

<BODY>
<CENTER>
<TABLE WIDTH=600>
<FORM METHOD="post" ACTION="/cgi/change">
<TR><TD VALIGN=top WIDTH=200>

<INPUT TYPE="radio" NAME="change" VALUE="image2"><IMG SRC="image2.gif">
</TD>

<TD VALIGN=top WIDTH=200>
<INPUT TYPE="radio" NAME="change" VALUE="image3"><IMG SRC="image3.gif">
</TD>

<TD VALIGN=top WIDTH=200>
<INPUT TYPE="radio" NAME="change" VALUE="image4"><IMG SRC="image4.gif">
</TD></TR>

<TR><TD ALIGN=center COLSPAN=3>
<INPUT TYPE="button" VALUE="Change">
</TD>

</FORM></CENTER>
</TR></TABLE>
</BODY></HTML>

--------------------------

<HTML>
<HEAD><TITLE>PAGE WITH IMAGE</TITLE>
</HEAD>

<BODY>
<CENTER>
<TABLE WIDTH=550>
<TR><TD VALIGN=top>

<H2>Head</H2>

Text, text, text.<P>

<IMG SRC="IMAGE.gif">

</CENTER>
</TR></TABLE>
</BODY></HTML>



gardensAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
thoellriConnect With a Mentor Commented:
gardens,

sorry for the delay, but I had to put out some fires here at work :-)

I think I understood the scope of the work now and here is what I would suggest to do.

1.) Generate the generic select image page.
2.) when cgi/change is called we need to make sure we know exactly who is calling it (username). I really don't know how you want to do this (login? let the user supply username/password in the form?)
3.) The script below assumes that the cgi-parameter 'user' identifies the username and 'image' selects an image from a pool of images.
4.) The script opens the user-specific html page and search for markers in the page. If these markers are found the image between the markers is replaced by the one chosen.


Let me know, if you need more information
  Tobias



#!/usr/local/bin/perl

use strict;
use CGI;

# what is the filename of the "second page"?
use constant USER_SPECIFIC_PAGE=>"user-specific-page.html";
# where are all the images?
use constant IMAGES_DIR=>"/www/images";

my $cgi=new CGI;

sub errorOut {
  my($reason)=shift;
  print $cgi->header();
  print $cgi->start_html("Error");
  print qq{Reason: $reason<br>\n};
  print $cgi->end_html();
  exit (1);
}

sub userDirFromUserName {
   my($username)=shift;

   # given a username compute the user's directory (where the user-specific-page is located)

   return qq{/www/users/$username};
}

sub mapImage {
   my($selection) = shift;

   #based on the selection made on the first page, select an image and return it's filename.

   my (@images) = ("image1.gif", "image2.jpeg", "image3.gif");

   return $images[$selection];
}

my $username=$cgi->param('user') || errorOut(qq{'user' parameter missing});
my $userfile=userDirFromUserName ($username)."/".USER_SPECIFIC_PAGE;
my $image=IMAGES_DIR."/".mapImage($cgi->param('image'));
my ($html);

open(HTML,"<$userfile") || errorOut(qq{can't open $userfile - $!});
{ local($/)=undef;
   $html=<HTML>;
}
close(HTML);
$html=~s{(<!--begin_img-->).*(<!--end_img-->)}{$1<img src="$image">$2}g;
open(HTML,">$userfile") || errorOut(qq{can't update $userfile - $!});
print HTML $html;
close(HTML);

print $cgi->header();
print $cgi->start_html("Success");
print qq{Updated: $username uses now image $image<br>\n};
print $cgi->end_html();


0
 
thoellriCommented:
Questions:

1.) will people always go to the first page before arriving at page 2?
2.) what should happen if somebody bookmarks page 2 and returns there?

It is quite easy to change the image on page 2 based on the selection on page 1, however the two questions above are important for a solid solution.

Cheers
  Tobias
0
 
gardensAuthor Commented:
Hi Tobias,

1) This depends. Some will first visit the first page, others will want to change the image immediately.
But if it is easier to create it so that people will not see the page first, this is fine.

2) Page one is a simple page wizard, that allows a person to have his version of the page. So each person will have a separate page (a real page or created on-the-fly this doesn't matter).

Therefore if anyone visits the page after it has changed, the new image is what he sees.

Thanks in advance
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
thoellriCommented:
Hmmm - this makes it a bit more complicated :-)

Do your users accept cookies?

Here is what I think of:
* When somebody goes to the first page and selects an image we send out a cookie which describes what image has been selected.
* On the second page we check the cookie and if found select the image which is identified by the cookie.
* If no cookie is found a "default" image is chosen.

Does this sound like a solution?

If yes, then you'll get two scripts: one which accepts the formdata from the first page and sets the cookie and a second one which selects the image based on the cookie.

Let me know, if this is a suitable solution and I'll send the code.

Tobias
0
 
gardensAuthor Commented:
It sounds good, but if someone else visits the person's page he will never have a cookie. Therefore he will always see the "default" image.
The code of the page should actually change.

I realize that this is probably more difficult, so I will add 100 points and give you an "A" if you are willing to create it.
0
 
thoellriCommented:
Just to clarify: Page2 is unique per user? Is it sitting in a private directory which is associated with this user?

Means: user1 has the following URLs

   http://someserver/user1/page1.html
   http://someserver/user1/page2.html

and user2

   http://someserver/user2/page1.html
   http://someserver/user2/page2.html

or is it like this:

   http://someserver/generic/selectImage.html
   http://someserver/username/user-specific-page2.html

I need to know this, because at some point the image needs to be replaced and if page2 is the same for all users then we need to store information about the selected image somewhere else.
If every users has his own page2 then we can actually make the change in place by modifying the HTML code.

Sorry for the constant back and forth. Do you happen to have a sample I can look at?

Tobias

0
 
gardensAuthor Commented:
Sorry, I went off-line (family emergency).

Your second description is correct. There is one page wizard for all users. Every user has it's own page two. So:

http://someserver/generic/selectImage.html
http://someserver/username/user-specific-page2.html

The HTML code in my first question is what I have at the moment. I am still working on the rather complicated text. However, the layout of the page will stay the same.
0
 
gardensAuthor Commented:
Thanks Tobias for all your hard work. My users will
login on an other page (the members index). A  friend of mine can make the small alteration so that the script will know which member is calling.

Have increased the point (well worth it :) and give you a deserved "A".

Thanks again.
0
All Courses

From novice to tech pro — start learning today.