1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

email won't send.- This should be an easy fix for someone better than me.

Discussion in 'PHP' started by Mitchell, Jul 18, 2010.

  1. #1
    I think there is a code mistake on this page. It seems to be receiving the POST info. If I write echo $email; at the end of the first and largest if statement, it will echo my email address.

    If I write $to="my_email_address.com"; instead of $to=$email, an email will be sent to me. Some how it seems my email address is not getting into the $to variable.

    Could it be the way the two largest if and if else statements are coded. Can someone see something I can't? Thanks.

    
    <?php
        include 'conn_mysqli.inc.php';
        
        $errString = "";
    
        if( isset($_POST['preview']) ){
        
            $confirm_code = md5(uniqid(rand()));
            //create short variable names    
            if (isset($_POST['title']))         $title = $_POST['title'];
            if (isset($_POST['email']))         $email = $_POST['email'];
            if (isset($_POST['edit']))             $editp = $_POST['edit'];
            
            #...............................................................................................................
            
            if(!$title || !$email) $errString .= "<p>All fields are required.</p>";
            
            if (!$editp){
                
                $query = "INSERT INTO whatever values ('$title','$email','$confirm_code')";
                if( !$db->query( $query ) ){
                    $errString .= "<p>There is unknow error with database.</p>";
                }
            } 
        #...................................................................................................................
        }else if( isset($_REQUEST['confirm'])) {
        
            $confirm_code = $_REQUEST['confirm'];
            $to=$email;
            $subject="Your confirmation link here";
            $header="from: your name <your email>";
            
            $message="http://www.website.com/confirmation.php?confirm=".$confirm_code;
            // '@' will not display messages like you don't have insatlled SMTP on localhost etc ..
            $sentmail = @mail($to,$subject,$message,$header);
            echo "check your email.";
            exit();
        }
        
    ?>
    <table width="600" align="center">
    <tr><td><font color="#990000"><?php echo $errString; ?></font></td></tr>
    <tr><td><strong>Post Title:</strong> <?php echo $title; ?></td></tr>
    <tr><td><strong>E-mail:</strong> <?php echo $email; ?></td></tr>
    <tr><td><input type="submit" value=" Edit " onclick="location.replace('post12.php?edit=<?php echo $confirm_code; ?>')" /> &nbsp;
    <input type="submit" value=" Confirm " onclick="location.replace('preview_post12.php?confirm=<?php echo $confirm_code; ?>')" /></td></tr>
    </table>
    
    PHP:

     
    Mitchell, Jul 18, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is this the whole script?
     
    Deacalion, Jul 18, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's the second page. The first page is to post info. It has a submit button named preview.

    I removed some items to make it simpler to anyone here to look at.
     
    Mitchell, Jul 18, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok. So you are storing the fields from POST in a table so you can pull them back out when the user has confirmed?
    Have you tried dumping $to when the user has confirmed to see what it really is?

    Oh... and an alternative to using the database to store data between pages is HTML hidden fields, an example:
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    <input type="text" name="text" />
    <input type="submit" value="Save" />
    
    <?php
    if (isset($_GET['text'])) {
        echo '<br />' . $_GET['text'] . '<input type="hidden" value="'.$_GET['text'].'" name="hidden'.time().'" /><br />';
                        
        foreach ($_GET as $k => $v)
            if (preg_match('/hidden\d+/', $k))
                echo '<input type="hidden" value="'.$v.'" name="'.$k.'" />' . $v . '<br />'; 
    }
    ?>
    
    </form>
    
    PHP:
     
    Deacalion, Jul 18, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Good lord. That's awesome technically speaking, but I'm new at this. I am not even sure what you mean by dumping $to.

    I may have echoed $to and got nothing. I will have to try that.
     
    Mitchell, Jul 18, 2010 IP
  6. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah, they are very handy. All you have to do is check for a form field, then have the page generate it as a hidden field. So you can continue passing on variables page after page, with no database.
    
    // if I've had the text field submitted to me from a form, then output it as a hidden field so the next form can receive the value
    if ($_GET['text']) {
        echo '<input type="hidden" name="text" value="'.$_GET['text'].'" />';
    }
    
    PHP:
    And by dump, I mean output/echo - as in the function var_dump(). :)
     
    Deacalion, Jul 18, 2010 IP
    mjewel likes this.
  7. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I added echo $email; and echo $to below. It did echo my email but nothing echoed from $to.

    
    <?php
    	include 'conn_mysqli.inc.php';
    	
    	$errString = "";
    
    	if( isset($_POST['preview']) ){
    	
    		$confirm_code = md5(uniqid(rand()));
    		//create short variable names	
    		if (isset($_POST['title'])) 		$title = $_POST['title'];
    		if (isset($_POST['email'])) 		$email = $_POST['email'];
    		if (isset($_POST['edit'])) 			$editp = $_POST['edit'];
    		
    		#...............................................................................................................
    		
    		if(!$title || !$email) $errString .= "<p>All fields are required.</p>";
    		
    		if (!$editp){
    			
    			$query = "INSERT INTO whatever values ('$title','$email','$confirm_code')";
    			if( !$db->query( $query ) ){
    				$errString .= "<p>There is unknow error with database.</p>";
    			}
    		} 
    		echo $email;
    	#...................................................................................................................
    	}else if( isset($_REQUEST['confirm'])) {
    	
    		$confirm_code = $_REQUEST['confirm'];
    		$to=$email;
    		$subject="Your confirmation link here";
    		$header="from: your name <your email>";
    		
    		$message="http://www.website.com/confirmation.php?confirm=".$confirm_code;
    		// '@' will not display messages like you don't have insatlled SMTP on localhost etc ..
    		$sentmail = @mail($to,$subject,$message,$header);
    		echo "check your email.";
    		echo $to;
    		exit();
    	}
    	
    ?>
    <table width="600" align="center">
    <tr><td><font color="#990000"><?php echo $errString; ?></font></td></tr>
    <tr><td><strong>Post Title:</strong> <?php echo $title; ?></td></tr>
    <tr><td><strong>E-mail:</strong> <?php echo $email; ?></td></tr>
    <tr><td><input type="submit" value=" Edit " onclick="location.replace('post12.php?edit=<?php echo $confirm_code; ?>')" /> &nbsp;
    <input type="submit" value=" Confirm " onclick="location.replace('preview_post12.php?confirm=<?php echo $confirm_code; ?>')" /></td></tr>
    </table>
    
    PHP:
     
    Mitchell, Jul 18, 2010 IP
  8. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I performed a var_dump($email);and got string(28) "myemail@here.net" and var_dump($to); and got a value of NULL.

    By the way thanks for your help.

    
     var_dump($to); and var_dump($email);
        if( isset($_POST['preview']) ){
            $confirm_code = md5(uniqid(rand()));
            //create short variable names    
            if (isset($_POST['title']))         $title = $_POST['title'];
            if (isset($_POST['email']))        $email = $_POST['email'];
            if (isset($_POST['edit']))            $editp = $_POST['edit'];
            
            #...............................................................................................................
            
            if(!$title || !$email) $errString .= "<p>All fields are required.</p>";
            
            if (!$editp){
                
                $query = "INSERT INTO whatever values ('$title','$email','$confirm_code')";
                if( !$db->query( $query ) ){
                    $errString .= "<p>There is unknow error with database.</p>";
                }
            } 
            var_dump($email);
        #...................................................................................................................
        }else if( isset($_REQUEST['confirm'])) {
        
            $confirm_code = $_REQUEST['confirm'];
            $to=$email;
            $subject="Your confirmation link here";
            $header="from: your name <your email>";
            
            $message="http://www.website.com/confirmation.php?confirm=".$confirm_code;
            // '@' will not display messages like you don't have insatlled SMTP on localhost etc ..
            $sentmail = @mail($to,$subject,$message,$header);
            echo "check your email.";
            var_dump($to);
            exit();
        }
    
    PHP:
     
    Mitchell, Jul 18, 2010 IP
  9. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I get two NULL values. As $email passes into the else if section it looses it's value.

    
    $sentmail = @mail($to,$subject,$message,$header);
    		echo "check your email.";
    		var_dump($to);
    		var_dump($email);
    		exit();
    
    PHP:
     
    Mitchell, Jul 18, 2010 IP
  10. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Can anyone figure this out?

    Do the larger if and else if statements make any sense how they're laid out together?

    Do I need to add this in the else if statment below?
    
    if (isset($_POST['email']))        $email = $_POST['email'];
    
    
    PHP:
    </span>
     
    Mitchell, Jul 19, 2010 IP
  11. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The code basically says:

    If the user clicked the preview button on the form page (post12.php previous page), then collect and process this info. Else if the user clicked on the confirm button below on this preview page (preview_post12.php), then send the user a confirmation email.

    Can someone tell me why the $email is empty or (NULL) in the else if statement?

    Is there something about an else if statement that nullifies variables?

    All the code in my php form files collect, process and email confirmation works except it won't send me an email unless I manually enter $to = instead of $to = $email. Then I will receive an email. Every where else the $email holds an email address.

    Thanks.
     
    Mitchell, Jul 30, 2010 IP