Welcome Guest, Not a member yet? Register   Sign In
No idea how to refactor this into Code Igniter
#5

[eluser]chobo[/eluser]
I found a pretty good way to add external classes. I add a constant in the index.php that builds off of the other paths already defined. Later I will try to centralize all my paths into one file, and maybe even the base_url, so everything is in one place.

Now onto the contact form Smile I actually got it to work, but it's still far from done in my view.

Q1: I use to use mysqli_escape_string() on the POST variables, but this doesn't work unless I require it. First, is it even worth doing, or is escaping just for inserting and interacting with databases?

Second, if it is worth doing, how can I get access to the $con (connection variable) for the mysqli connection, so I can pass it to the mysqli_escape_string()? I have tried looking for a simple escape function, but they are a little overkill like xss_clean, or they require objects like escape_str().

Q2: I'm trying to improve my coding, so could someone give me a few pointers on how I could refactor the following code? Thanks!

Code:
<?php

require_once 'template_engine.php';

class Contact extends TemplateEngine {


public function __construct()
{
  parent::__construct();
}

/* Displays default page */
public function index()
{  
  $this->load->helper(array('form', 'url'));
  $this->load->library('validation');
  
  //set rules
  $rules['name'] = "required|min_length[2]|max_length[35]";
  $rules['email'] = "required|valid_email";
  $rules['message'] = "required|min_length[10]|max_length[1000]";
  $this->validation->set_rules($rules);

  // if it fails set the fields
  $fields['name'] = 'Name';
  $fields['email'] = 'Email Address';
  $fields['message'] = 'Message';
  $this->validation->set_fields($fields);
  
  parent::set_section('header', $this->load->view('layouts/default_header', $this->header, true));
  parent::set_section('left', $this->load->view('left_ad_view', '', true));
  parent::set_section('footer', $this->load->view('layouts/default_footer', '', true));
  
  // run validation and see if it passes
  if ($this->validation->run() == FALSE)
  {      
   parent::set_section('content', $this->load->view('contact_view', '', true));
  }
  else
  {
   $from_name = ($_POST['name']);
   $from_address = ($_POST['email']);
   $subject = ($_POST['subject']);
   $body = ($_POST['message']);
  
   require_once EXT_LIB . 'phpmailer/class.phpmailer.php';
   $mail = new PHPMailer();
   $mail->IsSMTP(); // telling the class to use SMTP
   $mail->Host = "smtp.isp.net"; // SMTP server  
   $mail->FromName = $from_name; // Sets the name of the emailer
   $mail->From = $from_address;  // Sets the email address of the sender
   $mail->AddAddress("[email protected]");  // Sets the To Address
   $mail->Subject = $subject; // Sets the subject heading
   $mail->Body = $body;
   $mail->WordWrap = 50;  
  
   //init $message var?
   if(!$mail->Send())
   {
    $message = "There was a problem sending your email, please try again later.";
    $message .= "Mailer Error: " . $mail->ErrorInfo;
   }
   else
   {
    $message = "Your message has been sent successfully!";
   }
  
   parent::set_section('content', $this->load->view('message_sent_view', '', true));
  }

  // build page
  parent::build_template();
}
}
?>


Messages In This Thread
No idea how to refactor this into Code Igniter - by El Forum - 07-21-2007, 10:30 PM
No idea how to refactor this into Code Igniter - by El Forum - 07-22-2007, 12:21 AM
No idea how to refactor this into Code Igniter - by El Forum - 07-22-2007, 12:43 AM
No idea how to refactor this into Code Igniter - by El Forum - 07-22-2007, 11:19 AM
No idea how to refactor this into Code Igniter - by El Forum - 07-22-2007, 01:20 PM
No idea how to refactor this into Code Igniter - by El Forum - 07-22-2007, 05:30 PM
No idea how to refactor this into Code Igniter - by El Forum - 07-22-2007, 05:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB