codeigniter从控制器到视图传递emailfunction的值?

时间:2016-10-04 10:34:53

标签: php html codeigniter

我想将字符串从控制器传递给视图。所以我在Controller页面的If-Else子句中写了这样的$ data ['emailstr]和['emailfail']

                $this->load->library('email', array('mailtype'=>'html'));//default mailtype is 'text'
                $this->email->from('supanat_thana@hotmail.com', "Webmaster");
                $this->email->to($this->input->post('email'));
                $this->email->subject("Confirm your account");
                $message = "<p>Thank you for register!</p>";
                $message .= "<p><a href='".base_url()."register/activate_user'>Click Here</a> To Activate your Account </p>";
                $this->email->message($message);



                    if($this->email->send())
                    {
                        $data['emailstr'] = "email has been sent";
                    }
                    else
                    {
                        $data['emailfail'] = "could not send the email.";
                    }



                $this->load->view('regislogin/success', $data);

但是在viewpage中,我可以编写发送的成功字符串电子邮件但是在$ emailfail字符串上发生错误:未定义的变量:emailfail

<DOCTYPE! html>
<html>
<head>
    <title> Register </title>
    <meta charset="utf-8">
</head>
<body>
<?php
        echo "<h3>Thank you for your registration</h3>";
        echo $emailstr."<br/><br/>";
        if($emailfail != null) {echo $emailfail."<br/><br/>";} else{}
        echo anchor('home', 'กลับสู่หน้าหลัก');

?>
</body>
</html>

我该怎么办?

4 个答案:

答案 0 :(得分:0)

这是因为:

@Override
public void onResume() {
    super.onResume();
    if (mAdapter != null) {
        mAdapter.notifyDataSetChanged();
    }
}
  

$ data [&#39; emailstr&#39;]&amp; $数据[&#39; emailfail&#39;]

如果您的外部if条件不成功,则

未初始化。我认为你的情况会发生。

答案 1 :(得分:0)

使用isset()代替!=null。您无法对不存在的值进行比较,但isset()确定变量是否设置且不是NULL

if(isset($emailfail)) {echo $emailfail."<br/><br/>";} else{}

http://php.net/manual/en/function.isset.php

答案 2 :(得分:0)

而不是像下面那样发送字符串发送数组并检查

$key = md5(uniqid());
$this->load->library('email', array('mailtype'=>'html'));//default mailtype is 'text'
$this->email->from('supanat_thana@hotmail.com', "Webmaster");
$this->email->to($this->input->post('email'));
$this->email->subject("Confirm your account");
$message = "<p>Thank you for register!</p>";
$message .= "<p><a href='".base_url()."register/activate_user/$key'>Click Here</a> To Activate your Account </p>";

$this->email->message($message);
            if($this->register_model->add_temp_user($key))
            {
                if($this->email->send())
                {
                    $data['emailstatus'] =   "success"; 
                    $data['emailstr'] =   "email has been sent";
                }
                else
                {
                    $data['emailstatus'] =   "fail";
                    $data['emailfail'] = "could not send the email.";
                }
            }
            $this->load->view('regislogin/success', $data); 

在View中查看电子邮件状态,如下所示

<DOCTYPE! html>
<html>
<head>
<title> Register </title>
<meta charset="utf-8">
</head>
<body>
   <?php
   if($this->input->post("btn"))
   {
        echo "<h3>Thank you for your registration</h3>";
        echo $emailstr."<br/><br/>";
        if($emailstatus != "success") {echo $emailfail."<br/><br/>";} else{}
        echo anchor('home', 'กลับสู่หน้าหลัก');
   }else{
        echo "<h3>เข้าสู่ระบบแล้ว</h3>";
        echo "<pre>";
        print_r($this->session->all_userdata());
        echo "</pre>";
        echo anchor('login/logout', 'Logout')."<br/>"; 
        echo anchor('home', 'กลับสู่หน้าหลัก');
   }
?>
</body>
</html>

答案 3 :(得分:0)

也许我们应该&#34;为同一个变量分配不同的值&#34;而不是命名两个不同的变量。

if($this->email->send())
{
    $data['message'] = "email has been sent";
}
else
{
    $data['message'] = "could not send the email.";
}

另外,我想稍后您可能想要使用会话闪存数据。使用flash数据显示信息就像你的例子一样方便。

$this->session->set_flashdata('msg_to_display',$data['message']);
redirect('/your/controller');

在View页面中,可以通过

访问它
<?php echo $this->session->userdata('msg_to_display'); ?>

重点是在重定向后在会话中设置消息,但是一旦刷新页面,消息就已经消失了。

here

中了解详情