can't display data using ajax in codeigniter - ajax

Im using ajax to display data from controller.
My model :
public function DtOdontogram($id_pasien){
$odontogram=$this->db->query("SELECT id_odontogram, id_pasien, gigi, posisi, kondisi FROM odontogram where id_pasien=$id_pasien");
if ($odontogram->row_array()>0) {
return $odontogram->result_array();
}
return 0;
}
Controller
public function odontogram($id_pasien){
$data['pasien']=$this->m_pasien->DataPerPasien($id_pasien)->result();
$data['odontogram']=$this->m_pasien->DtOdontogram($id_pasien);
$data['sidebar']='member/dokter/sidebar_psn';
$data['content']='member/dokter/odontogram';
$this->load->view('member/dokter/main',$data);
}
Ajax
function load(){
$.ajax({
url:"<?php echo base_url('c_dokter/odontogram/');?>" + id_pasien,
type:"GET",
dataType:'json',
success:function(data){
if (data.status) {
console.log(data);
alert('succeess');
$.each(data,function(i,val){
$("#"+gigi).find("#"+posisi).css({fill: "#333333"});
});
}
},
error:function(){
alert('error ... ');
}
});
}
load();
Its always display error message in alert. Can anyone help me? If I display it using json_encode in controller, it can get the data.

Change the url parameter to :
url:"\"<?php echo base_url('c_dokter/odontogram/');?>" + id_pasien + '"',
and also the id_pasien is undefined, you have to define it.

Finally I found it by myself. I change the controller, separate between echo json encode and load view
$data['odontogram']=$this->m_pasien->DtOdontogram($id_pasien);
if ($this->input->is_ajax_request())
{
echo json_encode($data);
exit;
}
$data['pasien']=$this->m_pasien->DataPerPasien($id_pasien)->result();
$data['sidebar']='member/dokter/sidebar_psn';
$data['content']='member/dokter/odontogram';
$this->load->view('member/dokter/main',$data);

Related

Redirect to another view using AJAX call in Codeigniter

