0

I am working in codeigniter. I have an error in javascript code. I have a login form and want to show a javascript alert box if the login name or user name is wrong. I had completed my project on localhost and javascript is working fine on localhost. But when i upload the project on live server, it displays an error message.

A PHP Error was encountered

Severity: Warning Message: Cannot modify header information - headers already sent by (output started at /home/servername/public_html/projectdemo/wms/application/controllers/login.php:36) Filename: helpers/url_helper.php Line Number: 540

I put this javascript code in controller

<script type='text/javascript'>
function jsfunction()
{
    window.alert("Wrong User Name or Password");
}
</script>

and in controller, I called this function:

echo "<script type='text/javascript'>jsfunction();</script>";

Later I put the script in head section but same error message appears.
Can you please help me. Thanks in advance.

peterm
  • 91,357
  • 15
  • 148
  • 157
Sam
  • 243
  • 2
  • 5
  • 18

3 Answers3

0

CodeIgniter officially replies to the client using the view/template rendering system. Echoing something earlier than that part of the framework's procedure causes the server to reply to the client early, hence "headers already sent". After so, CodeIgniter cannot anymore do anything with the intended because you forced the server to reply prematurely.

What you should do is to set some flag along with the template/view data so that the view can conditionally render a section of HTML in the view/template (which would be your error alerting code)

Joseph
  • 117,725
  • 30
  • 181
  • 234
0

Headers must be parsed before any HTML, JS, CSS etc.

As https://stackoverflow.com/users/920374/setsuna mentioned you should put JavaScript into the view file. Your PHP error is basically saying that there was output before the headers for the page were sent which screws it up.

Community
  • 1
  • 1
diggersworld
  • 12,770
  • 24
  • 84
  • 119
0

Check the accepted answer here: How to fix "Headers already sent" error in PHP

Spoiler: You are not allowed to start output in the controller, that's why you have views.

Community
  • 1
  • 1
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63