Author Topic: PHP writing JavaScript not working as expected  (Read 266 times)

Forrest

  • Guest
PHP writing JavaScript not working as expected
« on: September 01, 2010, 06:59:49 AM »
Hi Guys, I am fairly new to PHP, and I recently changed my whole site to use PHP sessions, which worked fine.
Then I added PHP parts in my pages that display different menu choices depending on if a PHP session exists. This also works fine, until I found that any HTML/PHP pages that have forms/submissions no longer work, and I get kicked back to the index.php

After slow backtracking, I removed new parts, and found the forms started working again if I removed the PHP part that chooses which JavaScript file to reference, as follows from contact.php

<?php
if (isset($_SESSION['SESS_MEMBER_ID'])){
    echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"menu_horz.js\"></script>";
}
else {echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"log_menu_horz.js\"></script>";
}
?>

I tried changing the echo ".." to use single quotes ' '  instead, but then it doesn't work at all then with a form or no form on the page. I assume I am doing something wrong, but I can't find any resources that give me a definitive way of referencing Javascript files.  What freaked me out was only the ones with forms were an issue.

Any ideas of what the problem might be with the code above?
Will be grateful for any help you can give.

Regards
Dean

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: PHP writing JavaScript not working as expected
« Reply #1 on: September 01, 2010, 08:23:38 AM »
Are you calling session_start() before echo-ing this javascript code? Make sure you have no any output to browser before calling session_start(), even static html code, space, etc.
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

PuddleJumper9

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: PHP writing JavaScript not working as expected
« Reply #2 on: September 01, 2010, 08:41:48 AM »
Hi, thanks for the fast response.

The code at the top of all my pages starts as follows:

<?php session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>

......

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: PHP writing JavaScript not working as expected
« Reply #3 on: September 01, 2010, 09:05:47 AM »
Well, if so, the code where you echo javascript (in your first post) should not be a problem at all. Are you sure that after removing just this piece of code your form start working? Maybe you also removed something else?
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

PuddleJumper9

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: PHP writing JavaScript not working as expected
« Reply #4 on: September 01, 2010, 09:28:59 AM »
- To test it again with the same file I sent the example from (as I had been testing with a different form), and I removed literally only those lines I put in the first post. And it worked like a charm.

I have a similar PHP part on each page also, but that is never an issue (below):
<?php
if (isset($_SESSION['SESS_MEMBER_ID'])){
    echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"member-left.js\"></script>";
echo "<br />";   
}
?>

- Could it be more weirder :-)

- I wanted to find a work-around, but it would mean having PHP inside my JavaScript file, but I am not sure it would work, since JS is client side, and PHP server...  Essentially, I just want to run certain JS menus depending if the person is logged in or not.