Need help in PHP

Discussion in 'PHP' started by luckmann, Jun 13, 2011.

  1. #1
    <?php
    
    if(!$_POST) exit;
    
    $email = $_POST['email'];
    
    if($errors==1) echo $error;
    else{
    	$values = array ('name','message');
    	$required = array('name','message');
    	 
    	$your_email = "email@address";
    	$email_subject = "New Message: ".$_POST['subject'];
    	$email_content = "new message:\n";
    	
    	foreach($values as $key => $value){
    	  if(in_array($value,$required)){
    		if ($key != 'subject' && $key != 'name') {
    		  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 '<-------Here I guess---------------->';
    
    	} else {
    		echo 'ERROR!';
    	}
    }
    ?>
    Code (markup):

    How can i make this code call a url or redirect to URL on successful submit?

    Thanks
     
    luckmann, Jun 13, 2011 IP
  2. krishmk

    krishmk Well-Known Member

    Messages:
    1,376
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    185
    #2
    you can use header function to redirect (assuming no output is sent during the form process)
    header("Location:http://redirecturl.com");
     
    krishmk, Jun 13, 2011 IP
  3. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #3
    This will do:
    
    <?php
    if(!$_POST) exit;
    $email = $_POST['email'];
    if($errors==1) echo $error;
    else{
    	$values = array ('name','message');
    	$required = array('name','message');
    	 
    	$your_email = "email@address";
    	$email_subject = "New Message: ".$_POST['subject'];
    	$email_content = "new message:\n";
    	
    	foreach($values as $key => $value){
    	  if(in_array($value,$required)){
    		if ($key != 'subject' && $key != 'name') {
    		  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)) {
                header('location:redirect_here');
        } 
        else {
                echo 'ERROR!';
        }
    }
    ?>
    
    PHP:
     
    The Webby, Jun 13, 2011 IP
  4. luckmann

    luckmann Peon

    Messages:
    272
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for the help
     
    luckmann, Jun 13, 2011 IP
  5. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Replace "redirect_here" wwith the page where you want to redirect your user in this line:
    header('location:redirect_here');
    PHP:
     
    The Webby, Jun 13, 2011 IP
  6. luckmann

    luckmann Peon

    Messages:
    272
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well i got this

    Warning: Cannot modify header information - headers already sent by (output started at /888/8888/public_html/8888/contact.php:1)
     
    luckmann, Jun 13, 2011 IP
  7. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #7
    You need to make sure that there is no output before you modify the header, that includes white spaces as well..
    Have you used the code I provided, or the code in your original post?
     
    The Webby, Jun 13, 2011 IP
  8. luckmann

    luckmann Peon

    Messages:
    272
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    the code you provided :(
     
    luckmann, Jun 13, 2011 IP
  9. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #9
    PM me the content of contact.php
     
    The Webby, Jun 13, 2011 IP