Case switching

11 posts by 2 authors in: Forums > CMS Builder
Last Post: October 1, 2008   (RSS)

By terryally - September 5, 2008

Hiya,

Need some help.

I am trying to use a switch on a contact form. I am using the multi menu type to populate the dropdown box as illustrated below.

<?php // Select email contact from drop down box ?>

<select class="inp" size="1" name="emailto">
<option>Select where to send this email enquiry</option>

<?php foreach ($my_contact_directoryRecords as $record): ?>

<option value="<?php echo $record['case'] ?>"><?php echo $record['recipient'] ?></option>
<?php endforeach;?>
</select>


This produces the following:

<select class="inp" size="1" name="emailto">
<option>Select where to send this email enquiry</option>
<option value="admin">Administration</option>
<option value="accts">Accounts</option>
<option value="cab">Conferences and Banquetting</option>
<option value="events">Private Events & Special Occasions</option>
<option value="res">Reservations</option>
</select>




However my problem comes with the switch. Of course I do not want to reveal the email addresses online so I set up the switch below. I have tried to replicate this unsuccessful and need some assistance.


$emailto = $_POST['emailto'];

switch ($emailto) { // select addressee
case "admin":
$recipient = "Administration";
$emailto = "admin@mydomain.com";
break;
case "accts":
$recipient = "Accounts";
$emailto = "accounts@mydomain.com";
break;
case "cab":
$recipient = "Conferences and Banquetting";
$emailto = "conferences@mydomain.com";
break;
case "events":
$recipient = "Private Events & Special Occasions";
$emailto = "events@mydomain.com";
break;
case "res":
$recipient = "Reservations";
$emailto = "reservation@mydomain.com";
break;
default:
echo '<p><font color="red" size="+1">Please select a destination from the drop down box!</font></p>';
}

Re: [terryally] Case switching

By Dave - September 5, 2008

Hi Terry,

What I usually do at this point is print out all my values to make sure they are what I think they are. Try this after your switch code:

print "emailto (from POST) = {$_POST['emailto']}<br/>\n";
print "emailto = $emailto<br/>\n";
print "recipient= $recipient<br/>\n";

Also, don't set $emailTo twice. It's best not to use a variable for two different things. Try this instead:

switch ($_POST['emailto']) { // select addressee

Then you'll only be setting it inside of the switch block.

Let me know if that helps any! :)
Dave Edis - Senior Developer

interactivetools.com

Re: [Dave] Case switching

By terryally - September 5, 2008

Hi Dave,

What I have been trying to replicate with the switch is as follows but it is not working:

switch ($emailto) { // select addressee

<?php foreach ($my_contact_directoryRecords as $record): ?>

case "echo $record['case'];":
$recipient = "echo $record['recipient'];";
$emailto = "echo $record['emailto'];";
break;
<?php endforeach;?>
default:
echo '<p><font color="red" size="+1">Please select a destination from the drop down box!</font></p>';
}


Basically, if I were writing a MySQL/PHP SELECT/QUERY statement I would while a WHILE loop at that point to populate the switch statement. But with CMS B, I am wondering how to achieve this.

I tried the above but I got an error message.

Terry

Re: [terryally] Case switching

By Dave - September 8, 2008

You can't really generate PHP code with PHP code and then run it (or at least usually you shouldn't).

What are you trying to do? There may be a more direct way. Are you trying to lookup a email address from the database based on a nickname and then use that in an email form?
Dave Edis - Senior Developer

interactivetools.com

Re: [terryally] Case switching

By Dave - September 8, 2008

Yes, the code would look something like this (untested):

// lookup email alias
$emailAlias = $_POST['emailto'];
$escapedAlias = mysql_real_escape_string($_POST['emailto']);

list($records) = getRecords(array(
'tableName' => 'yourTableName',
'where' => " recipient = '$escapedAlias' ",
'limit' => 1
));
$record = @$records[0]; // get first record

if (!$record) { die("Couldn't find email for alias '$emailAlias'"); }

print "Recipient: $record['recipient']<br/>\n";
print "Email: $record['emailto']<br/>\n";


Hope that helps!
Dave Edis - Senior Developer

interactivetools.com

Re: [Dave] Case switching

By terryally - September 10, 2008 - edited: September 10, 2008

Tested and works wonderfully.

Thanks Dave!!

Re: [terryally] Case switching

By terryally - September 12, 2008

Hi Dave,

I've further tested this.

It works really well as a standalone but when I integrate it into an ISSET statement it does not.

Me thinks that instead of POSTing to the same page I would have to send it to another page for this to work.

I do not have the time right now to experiment as I am on a very tight deadline to deliver three websites but later this month I am going to experiment further since this seems a far better solution than hard coding a case switch. At that time, I will post here again and let you know the outcome.

Cheers
Terry

Re: [terryally] Case switching

By Dave - September 12, 2008

Ok great, thanks for posting back! :) Let us know what the outcome is.
Dave Edis - Senior Developer

interactivetools.com

Re: [Dave] Case switching

By terryally - September 30, 2008



// lookup email alias
$emailAlias = $_POST['emailto'];
$escapedAlias = mysql_real_escape_string($_POST['emailto']);

list($records) = getRecords(array(
'tableName' => 'yourTableName',
'where' => " recipient = '$escapedAlias' ",
'limit' => 1
));
$record = @$records[0]; // get first record

if (!$record) { die("Couldn't find email for alias '$emailAlias'"); }

print "Recipient: $record['recipient']<br/>\n";
print "Email: $record['emailto']<br/>\n";




Hi Dave,

I've tried but I cannot get the above code integrated and working, so I resorted to writing a query below which does the trick.

Terry


$emailAlias = $_POST['emailto'];
$escapedAlias = mysql_real_escape_string($_POST['emailto']);


$query = "SELECT * FROM cmsb_lakeside_contact_directory WHERE rcptid='$escapedAlias' LIMIT 1";
$result = mysql_query ($query);
$record = mysql_fetch_array ($result, MYSQL_BOTH);

if (record) {

$sendto = $record[email];
$recipient = $record[recipient];

}

The second last variable is meant to call the 'email' value but your system is stripping my code.