[support] loops?

MichelleX michelle at steelveils.com
Sat Dec 2 00:59:20 UTC 2006


I apologize if this isn't the right place to post this.. but I was 
hoping someone could help me out with a foreach loop.

I'm using the code from here http://drupal.org/node/68265 for a custom 
form in a page.
I've modified it to display select menus - and they work just great 
except I can't get it to send anything other than a number. I've been 
all through the api stuff and have tried various methods for writing a 
foreach loop for the select menus, but everything seems to conflict with 
common.inc.
Here's my modified code (hopefully this will display correctly):

<?php

$form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#default_value' => $object['name'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
);

$form['eMail'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail Address'),
    '#default_value' => $object['eMail'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
);

//account option
 $account = array(
'--'.t('Select').'--',
t('Model'),
t('Photographer'),
t('Crew'),
);

$form['assign'][$i] = array(
    '#type' => 'select',
    '#title' => t('Account purpose'),
    '#default_value' => $object['assign'][$i],
    '#options' => $account,
    '#required' => TRUE,
    '#description' => t('Choose which account your\'re applying for.'),
);

   
$form['location'] = array(
    '#type' => 'textfield',
    '#title' => t('Location'),
    '#default_value' => $object['location'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
'#description' => t('Enter your city, state and country'),
);

$form['age'] = array(
    '#type' => 'textfield',
    '#title' => t('Age'),
    '#default_value' => $object['age'],
    '#size' => 15,
    '#maxlength' => 128,
    '#required' => TRUE,
'#description' => t('Enter your age'),
);

//experience options
 $experience = array(
'--'.t('Select').'--',
t('Amateur/ Hobbyist'),
t('Experienced Hobbyist'),
t('Semi Professional'),
t('Professional'),
);

$form['exlevel'] = array(
    '#type' => 'select',
    '#title' => t('Experience Level'),
    '#default_value' => $object['exlevel'],
    '#options' => $experience,
'#required' => TRUE,
'#description' => t('Enter your experience level.'),
);

//travel options
 $travel = array(
'--'.t('Select').'--',
t('Yes'),
t('w/ expenses paid'),
t('w/o expenses paid'),
t('Only in my state'),
t('Only in my city'),
t('Depends on assignment'),
t('No'),
);

$form['willtravel'] = array(
    '#type' => 'select',
    '#title' => t('Will travel'),
    '#default_value' => $object['willtravel'],
    '#options' => $travel,
'#required' => TRUE,
'#description' => t('Are you willing to travel?'),
);

$form['nudity'] = array(
    '#type' => 'textfield',
    '#title' => t('Nudity Level'),
    '#default_value' => $object['nudity'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
'#description' => t('Enter your level of experience or willingness for 
nude assignments.'),
);
$form['compensation'] = array(
    '#type' => 'textfield',
    '#title' => t('Compensation'),
    '#default_value' => $object['compensation'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => TRUE,
'#description' => t('Example: DOA (Depends on Assignment), TFP, TFCD, 
Paid only, etc.'),
);
$form['website'] = array(
    '#type' => 'textfield',
    '#title' => t('Web Site'),
    '#default_value' => $object['website'],
    '#size' => 30,
    '#maxlength' => 128,
    '#required' => FALSE,
'#description' => t('If you have a personal web site, enter it here.'),
);
$form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('additional information'),
    '#default_value' => $object['message'],
    '#size' => 30,
    '#maxlength' => 128,
    '#rows'    => 7,
    '#required' => FALSE,
);
  
$form['file1'] = array(
    '#type' => 'file',
      '#title' => t('Four images are required for review.'),
);  
$form['file2'] = array(
    '#type' => 'file',
);
$form['file3'] = array(
    '#type' => 'file',
);  
$form['file4'] = array(
    '#type' => 'file',
);
  
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('send email'),
);

$form['#attributes']['enctype'] = 'multipart/form-data';

$output = drupal_get_form('contactform', $form);
return $output;

// validation function for the contact form
function contactform_validate($form_id, $form_values) {
    // first we validate if there is a email injection
    $finds = array("/bcc:/i",
            "/Content-Type:/i",
            "/Mime-Type:/i",
            "/MIME-Version:/i",
            "/multipart\/mixed/i",
            "/boundary=/i",
            "/subject:/i",
            "/cc:/i",
            "/to:/i");
    foreach($form_values as $value)
          foreach($finds as $find)
                if(preg_match($find,$value))
                    form_set_error('', '<h2 class="red center">Stop 
spamming</h2>');

    // then we validate the email-adress    
    if (!valid_email_address($form_values['eMail']) && 
!empty($form_values['eMail']))
        form_set_error('', t('Please check the spelling of your 
email-adress.'));
}


// submit function for the contact form
function contactform_submit($form_id, $form_values) {
    
    $from         = $form_values['name'].' <'.$form_values['eMail'].'>';
    $recipient    = 'MX <michelle at steelveils.com>';
    $subject    = 'Application';
    $account      = $form_values['assign'];
    $location      = $form_values['location'];
    $age      = $form_values['age'];
    $experience      = $form_values['exlevel'];
    $travel      = $form_values['willtravel'];
    $nudity      = $form_values['nudity'];
    $compensation      = $form_values['compensation'];
    $website      = $form_values['website'];
    $body         = wordwrap($form_values['message']);
    $reply        = 'Thank you for your application. Your application 
for an account is currently pending approval. Once it has been granted, 
you will receive further instructions in eMail.';
    $goto        = 'app-rec';

    if (!flood_is_allowed('contact', 
variable_get('contact_hourly_threshold', 8))) {
        $output = t("You cannot send more than %number messages per 
hour. Please try again later.", array('%number' => 
variable_get('contact_hourly_threshold', 3)));
        drupal_set_message('<h3 class="red center">'.$output.'</h3>');
        drupal_goto($goto);
    }else{  
        $attachment     = FALSE;
        $trenner     = md5(uniqid(time()));
        $headers    .= "MIME-Version: 1.0\n";
        $headers     .= "From: $from\nReply-to: $from\nReturn-path: 
$from\nErrors-to: $from\nX-Mailer: Drupal\n";          
        $headers     .= "Content-Type: 
multipart/mixed;\n\tboundary=$trenner\n";
        $message     .= "\n--$trenner\n";
        $message     .= "Content-Type: text/plain; 
charset=UTF-8;"."\n\n"; // sets the mime type
        $message     .= "Account type: " .$account. "\n";
        $message     .= "Location: " .$location."\n";
        $message     .= "Age: " .$age."\n";
        $message     .= "Experience Level: " .$experience."\n";
        $message     .= "Will Travel: " .$travel."\n";
        $message     .= "Nudity Level: " .$nudity."\n";
        $message     .= "Compensation: " .$compensation."\n";
        $message     .= "Web Site: " .$website."\n";
        $message     .= $body. "\n";
        $message     .= "\n\n";
       
            
             for($i=1;$i<=4;$i++){
            $file = file_check_upload('file'.$i);
            if($file->filename){
                $file->filepath = str_replace("\\","\\\\",$file->filepath);
                $message    .= "--$trenner"."\n";
                $message    .= 
"Content-Type:$file->filemime;\n\tname=$file->filename\n";
                $message    .= "Content-Transfer-Encoding: base64\n";
                $message    .= "Content-Disposition: 
attachment;\n\tfilename=$file->filename\n\n";
                $filedata    = fread(fopen($file->filepath, "rb"), 
$file->filesize);
                $message    .= chunk_split(base64_encode($filedata));
                $message    .= "\n\n";
                $attachment    = TRUE;
            }
        }
        $message .= "--$trenner--";

        // send Mail
        if($attachment) // use the php mail function if we have attachments
            mail($recipient, $subject, $message, $headers);
        else
            user_mail($recipient, $subject, $account, $location, $age, 
$experience, $travel, $nudity, $compensation, $website, $body, "From: 
$from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: 
$from");

        // Reply
        user_mail($from, $subject, wordwrap($reply), "From: 
$recipient\nReply-to: $recipient\nX-Mailer: Drupal\nReturn-path: 
$recipient\nErrors-to: $recipient");
  
        // Log the operation:
        flood_register_event('contact');
        watchdog('mail', t('%name-from use contact form', 
array('%name-from' => theme('placeholder', $form_values['name'] ." 
<$from>"),)));

        drupal_set_message('Your message has been sent to us');
        drupal_goto($goto);
        }
}

?>

Thanks!
Michelle



-- 
Steel Veils
Photography. Digital Design. Custom Art.
________________________________________________________

www.SteelVeils.com | michelle at SteelVeils.com | 314.283.1081


More information about the support mailing list