Welcome to WebmasterWorld Guest from 54.162.151.77

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Creating a PHP variable to include "Listing Title" in subject of form

     
9:57 am on Apr 1, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


I will be up front with you guys, I am not a PHP n00b, as in, I'm not even knowledgable enough to consider myself a n00b. I've installed a server application for managing real estate listings and there is a 'contact agent' form. For the subject of the email, it enters the listing id#, which is of no use. I would like it to say the address. I've figured out what the SQL query is that I need to run to get this data (first time working with SQL too but I tested this query and it worked), so now I just need help figuring out how to turn that into a variable.

Currently, the section that sets up the subject of the email says this:
$subject = $lang['email_in_reference_to_listing'] . $listing_ID;


I'd like to be able to change it to this

$subject = $lang['email_in_reference_to_listing'] . $listing_address;
so that it looks up the address using the query I set up. I just don't know how to tell it what to tell $listing_address to do or where to put it.

This is the SQL query that needs to run:
SELECT listingsdb_title FROM `default_en_listingsdb` WHERE listingsdb_id='$listing_id'


I believe this is where the $listing_id variable is being defined, so perhaps I need to set it up in there somewhere?
{
global $conn, $config, $lang;
require_once($config['basepath'] . '/include/misc.inc.php');
$misc = new misc();
$display = '';
$error = array();
$listing_id=intval($listing_id);
$agent_id=intval($agent_id);
if ($agent_id == 0) {
if ($listing_id != 0) {
$sql_listing_id = $misc->make_db_safe($listing_id);
$sql = 'SELECT userdb_id FROM ' . $config['table_prefix'] . 'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$agent_id = $misc->make_db_unsafe($recordSet->fields['userdb_id']);
}


I know this is probably an easy question for all of you, so forgive my ignorance. Any help is greatly appreciated.
1:34 pm on Apr 1, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


Welcome to WebmasterWorld, rabeatz.

The query selects the field being returned. It looks like the user's id is the agent ID and that is what is being displayed, not the listing ID. See the bold below?

$sql = 'SELECT userdb_id FROM ' . $config['table_prefix'] .
'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$agent_id = $misc->make_db_unsafe($recordSet->fields['userdb_id']);

That is the field being read from the database table for that listing ID. You will need to find the field name of the data you want and change it in both places (bold) here.

fixed sidescroll

[edited by: coopster at 2:32 pm (utc) on Apr 1, 2011]

1:41 pm on Apr 1, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


I could be wrong, but I'm quite sure it's listing ID that shows up. The code is written in a way to try to use agent_ID first and then listing ID if that's not available.

Here is the full code:

* contact
* This class contains all functions related to contacting people agents and friends about listings.
*
* @author Ryan Bonham
* @copyright Copyright (c) 2005
*/
class contact {
/**
* Contact::ContactAgentForm()
*
* @param integer $listing_id This should hold the listing ID. Listing_id is used only if agent_id is not set
* @param integer $agent_id This should hold the agent id
* @return
*/
function ContactAgentForm($listing_id = 0, $agent_id = 0)
{
global $conn, $config, $lang;
require_once($config['basepath'] . '/include/misc.inc.php');
$misc = new misc();
$display = '';
$error = array();
$listing_id=intval($listing_id);
$agent_id=intval($agent_id);
if ($agent_id == 0) {
if ($listing_id != 0) {
$sql_listing_id = $misc->make_db_safe($listing_id);
$sql = 'SELECT userdb_id FROM ' . $config['table_prefix'] . 'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$agent_id = $misc->make_db_unsafe($recordSet->fields['userdb_id']);
}
}
if (isset($_POST['message'])) {
// Make sure there is a message
if( ($_SESSION['security_code'] != md5($_POST['security_code'])) && $config["use_email_image_verification"] == 1 ) {
$error[] = 'email_verification_code_not_valid';
}
if (trim($_POST['name']) == '') {
$error[] = 'email_no_name';
}
if (trim($_POST['email']) == '') {
$error[] = 'email_no_email_address';
}elseif ($misc->validate_email($_POST['email']) !== true) {
$error[] = 'email_invalid_email_address';
}
if (trim($_POST['subject']) == '') {
$error[] = 'email_no_subject';
}
if (trim($_POST['message']) == '') {
$error[] = 'email_no_message';
}
}
if (count($error) == 0 && isset($_POST['message'])) {
// Grab Agents Email
$sql_agent_id = $misc->make_db_safe($agent_id);
$sql = 'SELECT userdb_emailaddress FROM ' . $config['table_prefix'] . 'userdb WHERE userdb_id = ' . $sql_agent_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
if ($config["include_senders_ip"] == 1) {
$_POST['message'] .= "\r\n" . $lang['senders_ip_address'] . $_SERVER["REMOTE_ADDR"];
}
if ($recordSet->RecordCount() != 0) {
$emailaddress = $misc->make_db_unsafe($recordSet->fields['userdb_emailaddress']);
// Send Mail
$sent = $misc->send_email($_POST['name'], $_POST['email'], $emailaddress, $_POST['message'], $_POST['subject']);
if ($sent === true) {
$display .= $lang['email_listing_agent_sent'];
}else {
$display .= $sent;
}
}
}else {
if (count($error) != 0) {
foreach ($error as $err) {
$display .= '<div class="error_text">' . $lang[$err] . '</div>';
}
}
$name = '';
$email = '';
$subject = '';
if ($listing_id !== 0) {
$subject = $lang['email_in_reference_to_listing'] . $listing_ID;
}
$message = '';
if (isset($_POST['message'])) {
$email = stripslashes($_POST['email']);
$name = stripslashes($_POST['name']);
$message = stripslashes($_POST['message']);
$subject = stripslashes($_POST['subject']);
}else {
$message = $lang['email_in_reference_to_listing1'] . $config['baseurl'] . '/index.php?action=listingview&listingID=' . $listing_id;
}
$display .= '<form name="contact_agent" method="post" action="index.php?action=contact_agent&amp;popup=yes&amp;listing_id=' . $listing_id . '&amp;agent_id=' . $agent_id . '">
<table border="0" cellspacing="2" cellpadding="4">
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="name">' . $lang['email_your_name'] . '&nbsp;&nbsp;</label>
<input id="name" name="name" value="' . htmlentities($name) . '" type="text" size="50">
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="email">' . $lang['email_your_email'] . '&nbsp;&nbsp;&nbsp;</label>
<input id="email" name="email" value="' . htmlentities($email) . '" type="text" size="50">
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="subject">' . $lang['email_your_subject'] . '</label>
<input id="subject" name="subject" value="' . htmlentities($subject) . '" type="text" size="50">
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="message">' . $lang['email_your_message'] . '</label>
<br />
<br />
<textarea id="message" name="message" rows="5" cols="50">' . htmlentities($message) . '</textarea>
</td>
</tr>';
if($config["use_email_image_verification"] == 1 ) {
$display .= '<tr>
<td colspan="2"><img src="'.$config['baseurl'].'/include/class/captcha/captcha_image.php" /></td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="security_code">' . $lang['email_verification_code'] . '</label>
<input id="security_code" name="security_code" type="text" />
</td>
</tr>';
}
$display .= '<tr>
<td colspan="2"><input type="submit" name="Submit" value="' . $lang['email_send'] . '">
</td>
</tr>

</table>
</form>';
}
return $display;
}
/**
* Contact::ContactFriendForm()
*
* @param integer $listing_id This should hold the listing ID that you aer emailing your friend about.
* @return
*/
function ContactFriendForm($listing_id)
{
global $conn, $config, $lang;
require_once($config['basepath'] . '/include/misc.inc.php');
$misc = new misc();
$display = '';
$error = array();
if (isset($_POST['message'])) {
// Make sure there is a message
if( ($_SESSION['security_code'] != md5($_POST['security_code'])) && $config["use_email_image_verification"] == 1 ) {
$error[] = 'email_verification_code_not_valid';
}
if (trim($_POST['name']) == '') {
$error[] = 'email_no_name';
}
if (trim($_POST['email']) == '') {
$error[] = 'email_no_email_address';
}
elseif ($misc->validate_email($_POST['email']) !== true) {
$error[] = 'email_invalid_email_address';
}
if (trim($_POST['friend_email']) == '') {
$error[] = 'email_no_email_address';
}elseif ($misc->validate_email($_POST['friend_email']) !== true) {
$error[] = 'email_invalid_email_address';
}
if (trim($_POST['subject']) == '') {
$error[] = 'email_no_subject';
}
if (trim($_POST['message']) == '') {
$error[] = 'email_no_message';
}
}
if (count($error) == 0 && isset($_POST['message'])) {
// Send Mail
$sent = $misc->send_email($_POST['name'], $_POST['email'], $_POST['friend_email'], $_POST['message'], $_POST['subject']);
if ($sent === true) {
$display .= $lang['email_listing_sent'] . ' ' . $_POST['friend_email'];
}else {
$display .= $sent;
}
}else {
if (count($error) != 0) {
foreach ($error as $err) {
$display .= '<div class="error_text">' . $lang[$err] . '</div>';
}
}
$name = '';
$email = '';
$subject = '';
// $friend_name = '';
$friend_email = '';
$message = '';
if (isset($_POST['message'])) {
$email = stripslashes($_POST['email']);
$name = stripslashes($_POST['name']);
$message = stripslashes($_POST['message']);
$subject = stripslashes($_POST['subject']);
// $friend_name = $_POST['friend_name'];
$friend_email = stripslashes($_POST['friend_email']);
}else {
$subject = $lang['email_in_reference_to_listing'] . $listing_field_Address;
$message = $lang['email_listing_default_message'] . "\r\n\r\n" . $config['baseurl'] . '/index.php?action=listingview&amp;listingID=' . $listing_id;
}
$display .= '<form name="contact_friend" method="post" action="index.php?action=contact_friend&amp;popup=yes&amp;listing_id=' . $listing_id . '">
<table border="0" cellspacing="2" cellpadding="4">
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="name">' . $lang['email_your_name'] . '&nbsp;&nbsp;</label>
<input id="name" name="name" value="' . htmlentities($name) . '" type="text" size="50">
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="email">' . $lang['email_your_email'] . '&nbsp;&nbsp;&nbsp;</label>
<input id="email" name="email" value="' . htmlentities($email) . '" type="text" size="50">
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="email">' . $lang['email_friend_email'] . '&nbsp;&nbsp;&nbsp;</label>
<input id="email" name="friend_email" value="' . htmlentities($friend_email) . '" type="text" size="50">
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="subject">' . $lang['email_your_subject'] . '</label>
<input id="subject" name="subject" value="' . htmlentities($subject) . '" type="text" size="50">
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="message">' . $lang['email_your_message'] . '</label>
<br />
<br />
<textarea id="message" name="message" rows="5" cols="50">' . htmlentities($message) . '</textarea>
</td>
</tr>
</tr>';
if($config["use_email_image_verification"] == 1 ) {
$display .= '<tr>
<td colspan="2"><img src="'.$config['baseurl'].'/include/class/captcha/captcha_image.php" /></td>
</tr>
<tr>
<td colspan="2" style="vertical-align: top" class="TitleColor"><label for="security_code">' . $lang['email_verification_code'] . '</label>
<input id="security_code" name="security_code" type="text" />
</td>
</tr>';
}
$display .= '<tr>
<tr>
<td colspan="2"><input type="submit" name="Submit" value="' . $lang['email_send'] . '">
</td>
</tr>
</table>
</form>';
}
return $display;
}
}

?>
2:51 pm on Apr 1, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


$_POST['subject'] is the variable being used in what appears to be the class email function. And that comes from the POST data in the HTML form which is apparently being initially populated at this section of code in the contact agent form class:
$subject = ''; 
if ($listing_id !== 0) {
$subject = $lang['email_in_reference_to_listing'] . $listing_ID;
}

You'll notice it is different in the contact friend class at initial form load:
$subject = $lang['email_in_reference_to_listing'] . $listing_field_Address;

But I cannot see where the $listing_field_Address is coming from so if it is not in your contact friend HTML form when you first pull it up, you likely have an error in your code somewhere because you are not first retrieving that value.

But in order to get that value in your agent form, you'll need to do something similar. Retrieve that value from the database and change the initial value.
3:09 pm on Apr 1, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


Sorry, Listing_Field_address is something I put in there hoping it'd work the other day, when I really knew nothing about what it was doing. Now I know it's pulling info from the database, so $listing_field_address does nothing.. I know what value I need to retrieve and where to put it, I just don't know how to write a code/define the variable to do it.

What I need to know is how to define what $listing_field_address (or whatever I want to call it) is so that it locates the value that it needs to locate. I want it to be the value that can be found using
SELECT listingsdb_title FROM `default_en_listingsdb` WHERE listingsdb_id='$listing_id'


For example, say that
$listing_id
on a property is 4.
SELECT listingsdb_title FROM `default_en_listingsdb` WHERE listingsdb_id='4'
or really
WHERE listingsdb_id='$listing_id'
would give me the address for 4. I want to make that result (the address) show up in the subject, not the listing id.

I'm very new to PHP so I apologize if I'm confusing anyone.
7:57 pm on Apr 1, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


No, you are making perfect sense. Read the second post again, paying attention to the bold field identifiers. I'm not familiar with your application but from what I can tell from your source code here it looks like you need to specify your field name in place of the bold identifiers.

$sql = 'SELECT address_field_name FROM ' . $config['table_prefix'] .
'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$address_field_name = $misc->make_db_unsafe($recordSet->fields['address_field_name']);
8:03 pm on Apr 1, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


I will give it a shot. If it works you're totally saving me tons of headache.
9:37 pm on Apr 1, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


It worked! Thanks for your support. Is there any way to hide that subject field now so the end user can't change it?
9:52 pm on Apr 1, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


Nvm, figured it out on my own!
12:47 am on Apr 2, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


Nice job :)
7:41 pm on Apr 4, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


Actually, it isn't fixed. It never sends messages anymore. If i change it back to the original code, it seems to work though.

I changed the bold areas in the original code.. should I have added new code instead? It seems that in order for it to send to the correct address it still needs to perform the function that it was originally doing..

Original code that works in sending the email:

{
global $conn, $config, $lang;
require_once($config['basepath'] . '/include/misc.inc.php');
$misc = new misc();
$display = '';
$error = array();
$listing_id=intval($listing_id);
$agent_id=intval($agent_id);
if ($agent_id == 0) {
if ($listing_id != 0) {
$sql_listing_id = $misc->make_db_safe($listing_id);
$sql = 'SELECT userdb_id FROM ' . $config['table_prefix'] . 'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$agent_id = $misc->make_db_unsafe($recordSet->fields['userdb_id']);
}


The code that pulls the address like I'd like, but doesn't succesfully send the email out:
{
global $conn, $config, $lang;
require_once($config['basepath'] . '/include/misc.inc.php');
$misc = new misc();
$display = '';
$error = array();
$listing_id=intval($listing_id);
$agent_id=intval($agent_id);
if ($agent_id == 0) {
if ($listing_id != 0) {
$sql_listing_id = $misc->make_db_safe($listing_id);
$sql = 'SELECT listingsdb_title FROM ' . $config['table_prefix'] .
'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$listingsdb_title = $misc->make_db_unsafe($recordSet->fields['listingsdb_title']);
}


I guess I need to somehow combine the two?
7:53 pm on Apr 4, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


Yeah, probably. The form as it exists must depend on the existing code as is, you'll have to insert your code to work alongside the original. You can either add your update to the area where the form first gets filled in by the server before displaying it or after the form is submitted to the server you can override the subject every time no matter what. Or both.
6:33 pm on Apr 5, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


Ok, got it working now, for sure.

Did both, I had tried that before but it didn't work, so I changed the order I had them in.

{
global $conn, $config, $lang;
require_once($config['basepath'] . '/include/misc.inc.php');
$misc = new misc();
$display = '';
$error = array();
$listing_id=intval($listing_id);
$agent_id=intval($agent_id);
if ($agent_id == 0) {
if ($listing_id != 0) {
$sql_listing_id = $misc->make_db_safe($listing_id);
$sql = 'SELECT listingsdb_title FROM ' . $config['table_prefix'] .
'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$listingsdb_title = $misc->make_db_unsafe($recordSet->fields['listingsdb_title']);
}
global $conn, $config, $lang;
require_once($config['basepath'] . '/include/misc.inc.php');
$misc = new misc();
$display = '';
$error = array();
$listing_id=intval($listing_id);
$agent_id=intval($agent_id);
if ($agent_id == 0) {
if ($listing_id != 0) {
$sql_listing_id = $misc->make_db_safe($listing_id);
$sql = 'SELECT userdb_id FROM ' . $config['table_prefix'] . 'listingsdb WHERE listingsdb_id = ' . $sql_listing_id;
$recordSet = $conn->Execute($sql);
if ($recordSet === false) {
$misc->log_error($sql);
}
$agent_id = $misc->make_db_unsafe($recordSet->fields['userdb_id']);
} }
}


I just changed the input tag for the subject form to be readonly so the user would not remove the address from the email.
9:10 pm on Apr 5, 2011 (gmt 0)

New User

5+ Year Member

joined:Apr 1, 2011
posts: 9
votes: 0


Thanks again for all your help, this has definetly been a learning experience for me, just figured out how to add a new field to the form and pass that into the message of the email too. Just glad to have it working the way I want and knowing I can change things around..