Проверка формы в codeigniter


я пытаюсь проверить форму в codeigniter и не смог этого сделать. Я хотел бы проверить переменные, поступающие из input формы mailcontacto.php который находится в view и в этой форме показывает мне ошибки проверки. Форма была отправлена хорошо, пока я не попытаюсь разместить проверку. Ниже коды.

контроллер

<?php

defined('BASEPATH') OR exit('No direct script access allowed');
class Emails extends CI_Controller {
function index(){
$datos['contenido'] = 'emails';
$this->load->view('contacto/mailcontacto', $datos); 
}
function enviar() {
//Descargar la libreria
$this->load->library('email');
$this->load->library('form_validation');


        $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 VIAJANDOFACIL.COM</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)
{

//Acción a tomar si existe un error el en la validación
}
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
   }
  }
 }
?>

вид

<form action="emails/enviar" method="post">
<table style="width:80%; margin-left:12%">
<tbody>
<tr>
<td><table style="width:70%; margin-left:16%; margin-right:16%;">
<tbody>
<tr>
<td><label><a >Nombre:</a></label></td>
</tr>
<tr>
<td ><input type="text"  id="nombre" name="nombre"></td>

</tr>
<tr>
<td><label><a>Telefono:</a></label></td>

</tr>
<tr>
<td><input type="text"  id="telefono"   name="telefono" ></td>

</tr>
<tr>
<td style="height:30px"><label><a>Email:</a></label></td>

</tr>


<tr>
<td><input type="text"  id="email" name="email" ></td>

</tr>
</tbody>
</table>
  </td>
  <td><table style="width:100%">
    <tbody>
      <tr>
         <td style="height:30px"><label><a>Asunto:</a></label></td>
      </tr>
      <tr>
        <td><input type="text"  id="asunto"  name="asunto"></td>
      </tr>
      <tr>
        <td><label><a>Mensaje:</a></label></td>
      </tr>
      <tr>
        <td><textarea rows="04" id="mensaje" name="mensaje"></textarea></td>
      </tr>
    </tbody>
  </table></td>
</tr>
</tbody>
 </table>
<table>
<tbody>
<tr>
  <td><button type="submit" value="enviar"  >Enviar</button></td><td></td>
</tr>
</tbody>

</table>
</form>
Author: Ing Alejandro Montes, 2016-05-04

1 answers

Послушайте, предполагая, что у вас установлен .htacces, чтобы удалить index.php

У нас есть вид

<form action="emails/enviar" method="post">
  <table style="width:80%; margin-left:12%">
    <tbody>
      <tr>
        <td>
          <table style="width:70%; margin-left:16%; margin-right:16%;">
            <tbody>
              <tr>
                <td><label><a >Nombre:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('nambre');?>
                </td>
                <td><input type="text" id="nombre" name="nombre" value="<?= set_value('nombre');?>"></td>
              </tr>
              <tr>
                <td><label><a>Telefono:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('telefono');?>
                </td>
                <td><input type="tel" id="telefono" name="telefono" value="<?= set_value('telefono');?>"></td>
              </tr>
              <tr>
                <td style="height:30px"><label><a>Email:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('email');?>
                </td>
                <td><input type="email" id="email" name="email" value="<?= set_value('email');?>"></td>
              </tr>
            </tbody>
          </table>
        </td>
        <td>
          <table style="width:100%">
            <tbody>
              <tr>
                <td style="height:30px"><label><a>Asunto:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('asunto');?>
                </td>
                <td><input type="text" id="asunto" name="asunto" value="<?= set_value('asunto');?>"></td>
              </tr>
              <tr>
                <td><label><a>Mensaje:</a></label></td>
              </tr>
              <tr>
                <td>
                  <?= form_error('mensaje');?>
                </td>
                <td><textarea rows="04" id="mensaje" name="mensaje"><?= set_value('mensaje');?></textarea></td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
    </tbody>
 </table>
<table>
<tbody>
<tr>
  <td><button type="submit" value="enviar"  >Enviar</button></td><td></td>
</tr>
</tbody>

</table>
</form>

Controller

class Emails extends CI_Controller{
   public function __construct(){
    parent::__construct();

    $this->load->library(array('email', 'form_validation'));
  }

  function index(){
    $datos['contenido'] = 'emails';
    $this->load->view('contacto/mailcontacto', $datos); 
  }

  function enviar() {
    $this->form_validation->set_rules('nombre', 'Nombre', 'trim|required|min_length[3]|xss_clean');
    $this->form_validation->set_rules('telefono', 'Telefono', 'trim|required|exact_length[10]|xss_clean');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
    $this->form_validation->set_rules('asunto', 'Asunto', 'trim|required|min_length[5]|xss_clean');
    $this->form_validation->set_rules('mensaje', 'Mensaje', 'trim|required|min_length[5]|xss_clean');

  if( $this->form_validation->run() === FALSE){
    $datos['contenido'] = 'emails';
    $this->load->view('contacto/mailcontacto', $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
  }
}

Я надеюсь, что это послужит вам, если у вас есть какие-либо ошибки, отправьте мне ошибку, которая показывает вам

 2
Author: elddenmedio, 2016-06-03 23:02:46