rendered paste body<?php
$choices = array('Seleccione Departamento', 'Atención al Cliente', 'Pedidos', 'Devoluciones', 'Reclamaciones', 'Programa de Afiliados', 'Cuentas de Empresa');
$choiceprior = array('Seleccione Prioridad', 'Baja', 'Media', 'Alta', 'Extrema');
if($_SERVER['REQUEST_METHOD'] == 'GET') {
showForm(array());
} else {
$errors = validateForm();
if(count($errors)) showForm($errors); // if errors show again
else print 'Form submitted succesfully!'; // no errors
}
function showForm($errors){
global $choices;
global $choiceprior;
// set defaults
$defaults = array();
$defaults['contact_nombre'] = isset($_POST['contact_nombre']) ? htmlentities($_POST['contact_nombre']) : '';
$defaults['contact_telefono'] = isset($_POST['contact_telefono']) ? htmlentities($_POST['contact_telefono']) : '';
$defaults['contact_email'] = isset($_POST['contact_email']) ? htmlentities($_POST['contact_email']) : '';
$defaults['contact_message'] = isset($_POST['contact_message']) ? htmlentities($_POST['contact_message']) : '';
foreach($choices as $key => $choice){
if(isset($_POST['departamento']) && ($_POST['departamento'] == $key)) $defaults['departamento'][$key] = 'selected="selected"';
else $defaults['departamento'][$choice] = '';
}
foreach($choiceprior as $key => $choice){
if(isset($_POST['prioridad']) && ($_POST['prioridad'] == $key)) $defaults['prioridad'][$key] = 'selected="selected"';
else $defaults['prioridad'][$choice] = '';
}
// print form
print "<form action='{$_SERVER['SCRIPT_NAME']}' method='post'>";
print "<table class='noborder' cellspacing='0'>";
print "<tr><td class='td-label-wide'><strong>Departamento</strong> <span class='highlight'>*</span></td><td class='td-field'>";
print "<select name='departamento' class='country'>";
foreach($choices as $key => $choice){
print "<option value='{$key}' {$defaults['departamento'][$key]}>{$choice}</option>";
}
print "</select>";
showError('departamento', $errors);
print "</td></tr>";
print "<tr><td><strong>Nombre y Apellidos</strong> <span class='highlight'>*</span></td><td>";
print "<input type='text' name='contact_nombre' class='field' value='{$defaults['contact_nombre']}'/>";
showError('contact_nombre', $errors);
print "</td></tr>";
print "<tr><td><strong>Teléfono de Contacto</strong> <span class='highlight'>*</span></td><td>";
print "<input type='text' name='contact_telefono' class='field' value='{$defaults['contact_telefono']}'/>";
showError('contact_telefono', $errors);
print "</td></tr>";
print "<tr><td><strong>E-mail</strong> <span class='highlight'>*</span></td><td>";
print "<input type='text' name='contact_email' class='field' value='{$defaults['contact_email']}'/>";
showError('contact_email', $errors);
print "</td></tr>";
print "<tr><td><strong>Motivo Consulta</strong> <span class='highlight'>*</span></td><td>";
print "<textarea name='contact_message' class='field' style='overflow:auto; height:100px;' rows='' cols=''></textarea>";
showError('contact_message', $errors);
print "</td></tr>";
print "<tr><td><strong>Prioridad</strong></td><td>";
print "<select name='prioridad' class='country'>";
foreach($choiceprior as $key => $choice){
print "<option value='{$key}' {$defaults['prioridad'][$key]}>{$choice}</option>";
}
print "</select>";
showError('prioridad', $errors);
print "</td></tr>";
print "</table><h4 id='options'>Opciones</h4><p class='hr-line'></p><table class='noborder options' cellspacing='0'><tr><td><input name='sendcopy' type='checkbox' value='true' /><strong>Enviar una cópia del mensaje a su mail.</strong></td></tr><tr><td><input name='ofertasynovedades' type='checkbox' value='true' checked='checked' /><strong>Deseo recibir información de ofertas y novedades</strong></td></tr><tr><td class='button' colspan='2'><input type='image' value='' src='' class='enviar' title='Enviar' /><input type='image' value='' src='' class='borar' title='Borar' /></td></tr></table></form>";
}
// display error
function showError($type, $errors){
if(isset($errors[$type])) print "<p class='highlight'>{$errors[$type]}</p>";
}
// validate data
function validateForm(){
global $choices;
// start validation and store errors
$error = array();
// validate drop-down
if(!(isset($_POST['departamento']) && (array_key_exists($_POST['departamento'], $choices)) && $_POST['departamento'] != 0)) $errors['departamento'] = 'Select a departamento';
// validate input
if(!(isset($_POST['contact_nombre']) && (strlen($_POST['contact_nombre']) > 5)))
$errors['contact_nombre'] = 'Insert a contact_nombre of at least 5 characters';
if(!(isset($_POST['contact_telefono']) && (strlen($_POST['contact_telefono']) > 5)))
$errors['contact_telefono'] = 'Insert a contact_telefono of at least 5 characters';
if(!(isset($_POST['contact_email']) && (strlen($_POST['contact_email']) > 5)))
$errors['contact_email'] = 'Insert a contact_email of at least 5 characters';
if(!(isset($_POST['contact_message']) && (strlen($_POST['contact_message']) > 5)))
$errors['contact_message'] = 'Insert a contact_message of at least 5 characters';
return $errors;
}
?>