Hi all
I have a form that requests some text input. This is then passed on to a handler script. The text input ($email_text) will be emailed out automatically using PHP... however, I want to include some variables inside the text where the submitter has used variable names such as $first_name and $last_name.
Whenever I try to do this I am unable to insert the data and the actual variable names remain there instead!
Can anyone tell me how to do this please?
Regards
Lawrence.
[SOLVED] Inserting Variables into an email
Moderator: General Moderators
- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
Did you look in php.net for mail?
Or could we have a look at your code?
Or could we have a look at your code?
Are you transforming the variables from $_POST['firstname'] to $firstname?
Check this out, maybe it can help.
viewtopic.php?t=15856
Check this out, maybe it can help.
viewtopic.php?t=15856
This is my form:
The text input $email_text is carried to process_sender.php. Here it accesses a database and retrieves $first_name etc..
This is the process_sender.php
The processor has the $email_text data PLUS the $first_name and $last_name data. The user has inserted $first_name into the email_text field when he submits the form. This is apparent when the data reaches the processor.
Lawrence.
Code: Select all
<head>
<title>Sender</title>
</head>
<body>
<form method="POST" action= "process_sender.php" >
<p>Email to:</p>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="278" id="table1">
<tr>
<td width="44"><input type="radio" value="all_verified" checked name="radio_but"></td>
<td>All Verified Subscribers</td>
</tr>
<tr>
<td width="44"><input type="radio" name="radio_but" value="non_verified"></td>
<td>Non Verified Subscribers</td>
</tr>
</table>
<p>Subject Line:&nbsp;
<input type="text" name="subject_line" size = "26" >
</p>
<p>Paste Email Here....</p>
<p><textarea rows="7" name="email_text" cols="32"></textarea></p>
<p><input type="checkbox" name="email_to_self" value="ON"> Send Test to Self!</p>
<p><input type="submit" value="Send" name="B1"><input type="reset" value="Reset" name="B2"></p>
<p>&nbsp;</p>
</form>
</body>
</html>This is the process_sender.php
Code: Select all
<?php
if ( !$radio_but or !$subject_line or !$email_text ){
echo "Not all fields are complete. Press Back! <br>";
exit ();
}
#Make the connection to my_sql
$conn = @mysql_connect("localhost", "lazersam_admin", "*****")
or die ("Sorry - could not connect to MySQL: " .mysql_error());
#Select the specified database
$rs = @mysql_select_db("lazersam_members", $conn) or die ("Err:Db");
if ($radio_but == "all_verified")
{
#Create query
$sql = "select * from subscribers where verified = "1" ";
}#End of "All Verified" If Statement
else
{
$sql = "select * from subscribers where verified = "0" ";
}
#Execute Query
$rs = mysql_query($sql, $conn) or die ("Could not do query: " .mysql_error());
#Get Num of rows
$num_of_rows = mysql_num_rows($rs);
echo "Num of email addresses found: $num_of_rows <br>";
if ($num_of_rows == 0){
echo "Error... did not find entry in database!";
exit ();
}
#Send messages
include ("headers.php");
#Get Data=====================================================
#This bit works fine...
while ($row = mysql_fetch_assoc($rs) ){
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$affiliate_id = $row['affiliate_id'];
$email = $row ['email'];
$msg = $email_text;
# Although I DO have the $first_name and $last_name could easily
# print out the data, I cant insert the data into the text string!
#
echo "$msg <br>";
#$success=mail($email, $Subject_line, $msg, $headers);
?>Lawrence.
- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
As Sami wrote, I think you need to do this
for all the variables you want to use in the e-mail.
===================================
= Yes, I'm trying to get my 101st posting today =
===================================
Code: Select all
<?php
$firstname = $_POST['firstname'];
?>===================================
= Yes, I'm trying to get my 101st posting today =
===================================
Hi
I think I have complicated my question so I have written a script to demonstate the problem...
This is a form that sends the text string "$inserted_text" to the processor.php.
The processor assigns a value to $inserted_text (namely "Insert this into the message").
It then prints the message. I would like the $inserted_text variable to be replaced by the "Insert this into the message" string.
Please can you advise?
Regards
Lawrence.
I think I have complicated my question so I have written a script to demonstate the problem...
This is a form that sends the text string "$inserted_text" to the processor.php.
Code: Select all
<html>
<head>
<title>Semd Email</title>
</head>
<body>
<form method="POST" action="process.php">
<p>Message:</p>
<p><textarea rows="2" name="message" cols="28">$inserted_text</textarea></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>It then prints the message. I would like the $inserted_text variable to be replaced by the "Insert this into the message" string.
Code: Select all
<?php
$msg = $_POST['message'];
$inserted_text = "Insert this into the message";
echo "$msg";
?>Regards
Lawrence.