我是CI的新手,想要将json编码值传递给查看以进行进一步处理,请参阅下文,
Jquery帖子:
$('#btn_reset_password').click(function(){
var parameters = $('#reset_password_form').serialize();
//alert(parameters);
$.ajax({
url: baseurl + 'site/check_email_exist',
type: 'POST',
data: parameters,
dataType: 'json',
success: function(output_str){
if(output_str.flag == "true"){
// redirect to change password page
window.location.replace(baseurl + 'site/change_user_password');
}else if(output_str == "false"){
$('#result_msg').html("Your email is not in our database");
}else{
$('#result_msg').html(output_str);
}
}
});
});
控制器:
class Site extends CI_Controller {
public function change_user_password() {
$this->load->view('header');
$this->load->view('change_user_password');
$this->load->view('footer');
}
public function check_email_exist(){
$email = $this->input->post('email');
if(!empty($email)){
$query = $this->db->query('SELECT * FROM users WHERE email="'.$email.'"');
if($query->num_rows() > 0){
$row = $query->row();
$output_str = array('email' => $row->email,
'flag' => 'true'
);
//$this->load->view('change_user_password', $output_str);
//$output_str = "true";
}else{
$output_str = "false";
}
}else{
$output_str = "Email cannot leave blank";
}
echo json_encode($output_str);
}
}
查看文件'change_user_password.php':
<?php
//$obj = json_decode($output_str);
echo $email;
?>
<p><div id="result_msg"></div></p>
<p>
<form id="reset_password_form">
<label><b>New Password: </b></label>
<input type="password" name="new_password" style="width:250px;" />
<input type="button" id="btn_change" name="" value="Change" /><br />
</form>
</p>
如何传递/检索array $ output_str中的电子邮件值以查看“change_user_password.php”?我尝试了很多方法,但仍然没有清楚这个问题,请帮忙多多谢谢。
加了:
Codeigniter不能以这种方式接受url传递参数=&gt; page?email=abc@xyz.com?
答案 0 :(得分:1)
好的,试试这个:
在您的控制器中:
public function check_email_exist(){
$email = $this->input->post('email');
if(!empty($email)){
$query = $this->db->query('SELECT * FROM users WHERE email="'.$email.'"');
if($query->num_rows() > 0){
$row = $query->row();
$output_str = array('email' => $row->email,
'flag' => 'true'
);
//$this->load->view('change_user_password', $output_str);
echo json_encode($output_str);
}else{
$output_str = "false";
}
}else{
$output_str = "Email cannot leave blank";
}
然后在你的jquery中:
$('#btn_reset_password').click(function(){
var parameters = $('#reset_password_form').serialize();
//alert(parameters);
$.ajax({
url: baseurl + 'site/check_email_exist',
type: 'POST',
data: parameters,
dataType: 'json',
success: function(output_str){
if(output_str.flag == "true"){
// redirect to change password page
window.location.replace(baseurl + 'site/change_user_password/'+output_str.email);
}else if(output_str == "false"){
$('#result_msg').html("Your email is not in our database");
}else{
$('#result_msg').html(output_str);
}
}
});
});
最后change_password
函数应该是:
public function change_user_password($email = '') {
$data['email'] = $email;
$this->load->view('header');
$this->load->view('change_user_password', $data);
$this->load->view('footer');
}
如果您愿意,可以使用会话:
public function check_email_exist(){
$email = $this->input->post('email');
if(!empty($email)){
$query = $this->db->query('SELECT * FROM users WHERE email="'.$email.'"');
if($query->num_rows() > 0){
$row = $query->row();
$output_str = array('email' => $row->email,
'flag' => 'true'
);
//$this->load->view('change_user_password', $output_str);
$this->session->set_userdata('email',$row->email);
//echo json_encode($output_str);
}else{
$output_str = "false";
}
}else{
$output_str = "Email cannot leave blank";
}
然后使用以下命令在视图中检索它:
$this->session->userdata('email');
所以你不必在字符串查询中传递电子邮件。