Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Unwanted CGI execution on Refreshing

by romy_mathew (Beadle)
on Nov 03, 2011 at 20:16 UTC ( #935762=perlquestion: print w/replies, xml ) Need Help??
romy_mathew has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a e commerce website but my current problem is when I add a product to my bag --> goes to bag.html where it shows the products I have selected but when I refresh the page the same items get incremented every time i refresh

I tried for selecting the page --> moves to> moves to mybag.html

But its fails every time i refresh my bag page the last item i added get incremented Please find the code for below

#!/usr/bin/perl -w use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use CGI::Session; use Apache::Session::MySQL; use DBI; use Template; use 5.010; use rightplacedb; use webdb; use Data::Dumper; $| =1; print "content-type: text/html\n\n"; my $dbh = webdb::connect() or die "Cannot connect to database $@\n"; my $cookie; my %session; my $template; my $sess_id = cookie('CUSTOMER_ID'); print "session id : $sess_id<br>"; tie %session, "Apache::Session::MySQL",$sess_id, { Handle => $dbh, LockHandle => $dbh }; my $page = param('t_shirt'); my $id = param('id'); my $price = param('price'); given ($page) { when ($page eq 'add_to_chart') { if (exists $session{$id}) { # update the quanity of the items $session{'$id'}{'price'} = $price; } else { $session{$id} = $id; $session{'$id'}{'price'} = $price; } print "<br>check the price $session{'$id'}{'price'}"; print "<br>the : $session{'item_code'}"; my $array = $session{'item_code'}; my @selected_item = @$array; push(@selected_item,$id); print "<br>before database : @selected_item<br>"; $session{'item_code'} = \@selected_item; print "<br>the : $session{'item_code'}"; my $bag_id = $session{'item_code'}; print "<br>my bag contains : @$bag_id<br>"; $template = '/home/romy/public_html/rightplace/template/src/mybag. +html'; $dbh->disconnect(); } default { $template = '/home/romy/public_html/rightplace/template/sr +c/entry.html'; } } untie(%session); my $vars = { position => $id, Brands => $price, }; my $tt = Template->new ({ INCLUDE_PATH => [ '/home/romy/public_html/rightplace/template/src/', '/home/romy/public_html/rightplace/template/lib/', ], WRAPPER => 'wrapper', ABSOLUTE => 1, RELATIVE => 1, }); $tt->process($template,$vars)|| die "Failed to execute the template $t +t->error() \n";

Replies are listed 'Best First'.
Re: Unwanted CGI execution on Refreshing
by Anonymous Monk on Nov 04, 2011 at 01:34 UTC
Re: Unwanted CGI execution on Refreshing
by MidLifeXis (Monsignor) on Nov 04, 2011 at 12:55 UTC

    The first response is right on the money.

    A URL is a handle to some asset accessible from your web site. When you want to modify it, you should be using POST, PUT, or DELETE. GET requests should not modify the asset.

    What I typically do is to use POST (when possible, I use PUT and DELETE in a REST methodology), and my response to the POST is a redirect to the GET for the object just modified.


      I agree here, server side redirect is the best way to go. I know this works well for Firefox and Webkit browsers. The browser will not keep the "post" url in its history after a redirect so you can then hit the back and forward buttons all day long. With CGI::Application it only takes less 10 lines with generous whitespace.
Re: Unwanted CGI execution on Refreshing
by Anonymous Monk on Nov 04, 2011 at 12:46 UTC
    Whereas I would find any one of the literally hundreds of existing "e-commerce sites" and simply use that. Maybe one hosted by you; maybe one hosted by someone else. There are no points to be earned for re-inventing the wheel.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://935762]
Approved by ww
Front-paged by MidLifeXis
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2017-10-17 06:07 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (218 votes). Check out past polls.