Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2003
    Location
    Miami, FL
    Posts
    3,262

    Question abut str_replace & text box

    Could I use str_replace to replace a string in a text box with a variable from a text box to be emailed through php?

    Something like:



    PHP Code:
    $message $_POST['message'];

    $sql mysql_query("SELECT FirstName FROM users"); 

    while(
    $row mysql_fetch_object($sql))
    {
      
    // Send the emails in this loop.
      
    $member_name $row->FirstName

    $body str_replace('{MEMBER_NAME}'$member_name$message); 
    Then in the textbox we'd use {MEMBER_NAME} To define the member name?

    then something like

    mail(" <$email>", "$subject", "$body", "From: $sitename<$fromemail>", "-fi$fromemail");

  2. #2
    Join Date
    Mar 2003
    Location
    South Bend, Indiana, USA
    Posts
    320
    Yes, you can use use str_replace() to perform that action. You can also go more "advance" and make your code something like this:

    PHP Code:
    $replace = array('{member_name}','{member_email}','{member_password}');
    $replace_with = array($row[Firstname],$row[email],$row[password])
    $body str_replace($replace$replace_with$message); 
    Hope this helps
    Daniel
    WLScripting.com - php scripts and tutorials

  3. #3
    Note to the people reading this - using unquoted indexes within array will make php to issue a notice (unless they are previously defined with define()).

    Semantically correct would be:
    $replace_with = array($row['Firstname'],$row['email'],$row['password']);

  4. #4
    Syntactically, you mean.

  5. #5
    Regarding the syntax, there would be no syntax errors in the unqoted example. The error would occur because of undefined constants. So sintactically, using unqoted associative indexes is also correct.

  6. #6
    Join Date
    Dec 2003
    Location
    Miami, FL
    Posts
    3,262
    Thanks. I got it working with this:

    $sql = mysql_query("SELECT fname,lname,email FROM mailing");

    while($r = mysql_fetch_array($sql)){
    $fname = $r['fname'];
    $lname = $r['lname'];
    $email = $r['email'];

    $body=stripslashes($message);
    $body = str_replace('%FNAME%', $fname, $body);
    $body = str_replace('%LNAME%', $lname, $body);
    $body = str_replace('%EMAIL%', $email, $body);


    FWIW, it works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •