我正在使用表单构建器,我将值保存到数据库中。但是现在,我在从数据库中获取记录时遇到了问题。我使用php序列函数保存了数据。现在我试图通过使用'json_encode'来获取值。我在我的php类中使用了这个'json_encode',但是我不知道如何将这些数据提取到适当的框中,即,我想要'input type = text'的值应该将值打印到该字段并且textarea值应该追加到teaxtea领域等等。 这是我的php和jquery代码;
php
function viewForm($id=""){
$form_items=$this->editorM->get($id);
if(is_array($form_items) && isset($form_items["id"])){
$data=json_encode($form_items['forms']);
echo $data;
$html="
<div class='tab_content padded' style='display:block'>
<div style='margin:0 auto;width:60%;text-align:center'>
<form method='post' rel='ajaxed' action='".base_url()."admin/form_editor/saveForm' loadingDiv='loader_save_page' class='add_form'>
<input type='hidden' name='form_id' value='$form_items[id]' class='id' />
<div style='float:right' id='new_form'>
<div class='q_feedback' id='loader_save_page_c' style='float:right'></div>
<button type='submit' class='button save'>Save Changes</button>
</div>
</form>
</div>
</div>
";
$this->load->view("admin/edit_page",array(
"html"=>$html
));
}else{
header("location: ".base_url()."admin/form_editor/");
}
}
function saveForm(){
$data=$this->input->post("data");
$form_id=$this->input->post('id');
$modified_date=time();
$ser=serialize($data);
if(trim($form_id)!=""){
if($this->editorM->save($form_id,array(
"forms"=>$ser,
"modifieddate"=>$modified_date,
"modifiedby"=>$this->_username
)))
}
}
}
jquery
$(function(){
var num=0;
var templates={
"btn":'<div class="user_input button" rel="btn">'+
'<input type="button" name="" value="" class="btn input_field"/>',
"text":'<div class="user_input text_field" name="test" rel="text">'+
'<input type="text" name="txt-{num}" value="" class=" label"'+
'placeholder="click here to edit" />' +
'<input type="text" name="text" value="" />',
"textarea":'<div class="user_input textarea" rel="textarea">' +
'<input type="text" name="caption-{num}" class="label" '+
'placeholder="click here to edit" />'+
'<textarea name="textarea" /></textarea>'
},
editor='<ul class="edit_list"><li class="delete">Remove</li></ul>';
function createTemplate(type){
num++;
return templates[type].replace("{num}",num)+editor+"</div>";
}
$("ul.form_icons li").live("click",function(){
var _this=$(this),type=_this.attr("rel");
if(templates[type]){
$("#new_form").before(createTemplate(type))
}
});
/*--------------------------FORM-----------------------------*/
$('.add_form').live('submit', function(e){
e.preventDefault();
var form_id=$('.id').attr('value');
var textInput=[],
textArea=[],
radio=[],
radioOption=[],
selectList=[],
selectOption=[];
//txtInput
$('div.user_input').each(function(index, elm){
if($(this).is('.text_field')){
$(this).find(':input').each(function(index, elm){
textInput.push({caption:$(elm).val(),
name:($(elm).val()).replace(/\s+/g, '-').toLowerCase(), type:elm.type});
});
//textarea
}else if($(this).is('.textarea')){
$(this).find(':input, textarea').each(function(index, elm){
textArea.push({caption:$(elm).val(),
name:($(elm).val()).replace(/\s+/g, '-').toLowerCase(), type:elm.type});
});
{}
});
//posting data
var data=[textInput, textArea, radio, selectList];
$.post(BASE_URL+'admin/form_editor/saveForm', {data:JSON.stringify(data), id:form_id}, function(data){
//echo"success"
});
return false;
});
/*-----------------------END_FORM_SUBMISSION------------------------*/
/*---------------fetch_record-------------*/
$.getJSON('form_editor.php', {data:'data'}, function(data){
$.each(data, function(key, val){
var n = key;
console.log(n)
});
});
});