2016-12-06 50 views
1

我的JavaScript以下和平:JSON字符串,但PHP变量打印空

$('.showEditForm').click(function() { 
    var webpagefileID = this.id; 
    if($('#editForm').css('display', 'none')) { 
     $('#editForm').css('display','block'); 
     $('#allFilesTable').css('display','none'); 
    }  
    $.post 
    ('http://localhost/myproject/Controllername/display_edit_record_form', { webpagefileID: webpagefileID }, function(result) { 

    });  
}); 

使用后,我打电话方法display_edit_record_form我笨控制器

这里是在我笨控制器命名display_edit_record_form方法的代码:

public function display_edit_record_form($webpagefileID) 
    { 
     $webpagefileID = $this->input->post('webpagefileID'); 
     $data['webpagefile'] = $this->Webpage_file_model->get($webpagefileID); 
     //$this->output->set_content_type('application/json'); 
     $this->output->set_output(json_encode($data['webpagefile'])); 
     echo json_encode(array("message" => $data['webpagefile'])); 
    } 

在PHP的看法,我想显示与下面的代码数据:

var_dump($message); 
echo "<br />"; 
$myarr1 = json_decode($message); 
print_r($myarr1); 
echo "<br />"; 
$myarr2 = json_decode($data['webpagefile']); 
print_r($myarr2); 
echo "<br />"; 
$myarr3 = json_decode($webpagefile); 
print_r($myarr3); 

我也得到一个NULL和2个空数组 在控制台不过,我可以看到这一点:

现在,在控制台,打开编辑表单的时候,我可以看到JSON字符串

{"message": 
{"webpagefileID":"10", 
"webpageID":"38", 
"webpagefileName":"New file", 
"webpagefileShowInRelatedFiles" :"1", 
.......continues..... 

我需要帮助,在视图中我的PHP代码来显示这个字符串,所以我可以populoate我的表单字段有....

回答

1

的输出errorCode这是因为你从display_edit_record_form()方法得到的输出是在ajax中接收到的。所以你的$message变量显示为空白。 你需要做的是收到来自post请求的数据后,使用jquery更新你的视图。

$.post 
    ('http://localhost/myproject/Controllername/display_edit_record_form', { webpagefileID: webpagefileID }, function(result) { 
     result = $.parseJSON(result); 
     // considering you have a text field with id myID 
     $('#myID').val(result.message.webpagefileID); 
     // similarly you can add any of your returned values 
    }); 
+0

有没有什么办法可以在视图中获得php数组的结果? – user2417624

+0

不是我所知道的。如果你想在视图中使用php数组的结果,你必须调用你的控制器中的方法并将值传递给视图。但在这种情况下,这不会是异步的权利? –

+0

是的,这不是我想要的。 – user2417624

0

json_decode()结果是null这意味着解析不成功(通常是因为字符串enconding)。 你应该可以看到打印输出json_last_error

+0

我打印最后一个错误时得到0。 – user2417624