My table contains many rows from database. After clicking edit button in a row, I want to redirect it to another page. I am able get the data, but it cannot redirect to the other page.
I'm using bootstrap template, so there are different file to load on view. I do not know how to load view that consist of more than one files.
Ajax
$('#example2').on('click','#edit',function(){
var id_per = $(this).closest('tr').attr('id');
alert("ini"+id_per);
$.ajax({
url:"<?php echo base_url('c_dokter/ubahdt_perawatan/');?>" + id_per,
type:"POST",
data:{
id_perawatan : id_per,
id_pasien : id_pasien
},
dataType:'JSON',
success:function(data) {
},
error:function() {
alert('error ... ');
}
});
Controller
public function ubahdt_perawatan($id_perawatan){
$data['pasien']=$this->m_pasien->spes_Perawatan($id_perawatan);
if ($this->input->is_ajax_request()) {
echo json_encode($data);
exit;
}
$data['pasien']=$this->m_pasien->DataPerPasien($id_pasien)->result();
$data['sidebar']='member/dokter/sidebar_psn';
$data['content']='member/dokter/edit_perawatan';
$this->load->view('member/dokter/main',$data);
}
Edited answer:
public function ubahdt_perawatan($id_perawatan){
$data['pasien']=$this->m_pasien->spes_Perawatan($id_perawatan);
if ($this->input->is_ajax_request())
{
echo json_encode($data);
}
}
public function test()
{
$id_pasien=$this->uri->segment(2);
$data['pasien']=$this->m_pasien->DataPerPasien($id_pasien)->result();
$this->load->view('member/dokter/sidebar_psn');
$this->load->view('member/dokter/edit_perawatan');
$this->load->view('member/dokter/main',$data);
}
set path to test() in routes.php..
$route['test/(:any)']='controller/test';
then in ajax success function
success:function(data){
window.location.href="<?php echo site_url('test/'.data);";
},
try it.

Ajax call with Zend Framework 2

I am trying to call an action in a controller via an ajax request and it works but it also loads the layout twice and I am getting this notice: "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/." The url where the ajax request is being sent at is /admin/errors.
Here is the code:
<script type="text/javascript">
$("input").on("click", function() {
$.ajax({
type: "POST",
url: "<?php echo $this->url('admin', array('action' => 'geterror')); ?>",
data: { error_type: $("input:checked").val() },
success: function(data) {
$('#view-errors').html(data + "<br>");
}
});
});
</script>
and the code in the action:
public function geterrorAction()
{
if (!$user = $this->identity()) {
return $this->redirect()->toUrl('/login/log');
}
$user = $this->identity();
$layout = $this->layout();
$layout->setTerminal(true);
echo ErrorView::viewErrors($_POST['error_type']);
}
Here is a image to better help explain.
Appreciate any help!
Add a blank layout file inside layout directory.
use $this->layout('layout/blank'); inside the action of the controller. This will set a blank layout.
public function geterrorAction()
{
$this->layout('layout/blank');
// .... other codes
}
try setting the terminal true on the view model
public function geterrorAction()
{
if (!$user = $this->identity()) {
return $this->redirect()->toUrl('/login/log');
}
$user = $this->identity();
$layout = $this->layout();
echo ErrorView::viewErrors($_POST['error_type']);
$view = new ViewModel();
$view->setTerminal(true);
return $view;
}

ajax success print multiple data

i am new in this ajax . i got a problem that is in my ajax coding i get a multiple data in loop so how can print that multiple data in ajax success
here is my code :
$.ajax({
url: 'ajaxdate',
data: {ending:ending},
type: 'POST',
success: function(data) {
$('#info1').text(data);
console.log(data.length);
alert(data);
/*for(var item in data){
console.info(item);//print key
console.info(data[item]);//print value
}*/
}
});
function ajaxdate()
{
$data=array();
foreach ($guestbookdetail as $guestbookdetail)
{
echo $data['full_name'] = $detail['list']['full_name']."\n";
}
}
How i print that multiple data in ajax success
What you can do with is expect JSON as response.
I see the problem in your function if you are printing the full_name in loop and expecting HTML response, all it will be just a string.
What you can do is something like this
function ajaxdate()
{
$data=array();
foreach ($guestbookdetail as $guestbookdetail)
{
$data[]['full_name'] = $detail['list']['full_name'];
}
die(json_encode($data));
}
This will return a json array with collection of object of full_name eg [{full_name: 'ABC'},{full_name: 'cde'}]
Then add dataType : 'json' in you ajax.
Now you can iterate thorough the response you get in ajax success.
For an example you have this <ul id="showResultHere"></ul> tag, where you would like to show the result, then TRY this in your "success" AJAX :
success: function (data){
$.each(data, function(index) {
//alert(data[index]);
$("#showResultHere").append("<li>" + data[index] + "</li>");
});
}
Or, another way to show your "full_name" from your guestbook array, here is another option too :
success: function (data){
data.forEach(function(data) {
$("#showResultHere").append("<li>" + data + "</li>");
});
}

Relevant data form_dropdown codeigniter

I am trying to post value using javascript in form_dropdown('') but it is not posting data on form.
Jquery library is loaded. On alert it display record of dep_selected
JQUERY On view page
function get_subdepartment() {
var dep_selected = $('select[name=txtDept]').val();
$.ajax({
data: {
dep_selected: dep_selected,
},
type: 'POST',
url: 'addVacancy/getSubDept',
success: function(data){ //alert(dep_selected);
console.log(data);
$('.subdepartment').html(data);
}
})
}
Controller Page:
function getSubDept(){
if($this->input->post('txtDept')){
echo "getSubDept > Dept: ".$this->input->post('txtDept');
}
else{
echo "getSubDept > No Return";
}
}
It display "getSubDept > No Return" Please help.
Well it's obvious, that in your controller you are requesting a post value by the key txtDept, but you don't have that when you send data with AJAX. What you have now is:
var dep_selected = $('select[name=txtLocation]').val();
and
data: {
dep_selected: dep_selected,
},
So you should simply change the first line in your controller method to
if($this->input->post('dep_selected')) {
I suggest you to do some reading about POSTing values with AJAX and how to work with JSON.

Ajax Response from CakePHP Controller returning null

I'm tryin to validate an input field with an ajax call to a cakephp controller
My Ajax is:
$("#UserAlphaCode").change(function () {
$.ajax({
type: "post",
url: '<?php echo $this->webroot ?>' + "/alpha_users/checkCode",
data: ({code : $(this).val()}),
dataType: "json",
success: function(data){
alert (data);
},
error: function(data){
alert("epic fail");
}
});
});
My controller code
function checkCode() {
Configure::write('debug', 0);
$this->autoRender = false;
$codePassed = $this->params['form']['code'];
$isCodeValid = $this->find('count',array('conditions'=> array('AlphaUser.code' => $codePassed)));
if ($isCodeValid == 0){
$codeResponse = false;
} else {
$codeResponse = true;
}
echo json_encode ($codeResponse);
}
I'm pretty sure I'm using $this->params wrong here to access the data sent from the ajax request. What should I be doing instead?
Try something like:
$codePassed = $_POST['code']
you might also try putting:
$this->log($codePassed,LOG_DEBUG);
somewhere in there and examine the output in tmp/logs/debug.log
Using firebug will help debug the transport.
Don't know why it would be returning null, but I normally use $this->data to fetch form data.
And did you try debug($this->params)? If you don't have a non-AJAX form to test the request from, use Firebug or Wireshark to see what is being return by the server for the debug() call—since it will break jQuery's AJAX handler by not being in JSON.

Resources