Help - Search - Members - Calendar
Full Version: Weird page redirection when submitting form.
Weborum Webmaster Forum > Web Page Design > PHP
Just Another Artist
Check out this link to the contact page on my art site. The form should send the form info and return this message: "Thank you for contacting me. I will respond soon.", but instead it redirects the page to a forum post/thread I created.

The correctly working version can be found on contact.php (remove the "-problem" from the page url). The entire code is below, but the quick difference between the two pages is:

contact.php:
<form method="post" action="contact.php">
contact-problem.php:
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">

I could simply just use the method that works, but I want to know why the broswer redirects to a place I have never mentioned in the code. It's as strange as getting spam eMails from people with the same name as your friends...as if someone knows what's in your address book.

Can you explain this phenomenom?

CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>[SAH] Contact -- Shad A. Hall Gallery</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="content-language" content="en-us" />
<meta http-equiv="Imagetoolbar" content="no" />
<link rel="stylesheet" href="sah-css.css" type="text/css" />

</head>
<body>

<div id="containermaster">

<div id="header">
<img src="images/sah-titlebar-768x112-g64.gif" width="768" height="112" alt="Title bar - Click to go to the main page" />
<img id="menu" src="images/sah-menu-768x17-g64.gif" width="768" height="17" alt="menu" usemap="#sah_menu_768x17_g64_Map" />
<map id="sah_menu_768x17_g64_Map" name="sah_menu_768x17_g64_Map">
<area shape="rect" alt="Contact" coords="198,0,242,15" href="contact.html" />
<area shape="rect" alt="About" coords="151,1,188,15" href="about.html" />
<area shape="rect" alt="Art Gallery" coords="117,1,140,15" href="gallery.html" />
<area shape="rect" alt="Home" coords="73,1,103,15" href="index2.html" />
</map>
</div><!--  end of header div //-->

<div id="left"></div>

<div id="content">
<img src="images/spacer.gif" width="1" height="30" alt="" />
<?php
  if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') //if form has been submitted
  {
    //for now:
    
    //get all the data from the form
    $name= $_POST['name'];
    $email = $_POST['email'];
    $usersubject = $_POST['subject'];
    $comment = stripslashes($_POST['comment']);
    $from="me@myexamplesite.com"; //this is what will appear in the from part in your email
    
    $errors = '';
    if($name == '')
    {
      $error .= "<span class=\"error\">You must enter your <label for=\"name\">name</label>.</span><br />";
    }
    if($email == '')
    {
      $error .= "<span class=\"error\">You must enter an <label for=\"email\">e-mail</label> address.</span><br />";
    }
    elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$", $email)) //if email entered, verify format
    {
      $error .= "<span class=\"error\">Your <label for=\"email\">email</label> is not of valid format.</span><br />";
    }
    if($comment == '')
    {
      $error .= "<span class=\"error\">You must enter some <label for=\"comment\">comment</label>.</span><br />";
    }
    if($error == '') //If no errors
    {
      $sendTo = "SAH comment <me@myexamplesite.com>"; //this will be the address that the mail gets sent to
      $subject = "Question from SAH Gallery";
      
      
      $body = "You have received the following from the web based contact form:\r\n";
      $body .= "---------------------------------------------------------------\r\n";
      $body .= "Subject:    Mail from Site\r\n";
      $body .= "From:       ".$name."\r\n";
      $body .= "IP Address: ".$_SERVER['REMOTE_ADDR']."\r\n";
      $body .= "Questions/comment:\r\n";
      $body .= $comment ."\r\n";
      $body .= "---------------------------------------------------------------\r\n";
      
      $headers = 'From: '.$email."\r\n" .
                 'Reply-To: '.$email;
                if(mail($sendTo, $subject, $body, $headers))
      {
        $success="<div class=\"error centertext\">Thank you for contacting me. I will respond soon.</div>";
        $name='';
        $email='';
        $comment='';
      }
      else
      {
        $error = "<div class=\"error\"><span class=\"error\">Unfortunately, we were unable to send your e-mail. Please try again.</span></div>";
        exit();
      }
    }
  }    
