|
|
|
|
Thread title: Php contact form question |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
01-28-2011, 07:09 AM
|
#1
|
Status: I'm new around here
Join date: Mar 2010
Location:
Expertise:
Software:
Posts: 5
|
Php contact form question
This code is from the bottom of a php contact submission form. How can I change the code to direct to, or display a specified web page if the email was successful and another if not in order to stop this generic page from showing. Thanks.
<?php if(isset($emailSent) == true) { ?>
<div class="ok">
<h1>Thanks, <?php echo $name;?></h1>
<p>Your email was successfully sent. We will be in touch soon.</p>
</div>
<?php } ?>
<?php if(isset($hasError) ) { ?>
<div class="error2">There was an error submitting the form.</div>
<?php } ?>
|
|
01-28-2011, 07:24 AM
|
#2
|
Status: Member
Join date: Mar 2005
Location: baltimore, md, usa
Expertise: php, mySQL, js, aJax
Software: virtual brain cells
Posts: 167
|
To redirect you would use
PHP Code:
header("Location: url");
You will not be able to redirect if you allready outputted text.
You can include another file by
PHP Code:
include("filepath/filename.ext");
|
|
01-28-2011, 07:40 AM
|
#3
|
Status: I'm new around here
Join date: Mar 2010
Location:
Expertise:
Software:
Posts: 5
|
|
|
01-28-2011, 03:59 PM
|
#4
|
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
Posts: 2,246
|
Originally Posted by lplplpx3
This code is from the bottom of a php contact submission form. How can I change the code to direct to, or display a specified web page if the email was successful and another if not in order to stop this generic page from showing. Thanks.
<?php if(isset($emailSent) == true) { ?>
<div class="ok">
<h1>Thanks, <?php echo $name;?></h1>
<p>Your email was successfully sent. We will be in touch soon.</p>
</div>
<?php } ?>
<?php if(isset($hasError) ) { ?>
<div class="error2">There was an error submitting the form.</div>
<?php } ?>
|
using a header re-direct is useless here: the script itself has already done the success/fail check and given you conditions for each. just read through the code piece by piece.
the top chunk is what happens upon successful completion. the second chunk is what happens if an error is thrown. if you want to change what gets outup, just change the html betwene the <?php ?> tags:
Code:
<?php if(isset($emailSent) == true) { ?>
<!-- giant thank you graphic -->
<img src="images/thanks.jpg" alt="Thank you for your submission" />
<?php } ?>
<?php if(isset($hasError) ) { ?>
<!-- error was thrown -->
<img src="images/error.jpg" alt="stimpy, you iiidiot!" />
<?php } ?>
|
|
04-24-2011, 12:41 PM
|
#5
|
Status: I'm new around here
Join date: Apr 2011
Location: Connecticut, United States
Expertise: design/development
Software: Coda, Photoshop
Posts: 6
|
I would advise against using multiple pages for a contact form.
The best way is to have the processing and the HTML form on the same page, with the processing before you even echo the <html> tag. As long as you haven't echoed any html yet, you should be able to use the header() function. If you have echoed html, then you can use a meta redirect.
|
|
04-24-2011, 01:59 PM
|
#6
|
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
Posts: 2,097
|
Originally Posted by andrewsuzuki
I would advise against using multiple pages for a contact form.
|
Why? There's no real reason not to..
You could use the action parameter of the form to send the POST data to another php file, which can then display different content based on the success or failure of the emailing. This would only require one extra file and keeps it somewhat modular.
|
|
04-24-2011, 11:24 PM
|
#7
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
Originally Posted by andrewsuzuki
I would advise against using multiple pages for a contact form.
|
It all depends on the application, there is no reason why it would be "better" to use one page opposed to multiple. There have been some projects I've done where a single page was the best options but there have been others where a redirect was the best way to go. It doesn't really matter which one you do.
|
|
05-06-2011, 05:30 AM
|
#8
|
Status: I'm new around here
Join date: Apr 2011
Location:
Expertise:
Software:
Posts: 16
|
You have to add just one line code after this line
<p>Your email was successfully sent. We will be in touch soon.</p>
header('Location: thanks.php');
exit();
|
|
07-24-2011, 02:16 AM
|
#9
|
Status: I'm new around here
Join date: Jul 2011
Location: USA
Expertise: Programming, Other IT Services
Software:
Posts: 3
|
I agree, there is no difference between one page, and using multiple. Some high end frameworks are built on the premise of splitting pages.
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|