Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

PHP Form to Email, then redirect

Hi, I have this excellent little Form to Email script that I've been using. It looks like this:

<SCRIPT LANGUAGE="php">
    $email = $HTTP_POST_VARS[email];
    $mailto = "equigley@alexandermarketing.com";
    $mailsubj = "Form submission";
    $mailhead = "From: $email\n";
    reset ($HTTP_POST_VARS);
    $mailbody = "Values submitted from web site form:\n";
    while (list ($key, $val) = each ($HTTP_POST_VARS)) {
        $mailbody .= "$key : $val\n"; }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
</SCRIPT>

The problem is, I'm not too good with PHP yet, so my question is: is there a way to get this script to immediately redirect the browser to another site after it has processed the script ?
0
eric_quigley
Asked:
eric_quigley
  • 5
  • 3
  • 3
  • +1
1 Solution
 
CyberGhostCommented:
Why do you use <script></script> statement for PHP?
It might help to get familiar with <?php and ?> to get PHP into your website.

Anyway, what you need to do is to update your script like this:

<?php
    $email = $HTTP_POST_VARS[email];
    $mailto = "equigley@alexandermarketing.com";
    $mailsubj = "Form submission";
    $mailhead = "From: $email\n";
    reset ($HTTP_POST_VARS);
    $mailbody = "Values submitted from web site form:\n";
    while (list ($key, $val) = each ($HTTP_POST_VARS)) {
        $mailbody .= "$key : $val\n"; }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
    Header("location:website_to_redirect.php"); // this is the part which redirects user to another page
    exit; // not needed here, however, a good practise when using Header function
?>

I assume your <script></script> woudln't work here with Header, as Header function sets headers to browser before any output is generated. And <script> will generate output, so Header won't work here.

regards,
CyberGhsot
0
 
CyberGhostCommented:
wow... I was so quick, I have misspelled my own nickname :-)))

regards,
CyberGhost  :-)(-:
0
 
eric_quigleyAuthor Commented:
hmmm...

Thanks for your quick response. When I tried your code, I got this error message:

Parse error: syntax error, unexpected T_STRING in /var/www/domains/x.nyatti.com/docs/contact_success.php on line 2

I put the code that you modified for me above into a page called contact_success.php with nothing else on it except for the code. Then I had my form post to contact_success.php

Am I not doing this correctly?

Thanks for you help.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CyberGhostCommented:
Sorry, I did not debug your own code :-)
You seem to forgot to add single quotes on line 2 :-))

Here's working code:

<?php
    $email = $HTTP_POST_VARS[$email]; // had missing $ in email variable
    $mailto = "equigley@alexandermarketing.com";
    $mailsubj = "Form submission";
    $mailhead = "From: $email\n";
    reset ($HTTP_POST_VARS);
    $mailbody = "Values submitted from web site form:\n";
    while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
    Header("location:website_to_redirect.php"); // this is the part which redirects user to another page
    exit; // not needed here, however, a good practise when using Header function
?>

So, from now on, when PHP generates such error code, only thing you need to do is check what it says is wrong (line 2 in this case :-)

regards,
CyberGhost
0
 
CyberGhostCommented:
I must be in love or something... I did the right correction in my comment but messed it up in your code :-)

here is the definitely working part of code that was wrong:

<?php
    $email = $HTTP_POST_VARS['email']; // had missing '' in email variable
    ....
?>

Sorry again,
CyberGhost
0
 
eric_quigleyAuthor Commented:
The real problem here is that I'm extremely new to PHP. I'm probably just not seeing the obvious and  I don't know what syntaxes are correct or incorrect.

Now I'm getting this message:

Parse error: syntax error, unexpected T_STRING in /var/www/domains/x.nyatti.com/docs/contact_success.php on line 3

Any ideas?  

Thanks again. Sorry to keep coming back with what are probably obvious questions.
0
 
tragicrockCommented:
<?php

