Get $variable from Form post

Discussion in 'PHP' started by anversli, Jan 14, 2009.

  1. #1
    I'm making a mailing script and need some help. What I'm trying to do is, I'm sending one mail to all users and I need to use variables in this mail.

    For example:
    After form submit:

    
    $textarea = str_replace('[', '$row[', $_POST['textarea']);
    
    $body  = "<html>\n";
    $body .= "<body>\n";
    $body .= "Dear " . $row["username"] . ", <br />";  
    $body .= "".$textarea."<br>";  
    $body .= "<br><br>";
    $body .= "</body>\n";
    $body .= "</html>\n";
    
    
    $mail->Body    = $body;
    $mail->AltBody = $text_body;  
    $mail->AddAddress($row["email"], $row["username"]);  
    
    if(!$mail->Send())  
        $msg = "There has been a mail error sending to " . $row["email"] . "<br>";  
    
    // Clear all addresses and attachments for next loop  
    $mail->ClearAddresses();
    $msg = "Mail send!<br />";
    
    PHP:
    But this isn't working, I got the same text at my inbox:
    Hi [firstname],
    This is a testmail, your registration date is [regdate]



    Can someone help me with this?
     
    anversli, Jan 14, 2009 IP
  2. Jasber

    Jasber Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It looks like there's some code missing. But make sure the names inside your $_POST variables match the names of the form items.
     
    Jasber, Jan 14, 2009 IP
  3. osmasters

    osmasters Well-Known Member

    Messages:
    453
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    100
    #3
    I think below will work but not sure.
    $textarea = str_replace('[', '[firstname]', $row["username"]);

    Please give us full code here.
     
    osmasters, Jan 15, 2009 IP
  4. hassanahmad1

    hassanahmad1 Active Member

    Messages:
    150
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #4
    why dont you use this:

    
    $textarea = str_replace('[username]', $row["username"], $textarea);
    $textarea = str_replace('[regdate]', $row["regdate"], $textarea);
    
    Code (markup):
    instead of :
    
     $textarea = str_replace('[', '$row[', $_POST['textarea']);
    
    Code (markup):
     
    hassanahmad1, Jan 15, 2009 IP
  5. anversli

    anversli Active Member

    Messages:
    350
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Thanks for reply's, the reason why i'm using
    Is because, it's not only [username] that can be used. Otherwise i have to make for every mysql row a str_replace.

    As you can see in my PHP code:
    
    $body .= "Dear " . $row["username"] . ", <br />";  
    
    PHP:
    Is working, but all the other str_recplaced variables from the FORM is not.
    $body .= "".$textarea."<br>";
    PHP:
    I got only ".$row[varibale]." in my mail.
     
    anversli, Jan 15, 2009 IP
  6. Vozzek

    Vozzek Active Member

    Messages:
    206
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Not sure completely, but I don't like using the double-quote " when I'm trying to put together or concatenate a string like you are doing here. I use single quotes everywhere possible, except to begin and end the literal portions of your string (which in your case would be $body).

    Instead of using:

    I'd make it something like:

    That may or may not be your problem, but it keeps things simple and clean.
     
    Vozzek, Jan 15, 2009 IP
  7. artiskool

    artiskool Peon

    Messages:
    34
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Using eval() will do the work, assuming that the $_POST['textarea'] does not contains double quotes "

    $textarea = str_replace('[', '$row[', $_POST['textarea']);
    $textarea = eval('echo "' . $textarea . '";');
    
    PHP:
    that should work flawlessly, but if $_POST['textarea'] contains double quotes the eval() will throw an error, if this happens, you can try escaping double quotes by htmlentities or equivalent.

    Hope it helps.

     
    artiskool, Jan 15, 2009 IP