Jump to content

Displaying an Alertbox from PHP Controller class


KSI
Go to solution Solved by maxxd,

Recommended Posts

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) {}
    });
  }
}

 

Link to comment
Share on other sites

  • Solution

This is typically called a toast notification. For instance, if you happen to use Laravel you'd install toastr as a dependency in pacakge.json:

    "dependencies": {
        "toastr": "^2.1.4"
    }

Create a toast component or partial:

<script>
	@if(Session::has('message'))
		window.toastr.success("{{ session('message') }}");
	@endif
	@if(Session::has('error'))
		window.toastr.error("{{ session('error') }}");
	@endif
	@if(Session::has('info'))
		toastr.info("{{ session('info') }}");
	@endif
	@if(Session::has('warning'))
		window.toastr.warning("{{ session('warning') }}");
	@endif
</script>

Include that component/partial in your main template file:

		@include('components.toast')

Finally, set the message in $_SESSION like so:

return Response::redirect('/dashboard')->with('message', 'Data Saved!');

Obviously if you're not using Laravel you'll have to do some digging but hopefully that's a decent starting point.

Link to comment
Share on other sites

2 hours ago, maxxd said:

This is typically called a toast notification. For instance, if you happen to use Laravel you'd install toastr as a dependency in pacakge.json:

    "dependencies": {
        "toastr": "^2.1.4"
    }
 

Create a toast component or partial:

<script>
	@if(Session::has('message'))
		window.toastr.success("{{ session('message') }}");
	@endif
	@if(Session::has('error'))
		window.toastr.error("{{ session('error') }}");
	@endif
	@if(Session::has('info'))
		toastr.info("{{ session('info') }}");
	@endif
	@if(Session::has('warning'))
		window.toastr.warning("{{ session('warning') }}");
	@endif
</script>
 

Include that component/partial in your main template file:

		@include('components.toast')
 

Finally, set the message in $_SESSION like so:

return Response::redirect('/dashboard')->with('message', 'Data Saved!');
 

Obviously if you're not using Laravel you'll have to do some digging but hopefully that's a decent starting point.

Okay I think I got an idea what to use for this now. Currently using Codeigniter so the equivalent of this should be a flash message.

Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.