Welcome Guest, Not a member yet? Register   Sign In
How to return HTML from a file on an AJAX callback.
#1

[eluser]Unknown[/eluser]
Hi,

On an AJAX callback to the client I would like to send basic markup. This markup currently has some PHP in it but I need to first read this content from a file, parse it with PHP and then send it back to the client using an echo.

Is this the best way to do this or is there a better cleaner simpler way?

Code:
// The method that handles the AJAX call.
public function showContents(){

$contents = file_get_contents('../path/myloginForm');
eval($contents);
echo $contents;
}

-------------------------------------------
MARKUP

<link rel="stylesheet" type="text/css" href="<?php base_url().'application/components/dhtmlxForm/dhtmlxForm/codebase/skins/dhtmlxform_dhx_skyblue.css'?>">
[removed][removed]
[removed][removed]

[removed]
  formStructure = [
      {type:"settings",position:"label-top"},
      {type: "fieldset",name:"calculator", label: "Calculator", list:[
          {type: "input", name: 'firstNum', label: 'First number:'},
          {type:"input", name:"secNum", label:"Second number:"},
          {type:"input", name:"resNum", label:"Result:"},
          {type:"newcolumn"},
          {type:"button", name:"plus", width:20,offsetTop:2, value:"+"},
          {type:"button", name:"minus",width:20,offsetTop:10, value:"-"},
          {type:"button", name:"multiply",width:20,offsetTop:10, value:"*"},
          {type:"button", name:"divide",width:20,offsetTop:10, value:"/"}
      ]}
];

var myForm = new dhtmlXForm("form_container",formStructure);
[removed]

<div id="form_container">/div>
#2

[eluser]CroNiX[/eluser]
I'd just store the html in a plain view file (with your php).

Then in your ajax call to the server:

Code:
$view_data['title'] = 'hello';  //some data to send to the view
$data['html'] = $this->load->view('view_file', $view_data, TRUE);
echo json_encode($data);
//send the html back in json

In the ajax complete event...
Code:
function(data) {
  $('#some_div').html(data.html);  //set the contents of #some_div with the returned html
}
#3

[eluser]Unknown[/eluser]
Thanks,

That is what I needed to do it is working now.




Theme © iAndrew 2016 - Forum software by © MyBB