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

[eluser]chobo[/eluser]
I'm trying to convert an existing site into CodeIgniter, and I'm making pretty good progress, but I'm kind of stuck on this file. To give you a brief intro the file just handles the contact form information by doing validation, producing appropriate error messages, and sending the email. Users would never actually see this file and it is loaded when the user hits the submit form.

Hold you breath the code is nasty Smile. I don't expect anyone to refactor it all, I just need some pointers on how to handle some things.

Issues:

1. I am using the phpMailer class and don't plan on changing. Which folder should I put this class in and how should I load it?

2. When the user clicks on the form, should I handle all the logic in a controller, including the validation? I have excluded the part with the mail credentials but it is called from the mail.php file.

Overall I just need to know where stuff should go, especially the part when a user clicks the form button, I'm not even sure how to handle that. Any help is appreciated, thanks.


Code:
<?php
function validate_input($from_name, $from_address, $subject, $body) {

// Validate User Name
if(strlen($from_name) > 50) {
  echo 'Your name is too long. Please keep you name under 50 characters';
  echo '<a href = "[removed]history.back()">  Go back  </a>';
      exit();
} else if (strlen($from_name) == 0) {
     echo 'Please fill in the Name field';
     echo '<a href = "[removed]history.back()">  Go back  </a>';
     exit();
}
    

// Validate User Address
if(strlen($from_address) > 50) {
      echo 'Your email address is too long. Please use an address under 50 characters';
      echo '<a href = "[removed]history.back()">  Go back  </a>';
      exit();
} else if (strlen($from_address) == 0) {
     echo 'Please fill in the Address field';
      echo '<a href = "[removed]history.back()">  Go back  </a>';
      exit();
}


// Validate Subject
if(strlen($subject) > 50) {
    echo 'Your email address is too long. Please use an address under 50 characters';
  echo '<a href = "[removed]history.back()">  Go back  </a>';
     exit();
} else if (strlen($subject) == 0) {
  echo 'Please select a subject';
      echo '<a href = "[removed]history.back()">  Go back  </a>';
      exit();
}
  

// Validate
if(strlen($body) > 1000) {
     echo 'Your message is too long. Please keep your message under a 1000 characters';
     echo '<a href = "[removed]history.back()">  Go back  </a>';
     exit();
} else if (strlen($body) < 10) {
      echo 'Your message is too short please revise it';
      echo '<a href = "[removed]history.back()">  Go back  </a>';
     exit();
}
}

require_once 'con-link.php';
require_once 'p-link.php'; // path to mail settings
$from_name = mysqli_escape_string($con, $_POST['name']);
$from_address =  mysqli_escape_string($con, $_POST['address']);
$subject =  mysqli_escape_string($con, $_POST['subject']);
$body =  mysqli_escape_string($con, $_POST['message']);

validate_input($from_name, $from_address, $subject, $body);

require("phpmailer/class.phpmailer.php");

// mail settings
require_once INCLUDE_PATH . 'mail.php';

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!";
}
?&gt;
&lt;?php
require_once 'link.php';
?&gt;
<div id="col_container">
  <div id="left_column">
   <h1 class="g_header">Affiliates</h1>
   <img src="images/affiliates.gif" height="600px" width="160px" />
  </div>
  <div id="right_column">
   <div>
    <h1 class="sent_message">&lt;?php echo $message; ?&gt;</h1>
   </div>
  </div>  
</div>
&lt;?php
require_once INCLUDE_PATH . 'footer.php';
?&gt;


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