Link to home
Start Free TrialLog in
Avatar of Ted1984
Ted1984

asked on

Problem: My contact.php does not automatically re-direct to my website

I was just wondering if there is anyway I can possibly change the way my contact.php is currently working. At the moment when someone fills up the form on my website they are left with a blank page with a 0 in the top left hand corner, and it really deteriorates the presentation value of the site. What I am looking for is to make the contact.php form do its process but afterwards have it redirect to another page on my website instead of simply sitting on that blank page.

Here is the script I am currently using for my contact.php form:

?php // contact.php
error_reporting(E_ALL);

// ACTIVATE THESE TWO LINES TO SEE THE POST INFORMATION
// echo "<pre>";
// var_dump($_POST);

// THE EMAIL ADDRESS THAT WILL RECEIVE THE MESSAGE
$m = 'info@deconlogic.com';

// A FUNCTION TO CLEAN UP THE INFORMATION AND AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $rgx
    = '#'         // REGEX DELIMITER
    . '['         // CHARACTER CLASS
    . '^'         // NEGATION - MATCH NONE OF THESE ACCEPTABLE CHARACTERS
    . ' A-Z0-9_,?/@&*=()!\.\-'
    . ']'         // END OF CHARACTER CLASS
    . '#'         // REGEX DELIMITER
    . 'i'
    ;

    // ELIMINATE EXCESS WHITESPACE
    $str = trim(preg_replace('#\s\s+#', ' ', $str));

    // RETURN THE STRING - BAD CHARACTERS REPLACED WITH THE QUESTION MARK
    return preg_replace($rgx, '?', $str);
}

// CLEAN AND FORMAT THE INFORMATION
$n = clean_string($_POST['n']);
$p = clean_string($_POST['p']);
$e = clean_string($_POST['e']);
$t = clean_string($_POST['t']);

$w = $_SERVER["HTTP_HOST"];

// CREATE THE EMAIL MESSAGE
$b = <<<MESSAGE
Name: $n
Phone: $p
Email: $e
Job Description: $t
MESSAGE;

// SEND THE MESSAGE
mail($m, "Message from $w", $b);

Open in new window


And this is a link to the page which is using this.

Decon - Contact Page

Hopefully someone can help me out, this is thanks in advance.
Avatar of James Williams
James Williams
Flag of United States of America image

PUT this on top of the <body>
<SCRIPT type="text/javascript">
 function goToPage(url)
 {
 var initial = "http://www.page yuou want.com";

 document.something.action=initial;
 }
 </SCRIPT>
Chage you form with this line.
<form method="post" action="contact.php" id="contact-form" onsubmit="goToPage(this.url.value);">

 <INPUT type="text" name="url" value="">
 <INPUT type="submit" value="GO">
 </FORM>
 </BODY>
 </HTML>
ASKER CERTIFIED SOLUTION
Avatar of designatedinitializer
designatedinitializer
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You do not need to redirect the client browser.  You can, but it is not necessary.  After line 48 (sends the email message) you can just create a "thank you" page, something like this using heredoc notation.  It can be as simple or elaborate as you would like.  You can load the page via include() or put the HTML string into the contact.php page.
$html = <<<HTML
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<!-- ... REST OF THE HTML PAGE GOES HERE -->
HTML;

echo $html;

Open in new window

Avatar of Ted1984
Ted1984

ASKER

Should this work?

<?php // contact.php
error_reporting(E_ALL);

// ACTIVATE THESE TWO LINES TO SEE THE POST INFORMATION
// echo "<pre>";
// var_dump($_POST);

// THE EMAIL ADDRESS THAT WILL RECEIVE THE MESSAGE
$m = 'info@deconlogic.com';

// A FUNCTION TO CLEAN UP THE INFORMATION AND AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $rgx
    = '#'         // REGEX DELIMITER
    . '['         // CHARACTER CLASS
    . '^'         // NEGATION - MATCH NONE OF THESE ACCEPTABLE CHARACTERS
    . ' A-Z0-9_,?/@&*=()!\.\-'
    . ']'         // END OF CHARACTER CLASS
    . '#'         // REGEX DELIMITER
    . 'i'
    ;

    // ELIMINATE EXCESS WHITESPACE
    $str = trim(preg_replace('#\s\s+#', ' ', $str));

    // RETURN THE STRING - BAD CHARACTERS REPLACED WITH THE QUESTION MARK
    return preg_replace($rgx, '?', $str);
}

// CLEAN AND FORMAT THE INFORMATION
$n = clean_string($_POST['n']);
$p = clean_string($_POST['p']);
$e = clean_string($_POST['e']);
$t = clean_string($_POST['t']);

$w = $_SERVER["HTTP_HOST"];

// CREATE THE EMAIL MESSAGE
$b = <<<MESSAGE
Name: $n
Phone: $p
Email: $e
Job Description: $t
MESSAGE;

// SEND THE MESSAGE
mail($m, "Message from $w", $b);

$html = <<<HTML
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<!-- ... deconenviro.com/formsent2.html -->
HTML;

echo $html;

Open in new window

Line 53 is an HTML comment.  I think you might want to read the HTML file deconenviro.com/formsent2.html into a string variable and put that variable in place of the comment.
Avatar of Ted1984

ASKER

I'm unsure how to turn it into a string variable, is there anyway you can help me with a script? I honestly know nothing about .php or just about any script for that matter, since you can tell I am even scared to remove your descriptions from the code itself.
If you don't know PHP but you're trying to write a PHP script, you might as well be trying to write German poetry without knowing German language.  The outcomes are likely to be suboptimal.

You might want to consider hiring a professional to help with this.  If you believe, as I do, that time is money you will undoubtedly find that you can exchange a very little money for a great saving of time.  It's not a scary thing at all; you just need some assistance from someone who is familiar with the languages.  ELance.com is a source for freelance professionals, and I have heard that EE now has a "hire me" section.
Avatar of Ted1984

ASKER

If I had the money to hire freelancers then I wouldn't be trying to do it myself, or come to a website such as this.

Now if you must know PHP is not poetry, it's not even right to call it a language, at most we can call it a system of grammar, defined by coding which is something so simple you can learn it over night as I just have, but you my friend will not learn poetry in a lifetime.

Now if I had to do PHP in the German Language, it would still not be as complicating as poetry.

Thank you for your help but you've repelled me from continuing a subscription past my trial, and directed me to the web to find my answers.

And in search I found this website and learned all I need to know.

Eureka!
Thank you for sharing, Ted1984.  I assumed, apparently incorrectly, that this site was something with economic value and a sponsoring organization that was paying for site development.  And I was quite serious when I said "a very little money."  I'm talking about buying someone a beer, and the thing is all done before the beer is gone.

Tizag is a good site, as is W3Schools.  However they do not have a question and answer dialog like we have here at EE.  The script you posted with the question is one that I wrote.  It's not intended to be a finished product; it is only a teaching example.  You may have figured that out, but as Alexander Pope says, "A little learning is a dangerous thing."  Please don't confuse ignorance with a lack of intelligence.  There are lots of smart people who do not know HTML or PHP (I teach many of them, and it's always a pleasure to see the "lightbulb" go on).  If you knew PHP and HTML you would have implemented the principles shown at ID: 37800827.

In case you ever decide you want to take on some structured learning, this book will help you get a foundation.  Very readable with excellent examples.  Now in its fourth printing, it's been a permanent part of my professional library since Edition One.
http://www.sitepoint.com/books/phpmysql4/

Good luck with your project, ~Ray