2011-11-22 19 views
0

聲明:我是網絡開發新手。Codeigniter中的電子郵件類使用表格

場景:我創建了一個聯繫表單,並且試圖將輸入傳遞到CodeIgniter中使用的電子郵件類功能。提交表單時收到未定義的變量錯誤。電子郵件庫自動加載以供參考。現在已經很晚了,我可能會忽略一些簡單的事情,但是這並不會傷害到發佈。非常感謝您的幫助!

控制器:

public function index() 
{ 
    //form validation 
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('subject', 'Subject', 'required'); 
    $this->form_validation->set_rules('message', 'Message', 'required'); 

    $this->data['email'] = array(
      'name' => 'email', 
      'id' => 'email', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('email'), 
    ); 
    $this->data['subject'] = array(
      'name' => 'subject', 
      'id' => 'subject', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('subject'), 
    ); 
    $this->data['message'] = array(
      'name' => 'message', 
      'id' => 'message', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('message'), 
    ); 

    if ($this->form_validation->run() == true) 
    { 
     $this->email->from($email); 
     $this->email->to('[email protected]'); 

     $this->email->subject($subject); 
     $this->email->message($message);  

     $this->email->send(); 

     redirect('contact/success'); 
    } 

    else 
    { 

     $this->data['error_message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('error_message'); 

     $title['title'] = 'Contact'; 

     $this->load->view('public/head_view', $title); 
     $this->load->view('public/header_view'); 
     $this->load->view('public/contact_view', $this->data); 
     $this->load->view('public/footer_view'); 
    } 

查看:

<div id="infoMessage"><?php echo $error_message;?></div> 

      <?php $attritubes = array('class' => 'nice'); ?> 
      <?php echo form_open('contact'); ?> 
       <p>Email Address:<br /> 
        <?php echo form_input($email); ?> 
       </p> 
       <p>Subject:<br /> 
        <?php echo form_input($subject); ?> 
       </p> 
       <p>Message:<br /> 
        <?php echo form_textarea($message); ?> 
       </p> 
       <p><?php echo form_submit('submit', 'Submit'); ?></p> 
      <?php echo form_close(); ?> 
+0

什麼是問題,什麼不起作用? –

+0

最新錯誤? – AlphaMale

+0

你什麼時候試圖訪問變量?你什麼時候打電話給視線?它似乎不在你的控制器中? – Chris

回答

1

嗯,你幾乎在此處定義的任何變量:

if ($this->form_validation->run() == true) 
    { 
     $this->email->from($email); 
     $this->email->to('[email protected]'); 

     $this->email->subject($subject); 
     $this->email->message($message);  

     $this->email->send(); 

     redirect('contact/success'); 
    } 

哪裏$email$subject$message從何而來? 你可能想使用類似(但我建議你做的更好:))

$email = $this->input->post('email'); 
$subject = $this->input->post('subject'); 
$message = $this->input->post('message'); 

此外,還要確保你調用它之前已經加載郵件庫,並且您加載視圖在else{}部分(因爲你省略了,我不知道)

+1

感謝您向我展示我是多麼愚蠢,哈哈。我結束了以下工作: '$ this-> email-> from($ this-> input-> post('email'));' '$ this-> email-> subject($ this-> ($ subject)';' '$ this-> email-> message($ this-> input-> post('message'));' 並修復了我的email.php中的其他錯誤配置文件,使其工作。非常感謝! – imlouisrussell