Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

G'day all

I have an index.shtml file containing 5 ssi includes:
top.pl (merely prints out some html and some variables)
vote.cgi (see below)
middle.shtml (plain html)
news.cgi (prints out news items)
bottom.pl (merely prints out some html and some variables)
vote.cgi is the one where I'm stumped.

My goal is that when the user votes and submits the form, the form is replaced by the results in the same place on the main page. I understand this would mean essentially re-writing the other pages and altering the output... but I can;t seem to get it to work.

Currently, vote.cgi picks up the fact that it is having a vote submitted using a hidden form field and prints out the results. The problem is the results are printed on their own separate page and I want them to replace the form on the main page.

I have searched, but I found nothing dealing with this specfic problem... any help greatly appreciated.

Cheers in advance

Replies are listed 'Best First'.
Re: Partial reloads of a page \ script ?
by LTjake (Prior) on Aug 28, 2002 at 11:42 UTC
    The way I've handle this situation in the past is to POST directly to vote.cgi. vote.cgi then records the vote and who voted (by either cookie, or some other tracking means). vote.cgi then sends a redirect header back to index.shtml.

    When index.shtml includes vote.cgi again, the script should realize that the user has already voted and will post only the results.

    some pseudocode:
    # ... $action = $query->param('action'); $voted = hasuservoted(); if ($action eq "show") $voted ? show_results() : show_poll(); } elsif ($action eq "vote" && !$voted) { vote(); } else { error("no valid action"); } sub show_poll { # display the poll so the user can fill it in. } sub show_results { # show the poll results -- user can no longer vote } sub vote { # record the user's vote # record who voted # redirect to index.shtml } sub hasuservoted { # returns 1 if user has voted # returns 0 if user has no voted }
    Something like that. In index.shtml you'd include vote.cgi?action=show. Your poll html should include the hidden field <input type="hidden" name="action" value="vote">.

    HTH.
      G'day all

      Thanks for the reply btw :-)

      I already had the hidden field set up and had sort of tried the redirect method... I don't quite understand how you inform index.shtml that the user has already voted

      You're saying, unless I missed the point, that on the cookie you print something to indicate they have voted ? and then run a check before you print the form out as to whether they have voted ?

      Hang on, maybe I do understand :P

      Cheers - it does help :-)
Re: Partial reloads of a page \ script ?
by dws (Chancellor) on Aug 28, 2002 at 08:25 UTC
    My goal is that when the user votes and submits the form, the form is replaced by the results in the same place on the main page. I understand this would mean essentially re-writing the other pages and altering the output...

    If the other parts of the page aren't affected, then your facing a situation that frames (and framesets) were designed to handle. If the voting page is in a frame, the frame source can be set to your voting CGI. That CGI can check whether the user has already voted. If not, emit a voting form. If so, emit results. To vote, POST to the same CGI, which records the vote, and emits HTML representing results. The remainder of the page is unaffected.

Re: Partial reloads of a page \ script ?
by bjelli (Pilgrim) on Aug 28, 2002 at 08:21 UTC
      G'day all (just identified - sorry)

      OK, tried that... it *almost* works, except the form cannot be submitted using the POST method. (Is this a server configuration issue or it just cannot be so ?) I was wanting to use POST as a method specifically, rather than GET.
      Is this possible ?

      Cheers in advance
        It is impossible. Content of POST (contrary to query string in GET requests) is not passed to scripts called via SSI.

        --
        Ilya Martynov (http://martynov.org/)