Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I wrote a PHP script that the user decides who they want to send the email  
to based on the selection from a "dropdown" box.  The essential part of the  
script that I'm having problems with is:


/* Pre-defined script variables. */
   $eol      = "\n";
   $mailto   = 'me@mycorp.net';
   $mailfrom = 'webserver@mycorp.net';
   $subject  = 'Contact Us Request';

/* Initialize a clean array to replace $_POST with clean data */
   $etype    = $_POST['etype'];
   $errmsg   = 'etype-: '.$etype.'  |  Case value-: ';

/* Define the mailto address
   switch ($etype) {
   case 0:
       $mailto = 'sales@mycorp.net';
       $errmsg .= '0';
   case 1:
       $mailto = 'support@mycorp.net';
       $errmsg .= '1';
   case 2:
       $mailto = 'webmaster@mycorp.net';
       $errmsg .= '2';
   case 3:
       $mailto = 'myacct@mycorp.net';
       $errmsg .= '3';
       $mailto = 'me@mycorp.net';
       $errmsg .= 'default';
builds an email and sends it to myself.

The email shows up at the "me" address instead of the "myacct" address.  The  
problem is the email shows "etype-: 3   |   Case value-: ".  This indicates  
that the switch didn't work at all.  Can anyone tell me why?



Re: Switch

Bill H wrote:
Quoted text here. Click to load it
Isn't it simpler to do:
   $aMailTo = array('sales@mycorp.net', 'support@mycorp.net',
                    'webmaster@mycorp.net', 'myacct@mycorp.net');
   $mailTo = 'me@mycorp.net';  // default
   if (array_key_exists($etype, $aMailTo))
     $mailTo = $aMailTo[$etype];
   $errmsg .= "$etype";

By the way, in your code above, you haven't closed off
the comment that starts on the Define the mailto address line.
Also, though it shouldn't make a difference in the switch
statement, the type of $etype is string.

Csaba Gabor from Vienna

Re: Switch


How stupid could I get.  The missing "close comment" was the problem.

I tried the array but it seems the:

  if (array_key_exists($etype, $aMailTo)) {
       $mailTo = $aMailTo[$etype];
        $errmsg .= "$etype"; }

...didn't work but:

  $mailTo = $aMailTo[$etype];

...did.  Go figure.

Thanks very much.


Quoted text here. Click to load it

Re: Switch

Bill H wrote:
Quoted text here. Click to load it



  case '0':
  case '1':

Or, when I know it's a numeric value, I use:

   $etype= isset($_POST['etype']) ? intval($_POST['etype']) ? 0;

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Switch

Bill H wrote:

Quoted text here. Click to load it

For future reference, when using an example domain name, please use
example.(com|net|org). Mark Segal, who owns mycorp.net might not be
happy with you posting valid e-mail addresses at his domain to Usenet,
and potentially attracting spammers to it.

Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 13 days, 16:31.]

                        USD/EUR Exchange Rate Graph

Re: Switch

Thanks Toby.

It neverceases to amaze the breadth of information all of us are unaware of.  


Quoted text here. Click to load it

Site Timeline