2

Currently I'm in my controller and I have placed a check in the controller. Basically checking if the value of a particular field is 0 or not. Now if it is 0, I want to display an alert popup via my controller. To do this I've tried the following code:

public function status($status){ 
    if($this->input->post('id')){
        foreach($this->input->post('id') as $key => $id):
            $check_emirate = $this->listings_model->check_emirate($id);
            if($check_emirate->emirate == 0){
                echo "<script>alert('This card was not approved.');</script>";
            }else{
            $data=array('status'=>$status);  $updated=$this->listings_model->update($data,array(),$id); 
    }
        endforeach;     
    } 
    return true;
}

Now it is entering my if condition, but it doesnt show me the popup.

This is the AJAX call I'm making to enable this function:

$(".status").click(function(e) { chagestatus($(this)); }

function chagestatus(obj){ 
  if($('.chkbx').is(':checked') == true){
    $.ajax({
      type: "post",
      dataType: "json",
      async:false,
      url: "<?php echo site_url('listings/status'); ?>/"+obj.data('val'),
      data: $(".chkbx:checked").serialize(),
      success: function(result) {}
    });
  }
}

Another thing I've tried here is using json_encode this way:

if($check_emirate->emirate == 0){
                $data = "<script>alert('This card was not approved, Thanks.');</script>";
                echo json_encode($data);
            }

  success: function(result) {
    result = JSON.parse(result);
  }

But this also didn't give back any response.

4
  • What have you tried to resolve the problem? Is that code added to the markup? How did cou check that the condition is really evaluated? Commented Jan 29, 2022 at 9:02
  • @NicoHaase So this alert was not added to the markup for some reason. I checked if its getting into the condition by simply printing and dying something. I've also tried using flashdata, but that doesn't work Commented Jan 29, 2022 at 9:05
  • What else did you try to resolve the problem? As you haven't shared the rest of your code, it's close to impossible to tell you what happens elsewhere Commented Jan 29, 2022 at 9:06
  • You haven't told us that you are using AJAX in the first place. Do you evaluate the AJAX result anywhere? Calling json_encode on the server and parsing that JSON result in the browser does not evaluate the result in any case Commented Jan 29, 2022 at 12:38

1 Answer 1

4

If the function in your controller works well, then you need append your result from server to your DOM:

    success: function(result) {
        $('body').append(result);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.