Add multiple file uploads to an existing PHP form -
i'm frontend developer trying struggle way through php form freelance client. quite extensive job application form, client has requested include 2 file uploads: image (jpg, png) , cv (pdf, doc). these files attach email containing form answers. uploading files folder on server permanently not required.
as woefully inadequate @ things php, resorted site called formbakery generate said form me, great - except doesn't include file upload capabilities.
i have tried couple of different resources php file upload , email attachment, including this tutorial , this answer, issue don't know how incorporate code have. hoping me out combining various different elements. if multiple file upload ask for, single 1 cv massive help.
php sending form
<?php // value array function array_get(array $array, $key, $default = null) { return (array_key_exists($key, $array)) ? $array[$key] : $default; } // string value array function array_get_string(array $array, $key, $default = '', $trim = true) { $val = array_get($array, $key); if (is_string($val)) { return ($trim) ? trim($val) : $val; } return $default; } // takes form array , validates function validateform(array $form) { // array of error messages $errors = array(); // each field in form foreach ($form $field) { // if field doesnt require validation, skip if (!isset($field['validation'])) continue; // each validation type foreach ($field['validation'] $validate) { // if field empty, there nothing check if (trim($field['value']) === '') break; switch($validate) { case 'email': if(!filter_var($field['value'], filter_validate_email)) { $errors[] = '"' . $field['value'] . '" invalid email address.'; } break; case 'number': if(!preg_match('/^[0-9 ]+$/', $field['value'])) { $errors[] = '"' . $field['value'] . '" invalid number.'; } break; case 'alpha': if(!preg_match('/^[a-za-z ]+$/', $field['value'])) { $errors[] = '"' . $field['value'] . '" contains invalid characters. field accepts letters , spaces.'; } break; } } // if field required , empty if (isset($field['required']) && trim($field['value']) === '') { // overwrite errors required error message $errors[] = '"' . $field['name'] . '" required field.'; } } // return array of errors return $errors; } // define form $form = array( 'position-applied-for' => array( 'name' => 'position applied for', ), // continue array items ); // populate form values post foreach ($form $field_name => &$field_data) { $value_type = gettype($_post[$field_name]); if ($value_type == 'string') { // string value post data $field_data['value'] = array_get_string($_post, $field_name); } elseif ($value_type == 'array') { // these multiple checkbox values // need concat them string $concat = ''; foreach (array_get($_post, $field_name) $item) { $concat .= $item . '; '; } $field_data['value'] = $concat; } } // unset referenced variable avoid quirks unset($field_data); $ajax = array_get_string($_post, 'request_method') === 'ajax'; $errors = validateform($form); $success = count($errors) ? false : true; // send email if ($success) { // full name: $fullname \nemail address: $emailaddress $formcontent = ''; $emailtemplate = "insert email template html"; foreach ($form $field_name => $field_data) { $formcontent .= $field_data['name'] . ': ' . $field_data['value'] . " \n"; } $formcontent = wordwrap($formcontent, 70, "\n", true); $recipient = 'email@hotmail.com'; $subject = 'creed coffee application form'; $mailheader = "from: applications@creed.coffee \r\ncontent-type: text/html;"; mail($recipient, $subject, $emailtemplate, $mailheader); } ?> <?php if ($ajax): ?> <?php if ($success): ?> pass <?php else: ?> fail <?php endif; ?> <?php else: ?> redirect <?php endif; ?>
this accompanied jquery validation etc. understand big ask , don't expect take time required write out me, appreciate if point me in right direction or give me enough pointers struggle onwards this!
Comments
Post a Comment