?>
           <?php
          if(!empty($error))
          {
            echo "<div class=\"error\">\n";
            echo "<h4 style=\"font-family:12px verdana, garamond, sans-serif; color:red;\">Uh oh! An error occurred...</h4>";
            echo $error;
            echo "</div>\n";
          }
          if(!empty($success))
          {
              echo $success;
          }
        ?>
<!--<form method="post" action="contact.php">-->
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<fieldset id="contact">
<legend id="legend">Contact Form:</legend>
<label for="name">Name:</label>
<input type="text" size="50" name="name" id="name" value="" maxlength="50" /><br />
<label for="email">Email:</label>
<input type="text" size="50" name="email" id="email" value="" maxlength="100" /><br />
<label for="comment">Questions/comment:</label><br />
<textarea name="comment" id="comment" cols="43" rows="4"></textarea>
<div class="input"><input type="submit" name="submit" value="Submit" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Reset" /></div>
</fieldset>
</form>

</div>

<div id="right"></div>

<div id="footer"></div>

</div><!-- end of container master div //-->
</body>
</html>



Creeped out,
Shad

P.s. I started this topic over here to see if I could get some fresh ideas and approaches on thoughts as why this might be happening. The other thread (mentioned above) posts answered the initial problem, but not what causes the issue. I also posted it here since it wouldn't be considered "double-posting"...not trying to be unethical here, just trying to get the most amount of people on it.
Joe
Replace;

CODE
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">


with

CODE
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post">


I think you would be better off making your errors an array, counting each error, if encountered and then if any are set outputting the array at the end.
Just Another Artist
Okay @ arrays. But I'll have to look up some stuff on arrays, which hopefully I can get to in a few hours.

So do you know what caused the weird page redirect?
Joe
More to the point, is it fixed now?
Just Another Artist
Fixed? Well, yes and no.

It worked fine with:
<form method="post" action="contact.php">

When I switched that line out for:
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post">
or even with a semi-colon (which should exist right??)
<form action="<?=$_SERVER['PHP_SELF']; ?>" method="post">
I get this error message:
The requested URL /shadahall/shadahall/contact.php was not found on this server.

But forget that for the time being, as I will simply go back to using the first method above, unless this method you suggested is preferred. In that case then I will work with it until it works.

Since the form worked fine before this thread, I am really just on a war path in hopes of figuring out why they browser redirects my form page to a thread post. Is it bug issue on my computer? If you test out the form does your browser redirect you to a thread instead of returning a message that says your message has been sent? Can you test this for me by clicking on the form text link that I just mentioned?

It's driving me nuts why a browser (FF, IE & NS) redirect the viewer off my site. This is what I'm wanting to know. biggrin.gif

Thanks,
Shad
Joe
The form works fine for me.
Just Another Artist
That is really really strange, but that also implies that the glitch is in fact on my computer.

Thanks for testing it!

What's the benefit of using one over the other?
<form method="post" action="contact.php">
<form action="<?=$_SERVER['PHP_SELF']; ?>" method="post">

Q2: Does it matter if action or method precedes the other attribute in the tag?

I'll be back Tuesday since Monday is a holiday here (Memorial Day). Have a great weekend!
Shad
sjthomas
QUOTE(Just Another Artist @ May 28 2006, 01:04 AM) [snapback]34170[/snapback]

That is really really strange, but that also implies that the glitch is in fact on my computer.

Thanks for testing it!

What's the benefit of using one over the other?
<form method="post" action="contact.php">
<form action="<?=$_SERVER['PHP_SELF']; ?>" method="post">

Q2: Does it matter if action or method precedes the other attribute in the tag?

I'll be back Tuesday since Monday is a holiday here (Memorial Day). Have a great weekend!
Shad



The advantage of using PHP_SELF is that if you move the script you won't have to manually configure it whereas is you define a script specifically, you will have to go through and change it if you move the script. The order of action and method shouldn't really matter.

I can't think of anything that would cause that to happen only on your machine. If it was redirecting you to a spammers site then I would say you have malware on your machine, but i can't see any spammer redirecting you to a web design forum. Is it possible that your pointing to a different script when you run it on your machine (localhost instead of the web server?) or if you are using a tool like Dreamweaver, it can alter things like references and links when it updates/uploads your files, so I would check my options on that. But to be honest, I've not got a clue!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.