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("me@me.com", $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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.