My problem is this: I am trying to make a form but I can not address well when I try to correct the validation after answering that the data is not valid through the conditional if. The idea is that I have to fill in the form's data, then it takes me to the view again to complete and correct the errors but when I submit it again I get an error as if I were to repeat the function or something and it does not send. Then the code and the screens of the procedure.
Controller
class Emails extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library(array('email', 'form_validation'));
}
$this->load->library(array('email', 'form_validation'));} function index(){
$datos['contenido'] = 'emails';
$this->load->view('contacto/mailcontacto', $datos); } function enviar() {
$nombre = $this->input->post('nombre');
$telefono = $this->input->post('telefono');
$email = $this->input->post('email');
$asunto = $this->input->post('asunto');
$mensaje = $this->input->post('mensaje');
$body_msg = '<html><body><br />'. '<h2><font face="times new roman" color="#da0021"><span><font face="times new roman" color="#00769f"> CONTACTO </h2></font>'.'<table rules="all" style="border-width: 1px; border-style: dashed; border- color: #50a9d5; " cellpadding="10">' ."<tr><td><strong>Nombre</strong> </td><td>" . $nombre . "</td></tr>"."<tr><td><strong>Telefono:</strong> </td><td>" . $telefono . "</td></tr>"."<tr style=style='background: #eee;'><td><strong>Enviado desde:</strong> </td><td>" . $email. "</td></tr>"."<tr><td><strong>Asunto:</strong> </td><td>" . $asunto . "</td></tr>"."<tr><td><strong>Mensaje:</strong> </td><td>" . $mensaje . "</td></tr>"."<br />"; //Validaciones //Nombre del campo, titulo, restricciones
$this->form_validation->set_rules('nombre', 'Nombre','required|min_length[3]|alpha|trim');
$this->form_validation->set_rules('email', 'Email', 'required|min_length[3]|valid_email|trim');
$this->form_validation->set_rules('telefono', 'Telefono', 'required|numeric');
$this->form_validation->set_rules('asunto', 'Asunto', 'required|min_length[3]|alpha|trim');
$this->form_validation->set_rules('mensaje', 'Mensaje', 'required|min_length[3]|alpha|trim'); if( $this->form_validation->run() === FALSE){
$datos['contenido'] = 'emails';
$this->load->view('contacto', $datos); } else{ //Acción a tomas si no existe ningun error // Datos para enviar el correo
$this->email->from('[email protected]', 'Contacto');
$this->email->to('[email protected]');
$this->email->subject($asunto);
$this->email->message($body_msg );
$this->email->attach('img/logo.png');
$this->email->send(); redirect('contacto'); // Se direcciona } }} ?>'
application / config / config.php
$config['base_url'] = 'http://www.dominio.com/';
application / config / autoload.php
$autoload['libraries'] = array('form_validation', 'session');
$autoload['helper'] = array('url', 'form')
application / config / routes.php
$route['default_controller'] = "inicio"; // Cambiado el default por inicio
$route['404_override'] = '';
$route['enviar'] = 'emails/enviar';
application / views / mailcontact.php
<form action="emails/enviar" method="post">
<table style="margin:-01px auto; width:100%;background:#ffffff"> <tr>
<td style="padding:10px 10px 10px 10px"><label><a style="color:#E41519"> </a><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">Envianos un mensaje o requerimiento y un asesor lo contactara a la brevedad posible:
</a></label></td>
</tr></tbody></table>
<table style="width:100%; background:#ffffff;">
<tbody>
<tr>
<td style=" background:#05C421"><?php if(isset($mensaje)) echo $mensaje; ?></h2>
<?=validation_errors();?><!--mostrar los errores de validación--></td></tr>
</tbody>
</table>
<table style="width:100%; height:auto; background:#ffffff; margin:05px auto;">
<tbody>
<tr>
<td><table style="margin:-50px auto; width:100%; width:60%; background:#ffffff">
<tbody>
<tr>
<td style="padding:20px 10px 10px 10px"><table width="200" border="1">
<tbody>
<tr>
<td><label><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">Nombre:
</a></label></td>
<td> </td>
</tr>
</tbody>
</table>
</td>
<td> </td>
<td style="padding:20px 10px 10px 10px"><table width="200" border="1">
<tbody>
<tr>
<td><label><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">Teléfono:
</a></label></td>
<td> </td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td><input type="text" placeholder=" id="nombre" name="nombre" class="form-control logpadding margtop10 mayuscula" required title="Se necesita un nombre minimo dos letras" autofocus="autofocus" value="<?php echo set_value('nombre') ?>" /></td>
<td> </td>
<td><input type="text" placeholder="0261000000" id="telefono" name="telefono" class="form-control logpadding margtop10" required pattern="^\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d$" title="Se necesita un minimo 10 numeros incluyendo codigo de area sin espacios" autofocus="autofocus" value="<?php echo set_value('telefono') ?>"/></td>
</tr>
<tr>
<tr><td ><a style="color:#F0080C"><?php echo form_error('nombre') ?></a> </td><td></td><td><a style="color:#F0080C"><?php echo form_error('telefono') ?></a> </td></tr>
<td><a style="color:#24BCFC"> Solo Formato Letras permitidas</a></td><br/><br/>
<td> </td>
<td><a style="color:#24BCFC">Solo Formato Números permitidos</a></td><br/><br/>
</tr>
<tr>
<td style="padding:10px 10px 10px 10px"><table width="200" border="1">
<tbody>
<tr>
<td><br/>
<label><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">Pais:
</a></label></td>
<td> </td>
</tr>
</tbody>
</table>
</td
><td> </td>
<td style="padding:10px 10px 10px 10px"><table width="200" border="1">
<tbody>
<tr>
<td><br/>
<label><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">Ciudad:
</a></label></td>
<td> </td>
</tr>
</tbody>
</table></td>
</tr>
<tr><tr><td><a style="color:#F0080C"><?php echo form_error('pais') ?></a></td><td></td><td><a style="color:#F0080C"><?php echo form_error('ciudad') ?></a></td></tr>
<td><input type="text" id="pais" name="pais" class="form-control logpadding margtop10" value="<?php echo set_value('pais') ?>"/></td>
<td> </td>
<td><input type="text" id="ciudad" name="ciudad" class="form-control logpadding margtop10" value="<?php echo set_value('ciudad') ?>"/></td>
</tr>
<tr>
<td style="padding:10px 10px 10px 10px"><table width="200" border="1">
<tbody>
<tr>
<td><br/><label><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">Email:
</a></label></td>
<td> </td>
</tr>
</tbody>
</table></td>
<td> </td>
<td style="padding:10px 10px 10px 10px"><table width="200" border="1">
<tbody>
<tr>
<td><br/>
<label><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">Asunto:
</a></label></td>
<td> </td>
</tr>
</tbody>
</table></td>
</tr>
<tr><tr><td><a style="color:#F0080C"><?php echo form_error('email') ?></a></td><td></td><td><a style="color:#F0080C"><?php echo form_error('asunto') ?></a></td></tr>
<td><input type="text" placeholder="" id="email" name="email" class="form-control logpadding margtop10" required pattern="[a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*@[a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{1,5}" title="Se necesita un formato de email [email protected]" autofocus="autofocus" value="<?php echo set_value('email') ?>" /></td>
<td> </td>
<td><input type="text" placeholder="" id="asunto" name="asunto" class="form-control logpadding margtop10 mayuscula" required title="Se necesita un nombre minimo dos letras" autofocus="autofocus" value="<?php echo set_value('asunto') ?>" /></td>
</tr>
<tr>
<td> <a style="color:#24BCFC"> Solo Formato Email permitido</a>
<br/><br/></td>
<td> </td>
<td> <a style="color:#E41519"> </a>
<br/><br/></td>
</tr>
</table></td></tr><tr><td><table style="margin:50px auto; width:100%; width:60%; background:#ffffff"> <tr>
<td style="padding:10px 10px 10px 10px"> <label><a style="font-family: 'Roadgeek 2005 Series 1B', sans-serif; font-size:16px; color:#000000; ">¿En que podemos ayudarte?:
</a></label></td>
</tr><tr><td><textarea class="mayuscula" rows="04" id="mensaje" name="mensaje" style="margin-top:08px; width:80%" required pattern="[a-zA-Z]+{2,50}" title="Se necesita un mensaje o requerimiento minimo dos letras" autofocus="autofocus" value="<?php echo set_value('mensaje, $foo') ?>"/></textarea></td></tr><tr><td><a style="color:#F0080C"><?php echo form_error('mensaje') ?></a></td></tr></table></td></tr></tbody></table>
<table style="width:100%; background:#ffffff;">
<tbody>
<tr>
<td><button style=" margin:0 auto; type="submit" value="enviar" class="btn btn-primary">Enviar</button></td>
</tr>
</tbody>
</table>
</form>
Screen 2 Returning the errors and correcting the form
Here the error Screen 3 ERROR