function send_me_to($url){
      echo "<SCRIPT LANGUAGE=\"JavaScript\">\n";
      echo "      <!-- \n";
      echo "            function redirect(){ \n";
      echo "            parent.location.href=\"".$url."\" }\n";
      echo "      //-->\n";
      echo "</SCRIPT>\n";
      echo "<SCRIPT LANGUAGE=\"JavaScript\">\n";
      echo "      <!-- \n";
      echo "            setTimeout(\"redirect()\",0)\n";
      echo "      //-->\n";
      echo "</SCRIPT>\n";
}

    $email = $_POST['email'];    
    $mailto = "equigley@alexandermarketing.com";
    $mailsubj = "Form Submission";
    $headers  = "From: ".$email."\n";
    $headers .= "X-Mailer: PHP ".phpversion()."\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=ISO-8859-9\n\n";
    $mailbody = "Values submitted from web site form: \n";
    reset ($_POST);
    while (list ($key, $val) = each ($_POST)) { $mailbody .= $key." : ".$val."\n"; }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
    send_me_to('contact_success.php');
    exit();
?>

if that doesn't work, try it again with $HTTP_POST_VARS instead of $_POST ...don't know what version of PHP your working on.
0
 
CyberGhostCommented:
Can't see a problem.
I've put this code into PHP file and tried it out:

<?php
    $email = $HTTP_POST_VARS['email']; // had missing '' in email variable
    $mailto = "equigley@alexandermarketing.com";
    $mailsubj = "Form submission";
    $mailhead = "From: $email\n";
    reset ($HTTP_POST_VARS);
    $mailbody = "Values submitted from web site form:\n";
    while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
    Header("location:website_to_redirect.php"); // this is the part which redirects user to another page
    exit; // not needed here, however, a good practise when using Header function
?>

... and it works. So I have no clue what is wrong now. Is the code above you have on your site?
Especially this line...
    $email = $HTTP_POST_VARS['email']; // had missing '' in email variable
???
0
 
tragicrockCommented:
SORRY...forgot to change the headers variables...see below:

<?php

function send_me_to($url){
     echo "<SCRIPT LANGUAGE=\"JavaScript\">\n";
     echo "     <!-- \n";
     echo "          function redirect(){ \n";
     echo "          parent.location.href=\"".$url."\" }\n";
     echo "     //-->\n";
     echo "</SCRIPT>\n";
     echo "<SCRIPT LANGUAGE=\"JavaScript\">\n";
     echo "     <!-- \n";
     echo "          setTimeout(\"redirect()\",0)\n";
     echo "     //-->\n";
     echo "</SCRIPT>\n";
}

    $email = $_POST['email'];    
    $mailto = "equigley@alexandermarketing.com";
    $mailsubj = "Form Submission";
    $mailhead  = "From: ".$email."\n";
    $mailhead .= "X-Mailer: PHP ".phpversion()."\n";
    $mailhead .= "MIME-Version: 1.0\n";
    $mailhead .= "Content-type: text/html; charset=ISO-8859-9\n\n";
    $mailbody = "Values submitted from web site form: \n";
    reset ($_POST);
    while (list ($key, $val) = each ($_POST)) { $mailbody .= $key." : ".$val."\n"; }
    mail($mailto, $mailsubj, $mailbody, $mailhead);
    send_me_to('contact_success.php');
    exit();
?>

if that doesn't work, try it again with $HTTP_POST_VARS instead of $_POST ...don't know what version of PHP your working on.
0
 
Mark GilbertSenior Performance EngineerCommented:
A T_STRING error leads me to believe that you have missed a colon at the end of your line.
0
 
eric_quigleyAuthor Commented:
Thanks CyberGhost, I'm not sure what I was doing wrong, but when I tried it again, it worked.

Sorry tragicrock, your solution totally worked too, but Cyberghost got to it first so I felt like I should award the points to him. Thanks for your solution though.
0
 
tragicrockCommented:
be careful with the code.  later versions disallow certain syntaxes, so you want to use the latest possible (check you server version).  also, variable with strings can be tricky.  usually you would only put a non-concatinated variable in single quotes i.e.

$x = 'the value of $y';

or go old skool...the way I like too...explicit:

$x = "the value of ".$y;

and another common is the $HTTP_POST_VARS['email'] vs. $_POST['email'] vs. $email issue.  all three work, but it depends on your version PHP.  my advise is to get up to speed with the latest version and get your host to put you on a machine with PHP5.  that'll ensure code compatibility and sustainability...unless of course it's of no concern.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now