hi,

i have a site where the user can copy and paste a letter they typed on their pc into an HTML editor in the site and when inserted goes into the database.

what i need to be able to do is have a button when inserting or a specific code tag that when they are keying on the letter they can be specific to where the want the clients name or address to appear.

at the moment i can create a default of the clients address by echo on the page where the address should be and the "dear client name ,..."part

however i need to a bit like in an access/word mail merge you can put in <Name> anywhere in the doc or <address> and when generated it displays the name and address anywhere they have specified.

i need this to be for an end user inputting so a button that auto adds this in when keying the letter would be a great idea or something simple like <name> but i have no idea how to get this one created?

any help appreciated.

thanks

I assume your using a wysiwyg editor?
So when the user types in <name> it is converted to &lt;name&gt;
and your database might have a query like:
$row = mysql_query($sql, $conn);
which may get the rows $row and $row
so that means you can use a string replace to insert the name as follows

$message = $_POST['message'];
$name = $row['name'];
$address = $row['address'];
$message = str_replace('&lt;name&gt;', $name, $message);
$message = str_replace('&lt;address&gt;', $address, $message);

all sorted :)
I'm sure you can configure this more to your needs.
there is a full list of html character references here

You can just copy the image :)

hey thanks for the reply, sorry for long time not reply.

looking at i am not sure how i would exactly impliment into the system?

can you help with a bit more detail?

i have a mysql database behind the php site, i want to add in placeholders when they are inserting the letter into the database and then when the produce that letter for a client it recognizes where the user had put the place holders to replace with a value from the database?

sorry but if you could guide me a bit further i would appreciate.

many thanks

I'm guessing the previous poster slightly misunderstood. You will have variables like $name and $address in the script, correct? And anywhere the user enters <name> you want to put $name in there? str_replace is the way to do it.

$name = "John Smith";
$raw_message = $_POST['message'];
$message = str_replace("<name>", $name, $raw_message);

Here's what it will do:
Input:
Dear <name>, I am writing to inform you of some random event. I know, <name>, that you are busy. But your name( <name> ) is really awesome. Thanks, <name>.

After the str_replace, $message will be:
Dear John Smith, I am writing to inform you of some random event. I know, John Smith, that you are busy. But your name( John Smith ) is really awesome. Thanks, John Smith.

(bold inserted to point out name)

thanks that is exactly what i was thinking.

however how could i set "John Smith" to a value from the database?

appreciate your help, also how would i start the script off, would it be in the page where i have the form or would it be called from a seperate page?

just not entirely sure how you call the script to be honest.

cheers

i want to add in placeholders when they are inserting the letter into the database and then when the produce that letter for a client it recognizes where the user had put the place holders to replace with a value from the database?

If that is what you want to do(I'm assuming you're creating form letters, and when the client views it, the client's name gets inserted), you will need to put this in the display section of the script. I can't tell you exactly where without seeing your code, but it would be write before the part where you actually display the message. Example:

// $letterid should be the id of the specific letter you want
$query = "SELECT letter FROM Letters WHERE letterid='$letterid'";
$result = mysql_query($query);

$letter_array = mysql_fetch_array($result);
$letter = $letter_array['letter'];

// $clientid should be the id of the client who's viewing the letter
$query = "SELECT name FROM Clients WHERE clientid='$clientid'";
$result = mysql_query($query);

$client_info = mysql_fetch_array($result);

$letter = str_replace("<name>", $client_info['name'], $letter);

echo $letter;

thanks for this, what if i wanted to bulk create the letters? this is good for one letter at a time. but say i wanted to generate 100 in one go and print off?

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.