Jump to content

Storing variables


mattyvx

Recommended Posts

Hello all, I'm having some problems with a script ive written which is designed to allow me to send a message to all the users of my site.

 

the form works fine, the SQL works fine but im having a problem with storing php variables and outputting them in the message.

 

The form has a field named "allmessage" which is where i'll type my message, I'll type something like

 

"Hello $Name" and store the text as

 

$allmessage = $_POST['allmessage'];

 

example script

 

//loops through each member
    while ($row=mysql_fetch_array($sql)) 
    {
    
    //sets variables
    $ID=$row['ID'];
    $Name=$row['MemberName'];
    $Email=$row['Email'];

    $content_type = 'Content-Type: text/plain; charset="UTF-8"' ;

    mail($Email, $allsubject, $allmessage, $content_type);
    }
//loop ends send us a copy of the mail
mail("[email protected]", $allsubject, $allmessage, $headers );

 

Now what I want to be outputted in the email is "Hello John" or "Hello Paul"

 

but what I get is "Hello $Name".

 

Any ideas?!

 

Link to comment
https://forums.phpfreaks.com/topic/222249-storing-variables/
Share on other sites

you can't send a variable name as a string through a post and expect it to be placed into the global variable scope. if you want to replace the string "$Name" in the posted variable $allmessage, you'll need to replace it explicitly, maybe like

 

$Name=$row['MemberName'];
$allmessage = str_replace('$Name', $Name, $allmessage);

Link to comment
https://forums.phpfreaks.com/topic/222249-storing-variables/#findComment-1149685
Share on other sites

thanks, I used the replace method however at the start of each loop I needed to define a new message variable to keep the original $allmessage and variables intact.

 

...Otherwise I end up with many of the same message because once it's parsed the variables there's nothing left to replace!

 

i.e.

loop

{

$m = $allmessage;

$m = str_replace('$Email', $Email, $m);

 

...//

}

 

Works fine!

Link to comment
https://forums.phpfreaks.com/topic/222249-storing-variables/#findComment-1149701
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.