CodeIgniter Forums
Show alert message on load view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Show alert message on load view (/showthread.php?tid=55854)

Pages: 1 2


Show alert message on load view - El Forum - 11-13-2012

[eluser]Juzt1s[/eluser]
Hello,

I want to show alert message on specific view load. However, I want that message to be shown ONLY then, when I load that view from specific function, if I load it from another one, the alert message must NOT be shown.

Maybe I can pass it somehow?
like:
Code:
$data['alert'] = 'alert("ALERT!");';

I really appreciate your help in advance.

Peace.


Show alert message on load view - El Forum - 11-13-2012

[eluser]harshitha[/eluser]
try this ,
Code:
$data['alert'] = '[removed]alert("ALERT!")[removed]';



Show alert message on load view - El Forum - 11-13-2012

[eluser]harshitha[/eluser]
try this ,
Code:
$data['alert'] = '[removed]alert("ALERT!")[removed]';



Show alert message on load view - El Forum - 11-13-2012

[eluser]Juzt1s[/eluser]
Doesn't work that way.


Show alert message on load view - El Forum - 11-13-2012

[eluser]Barwick[/eluser]
Include this in the $data array you're attaching to the view you want the alert to show:
Code:
$data['alert'] = 'alert("ALERT!")';
(FYI, you had an extra ";" that I removed).

In the view that you don't want the alert showing, just leave the variable blank so you don't get an error.
Code:
$data['alert'] = '';




Show alert message on load view - El Forum - 11-13-2012

[eluser]adamck[/eluser]
Code:
$data['alert'] = 'alert("ALERT!")';
$this->load->view('home', $data);

Surely in your code you can then see if the array is empty or not, if i remember correctly... using 'empty' will stop you getting an error if the array doesn't exist.

Code:
<?php
if(!empty($alert)) //if not empty
{
    echo '[removed]'.$alert.'[removed]';    
}
?>

This way you dont have to pass any empty alerts to the other views...


Show alert message on load view - El Forum - 11-13-2012

[eluser]Barwick[/eluser]
Also, don't think you should be calling an alert function in a variable. Essentially, that's exactly what the $data array is outputting anyways...unless I'm misunderstanding your use of the $data variable. Is that loading data into a view? Set it up something like this...

Code:
//Controller:

$data['alert'] = ''; // no alert

if (something happens)
{
   $data['alert'] = 'ALERT!';  // If "something" happens, alert
}

$this -> load -> view('view_name', $data); // pass $data into view

//Then in your view...you can output the alert...
<?php echo $alert; ?>



Show alert message on load view - El Forum - 11-13-2012

[eluser]Barwick[/eluser]
[quote author="Barwick" date="1352820392"]Also, don't think you should be calling an alert function in a variable. Essentially, that's exactly what the $data array is outputting anyways...unless I'm misunderstanding your use of the $data variable. Is that loading data into a view? Set it up something like this...

Code:
//Controller:

$data['alert'] = ''; // no alert

if (something happens)
{
   $data['alert'] = 'ALERT!';  // If "something" happens, alert
}

$this -> load -> view('view_name', $data); // pass $data into view

//Then in your view...you can output the alert...
<?php echo $alert; ?>
[/quote]

You could even ditch the "empty" alert altogether, and use this in your view instead of the echo:

Code:
<? if (isset($alert)) : ?>

I'm no CI pro, so take my tactics with a grain of salt. Someone may chime in with some better advice. Smile


Show alert message on load view - El Forum - 11-13-2012

[eluser]adamck[/eluser]
Or Jquery...?

You could pass a variable to a hidden div, or you could see if the page title = something, or base it on a URL segment?

Code:
<div id="alert">&lt;?=$alert;?&gt;</div>
<scr...>
if($('#alert').text().length > 0)
{
    alert('this is just an alert');
}
<scr...>



Show alert message on load view - El Forum - 11-13-2012

[eluser]adamck[/eluser]
Or... just for fun, we could set flash data

Controller
Code:
$this->session->set_flashdata('alert', 'This is an alert');
$this->load->view('home');

View
Code:
&lt;?php if($this->session->flashdata('alert')) { ?&gt;
<scr...>
    alert(&lt;?=$this->session->flashdata('alert');?&gt;);
</scr...>
&lt;?php } ?&gt;