• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Request to add alert javascript in controller page solution

#1
[eluser]jiahui[/eluser]
Halo, I have a question.

Can we add javascript within controller site or not? My purpose is to let user know, where is the data stored, after they submitted.

I tried with the below scripting, but it does not working. Please kindly advise.

Code:
function signup_process()
        {
            if ($this->input->post('groupid')==1){
                "[removed]alert("Related records has been added into Admin group.");[removed]"
            }else if ($this->input->post('groupid')==2){
                "[removed]alert("Related records has been added into Exporter group");[removed]"
            }else if ($this->input->post('groupid')==3){
                "[removed]alert("Related records has been added into Importer group");[removed]"
            }
}


For addition, anyone can provide me a codeIgniter example with using query string, pass it and use it in controller site one. Thank you.

#2
[eluser]@rno[/eluser]
Hi jiahui,

No, you cannot alert form within a controller, as the controller runs on the server, not on the client. You might however use the following:

Code:
function signup_process()
        {
            if ($this->input->post('groupid')==1){
                $data['alert_msg'] = "Related records has been added into Admin group.";
            }else if ($this->input->post('groupid')==2){
                $data['alert_msg'] = "Related records has been added into Exporter group";
            }else if ($this->input->post('groupid')==3){
                $data['alert_msg'] "Related records has been added into Importer group";
            }
}

Then, in your views header use:

Code:
<script>
<?php
if isset($alert_msg)
  echo 'alert("'.$alert_msg.'");';
?>

Hope this helps.

Regards,
Arno

#3
[eluser]rogierb[/eluser]
A controller is executed server side, javascript is executed client side side.
You have to put the javascript in HTML, either a view file (The Better Way) or echo HTML in your controller.

Another solution would be to pass a variable wth the error message to your view file
Code:
$error = "Related records has been added into Admin group.";

and in your view file
Code:
error = '<?php echo $error?>'
if(error !='') alert(error)

Edit: To late....

#4
[eluser]Zeeshan Rasool[/eluser]
[quote author="rogierb" date="1256827477"]A controller is executed server side, javascript is executed client side side.
You have to put the javascript in HTML, either a view file (The Better Way) or echo HTML in your controller.

Another solution would be to pass a variable wth the error message to your view file
Code:
$error = "Related records has been added into Admin group.";

and in your view file
Code:
error = '<?php echo $error?>'
if(error !='') alert(error)

Edit: To late....[/quote]

Yes, rogierb defined it well. You can pass whole html or pass javascript thru controller to get this way.

#5
[eluser]jiahui[/eluser]
Thank you for your all answers. But I want to make it like will prompt a alert popup box after user pressed submit button and then based on what the select form they selected, system will alert them their account has been stored in which area like that.

#6
[eluser]@rno[/eluser]
Sure, and that's exactly what's gonna happen.
You should call a view after posting the data to your database stating the users data was saved. It would be a little strange if your popup would be shown before the data was actually posted.

Does this make it a little more clear?

Regards,
Arno

#7
[eluser]Ben Edmunds[/eluser]
I really don't recommend this because I think javascript should be in the view but I can think of a few cases where you might want to have javascript in the controller so this is what I have done in the past:

controller:
Code:
$this->data['head'][] = "[removed]whatever javascript here...[removed]";
so you can do
Code:
$this->data['head'][] = "[removed]function msgPopup(){alert('alert goes here');}[removed]";

then in your view or template:
Code:
<head>
   <?php foreach ($head as $headItem):?>
      <?php echo $headItem . "\n";?>
   <?php endforeach;?>
</head>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.