PHP writing JavaScript not working as expected

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 ""; } else {echo ""; } ?>

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

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.

Hi, thanks for the fast response.

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

<?php session_start(); ?>

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?

  • 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 ""; echo "
"; } ?>
  • Could it be more weirder :slight_smile:

  • 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.

Sponsor our Newsletter | Privacy Policy | Terms of Service