Welcome Guest, Not a member yet? Register   Sign In
Form to email: passing success / fail message back to view - howto?
#1

[eluser]stef25[/eluser]
im making a simple microsite where people fill in a form to recommend company x to their friends. i have one view (index.php) and one controller (mailer.php). the form submits to the page it's on. the view after the submission should be exactly the same, apart from one little <p>success or fail message</p>.

Im a bit stuck as to how i can get this message back to the view. I read alot about the use of this->load->vars, but how do i get this to appear in my view? $feedback doesnt exist unless the message sending is success or fail, so when i first arrive at index.php i get an error " Undefined variable: feedback", referring to my echo $feedback.

anyone?


Code:
//send mail + success / fail message            
            if (!$this->email->send()) {
                $feedback = array(
                'message' => 'Sorry, message could not be sent'
                );    
                $this->load->vars($feedback);
            }            
            else {
                $feedback = array(
                'message' => 'Message sent!'
                );    
                $this->load->vars($feedback);
            }
#2

[eluser]rweston[/eluser]
Code:
if(!isset($feedback)) $feedback = '';

What you were trying to do? or :

Code:
//send mail + success / fail message            
$feedback = array('message' => '');
           if (!$this->email->send()) {
                $feedback = array(
                'message' => 'Sorry, message could not be sent'
                );
            }            
            else {
                $feedback = array(
                'message' => 'Message sent!'
                );    
            }
            $this->load->vars($feedback);
#3

[eluser]stef25[/eluser]
i now have code below in my view, but it always stays at 'hi!'. with my code in the OP, should the success / fail msg get carried back to the view?

Code:
if(!isset($feedback)) $feedback = 'hi!';
echo $feedback;
#4

[eluser]rweston[/eluser]
stef, from the other thread, i swapped some stuff around. e.g. the loading of the view after the check for the posted data and added a 'definition' for $feedback.

I'm fairly 'new' to working with CI, but i've stuck with passing data to the view using the 2nd paramater of the view method ( $this -> load -> view ('viewname', $data) ), but i'll have to check out the load -> vars method and get familar with it.

Code:
class Mailer extends Controller {
    function index() {      
        
        $feedback = array('message' => '');
        //check to see if form is being submitted
        if($this->input->post('sendCheck'))
        {
            //load email library
            $this->load->library('email');
            
            //text alternative
            $textMail = "this is a text replacement";
            
            //grab values
            $fromName = $this->input->post('fromName');
            $fromEmail = $this->input->post('fromEmail');
            $toName = $this->input->post('toName');
            $toEmail = $this->input->post('toEmail');                    
            
            //insert values
            $this->email->from($fromEmail, $fromName);
            $this->email->to($toEmail, $toName);
            $this->email->subject($fromName.' would like to recommend Company X to you');
            $this->email->message('<p>All your base are belong to us</p>');
            $this->email->set_alt_message($textMail);
            echo $this->email->print_debugger();
                        
            //send mail + success / fail message            
            if (!$this->email->send()) {
                $feedback = array(
                'message' => 'Sorry, message could not be sent'
                );    
                $this->load->vars($feedback);
            }            
            else {
                $feedback = array(
                'message' => 'Message sent!'
                );    
                $this->load->vars($feedback);
            }                              
        }
                
        //load the default view, the main index.php html design
        $this->load->view('index');                    
    }
}
#5

[eluser]Michael Wales[/eluser]
Not sure why you are using load vars in this instance... here's how I would accomplish the same:

Code:
$this->data->feedback = 'hi!';
if (!$this->email->send()) {
  $this->data->feedback = 'Sorry, message could not be sent.';
} else {
  $this->data->feedback = 'Message sent!';
}
$this->load->view('myview', $this->data);

View:
Code:
echo $feedback;




Theme © iAndrew 2016 - Forum software by © MyBB