[英]codeigniter pass value of emailfunction from controller to view?
I want to pass the string from controller to view. 我想将字符串从控制器传递到视图。 So I write $data['emailstr] and ['emailfail'] like this in If-Else clause in Controller page
所以我在控制器页面的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);
But in the viewpage, I can write the success string email sent but has error occur on $emailfail string : Undefined variable: emailfail 但是在查看页面中,我可以编写已发送的成功字符串电子邮件,但在$ 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>
How should I do? 我应该怎么做?
This is because: 这是因为:
if($this->email->send())
{
$data['emailstr'] = "email has been sent";
}
else
{
$data['emailfail'] = "could not send the email.";
}
$data['emailstr'] & $data['emailfail']
$ data ['emailstr']和$ data ['emailfail']
are uninitialized if your outer if condition do not success. 如果您的外部if条件未成功,则未初始化。 I think that's happen with your case.
我认为您的案件就是这种情况。
Use isset()
instead of !=null
. 使用
isset()
代替!=null
。 You can't run a comparison on a value that doesn't exist, but isset()
determines if a variable is set and is not NULL
. 您不能对不存在的值进行比较,但是
isset()
确定是否设置了变量并且该变量不是NULL
。
if(isset($emailfail)) {echo $emailfail."<br/><br/>";} else{}
http://php.net/manual/en/function.isset.php http://php.net/manual/zh/function.isset.php
Instead of sending string send array like below and check 而不是像下面那样发送字符串发送数组并检查
$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);
In View check emailstatus like below 在查看中检查电子邮件状态,如下所示
<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>
Perhaps we should "assign different value to the same variable" instead of naming two different variables. 也许我们应该“将不同的值分配给同一变量”,而不是命名两个不同的变量。
if($this->email->send())
{
$data['message'] = "email has been sent";
}
else
{
$data['message'] = "could not send the email.";
}
Also, I think later you might want to use session flash data. 另外,我认为以后您可能要使用会话闪存数据。 It would be convenient to use flash data for displaying information like your example.
使用闪存数据来显示信息(如您的示例)会很方便。
$this->session->set_flashdata('msg_to_display',$data['message']);
redirect('/your/controller');
And in View page, it could be accessed by 在“查看”页面中,可以通过
<?php echo $this->session->userdata('msg_to_display'); ?>
The point is it set the message in the session after redirect, but once you refresh the page, the message is already gone. 关键是它在重定向后在会话中设置了消息,但是刷新页面后,消息已经消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.