SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast freelancekaleem's Avatar
    Join Date
    Dec 2010
    Location
    Pakistan
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Contact Us form redirect to home page after submit

    I have developed a contact us page and also PHP code that works perfect. Know the only problem is that after submitting the form it shows only "Message sent" on a new page. I want that after submitting the form page will redirect back to home page. It is only one page "Landing Page". Here is the PHP code.`
    Code:
    <?php
        
        if(!$_POST) exit;
        
        $email = $_POST['email'];
        
        
        //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
        if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
        	$error.="Invalid email address entered";
        	$errors=1;
        }
        if($errors==1) echo $error;
        else{
        	$values = array ('name','email','message','phone');
        	$required = array('name','email','message','phone');
        	 
        	$your_email = "todd@toddandersonhomes.com";
        	$email_subject = "Free Credit Report: ".$_POST['subject'];
        	$email_content = "Following is the free credit report detail from client:\n";
        	
        	foreach($values as $key => $value){
        	  if(in_array($value,$required)){
        		if ($key != 'subject' && $key != 'company') {
        		  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        		}
        		$email_content .= $value.': '.$_POST[$value]."\n";
        	  }
        	}
        	 
        	if(@mail($your_email,$email_subject,$email_content)) {
        		echo 'Message sent!'; 
        	} else {
        		echo 'ERROR!';
        	}
        }
        ?>
    HTML Code:

    HTML Code:
     <form action="contact.php" method="post">
        
                            	<div class="formele">
        				<ul>
        					<li>
        					<label>Name:</label>
        					<input type="text" id="name" name="name" class="text2" />
        					</li>
        					<li>
        					<label>Telephone:</label>
        					<input id="name" type="text" name="phone" class="text2" />
        					</li>
        					<li>
        					<label>E-mail:</label>
        					<input id="name" type="text" name="email" class="text2" />                    
        					</li>
                            <li>
        					<label>Address:</label>
        					<input id="name" type="text" name="message" class="text2" />                    
        					</li>
        					
        					<li class="button">
        					<input type="image" type="text" src="images/submit.png" />					
        					</li>
        				</ul>			
        			</div><!--Contact Form Ends Here -->
                    </form>

  2. #2
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the form tag: action="/" or action="" not sure

  3. #3
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Right after the email is sent, instead of this message
    echo 'Message sent!';

    use redirect like this:
    header("Location: your home page");

    your home page is the name of your Landing Page. Maybe not needed if it is index.php or index.html

    Note that if your home page is at your parent directory make sure to use ../ before the name.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Or, you could just let your homepage handle it.
    Stick the code to handle the submission in your homepage (or an include thereto) and just have the form point at the homepage.

    (I try to avoid header() calls whenever possible.)

    and Andres: it would be "/". "" translates to "the current page" in form-speak.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can you tell us why header() should be avoided? So what is the best way to perform redirect?

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    I avoid header calls because i tend to use a header include file. Which means my output has already been started, so i CANT send a header call.

    In this particular scenario, consider what you're telling him to do.
    Person fills form out. Hits submit.
    Form Processor Page loads for a brief instant (page 2 load)
    Processor page sends Header. Browser loads new page (Page 3 load)
    Index page loads.

    In terms of your database, assuming that each page uses a database connection, you've connected 3 times.

    If, however, you point the form directly at the index page, you dont need a third connection - you've already opened one for the index page, so you'll just use that connection to process the form during the course of loading the index page.

    Plus you dont have the browser making the 'reload click' noise an extra time. Which makes it better. :P
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Good points. Very often people come back and complain to me that the header() gave them error, such as header already sent message. However, it is common that we use the same script to process the form. This is so that validation can be performed and resubmitted if needed. Only if it passed validation that we perhaps want to call a different script. We could provide a link for the user to click on, or do this automatically using redirect such as header() that I suggested.

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    There's no reason that you cant do validation at the header - if it fails, you can include_once('form.php); die(); (or, if you really felt the need, you could push a header and force a reload again).

    As long as you use include_once instead of include, there's no real need for it.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  9. #9
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Good to know. Thanks!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •