Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirect to a new url in perl 1

Status
Not open for further replies.

apw420

MIS
May 12, 2004
6
US
I have a form in an HTML doc. I send it to my Perl script. The perl script then sends it to my mySql database. I then want to redirect the user to another HTML page. Yet, I can't seem to get it to work. Any ideas?

Thank you for your help.

Code:
if ($rows > 0)
{print redirect(-location=>'[URL unfurl="true"]http://matrix.csis.pace.edu/~s04-it604-s13/Project/thanks.html');}[/URL]
else {print "<h2>Error: INSERT query failed.</h2>";
print "<a href=\"javascript:history.go(-1)\"><h2>Return to Previous Page</h2></a>";}
print "</body></html>";
 
Really need more code than that. Also, precisely WHAT is happening?

Can't seem to get it to work

tooooo vague. Error messages? You just see a blank browser? What happens?

At this point, I can only guess. I am guessing it might be one of two things:

1) You are using CGI.pm and printing header() at the beginning of your execution. This would preclude your redirect from working. Solution: don't print header until you know you won't be redirecting.

2) You $rows is in fact <= 0 and you are NOT printing header() before you output your error message. Solution: print header, then your error message.

--jim
 
Sorry for the vagueness. :)

Here's the entire code:

Code:
#!/usr/bin/perl -w
use CGI ':standard';
use DBI;
print "Content-type:text/html\n\n";
print "<html><head><title>Inserting into MySQL";
print "</title></head><body>";
$dbh = DBI->connect("DBI:mysql:s04-it604-s13:localhost", "s04-it604-s13", "PASSWORD");
if (param()) {
  $name=param('name'),
  $email=param('email'),
  $phone=param('phone'),
  $time=param('time'),
  $homequestion=param('homequestion'),
	$selected=param('selected'),
	$textarea=param('textarea');
	
$query = "INSERT INTO estform (name, email, phone, time, homequestion, selected, textarea ) 
	VALUES ( '$name', '$email', '$phone', '$time', '$homequestion','$selected', '$textarea' )";
$rows = $dbh->do($query);
}

if ($rows > 0)
{print redirect(-location=>'[URL unfurl="true"]http://matrix.csis.pace.edu/~s04-it604-s13/Project/thanks.html');}[/URL]
else {print "<h2>Error: INSERT query failed.</h2>";
print "<a href=\"javascript:history.go(-1)\"><h2>Return to Previous Page</h2></a>";}
print "</body></html>";

And this is what I get when I click the SUBMIT on the form:
Status: 302 Moved Location:
Thank you for your help.
AW
 
No problem...

Try this:

Code:
#!/usr/bin/perl -w
use CGI ':standard';
use DBI;

$dbh = DBI->connect("DBI:mysql:s04-it604-s13:localhost", "s04-it604-s13", "PASSWORD");
if (param()) {
    $name=param('name'),
    $email=param('email'),
    $phone=param('phone'),
    $time=param('time'),
    $homequestion=param('homequestion'),
    $selected=param('selected'),
    $textarea=param('textarea');
    $query = "INSERT INTO estform (name, email, phone, time, homequestion, selected, textarea ) 
        VALUES ( '$name', '$email', '$phone', '$time', '$homequestion','$selected', '$textarea' )";
    $rows = $dbh->do($query);
}

if ($rows > 0) {
    print redirect(-location=>'[URL unfurl="true"]http://matrix.csis.pace.edu/~s04-it604-s13/Project/thanks.html');[/URL]
}   
else {
    print header();
    print <<EOF
    <html><head><title>Inserting into MySQL
    </title></head><body>
    <h2>Error: INSERT query failed.</h2>
    <a href="javascript:history.go(-1)"><h2>Return to Previous Page</h2></a>
    </body></html>
EOF
}
Earlier guess #1 was correct. I took your code and moved the output to a more logical place. Should work now.

--jim
 
You rock. Thank you.

What is EOF?

Was the main problem that I "initiated" the display text too early?
 
EOF is nothing. Just a symbol that may as well have been POOPYMCDOOPY. check "perldoc perlop" and search on here-doc to get the scoop on that.

When you are doing CGI script, you have to worry about headers. It's part of the hyper-text-transfer protocol.

Want to see some headers to get a better idea of what they are? Use telnet and access the protocol directly. Below is a copy/paste of a telnet session... what I typed is in red.

Code:
>[red]telnet tek-tips.com 80[/red]
Trying 216.45.19.33...
Connected to tek-tips.com.
Escape character is '^]'.
[red]GET /[/red]
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Thu, 13 May 2004 04:19:20 GMT
Set-Cookie: CFID=23159964;expires=Sat, 06-May-2034 03:49:51 GMT;path=/
Set-Cookie: CFTOKEN=59904918;expires=Sat, 06-May-2034 03:49:51 GMT;path=/
Set-Cookie: CHANDLE=;expires=Tue, 13-May-2003 03:49:51 GMT;path=/
Set-Cookie: CFCLIENT_TEK=;expires=Sat, 06-May-2034 03:49:51 GMT;path=/
Set-Cookie: CFGLOBALS=urltoken%3DCFID%23%3D23159....;expires=Sat, 06-May-2034 03:49:51 GMT;path=/
Content-Type: text/html; charset=UTF-8






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script language="JavaSc... blah blah blah

This gives us some interesting info. But I am only going to care about one thing right now: and that is the 'Content-Type' line. Look familiar? Well, once that line is there, followed by two new lines, per http, the rest of the transmission is content for display.

A redirect is actually sent as a header... so it needs to be BEFORE the "Content-Type......\n\n" line.

Make sense?

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top