Jump to content

Weird PHP behaviour (on a page containing HTML Form submit)


PuddleJumper9

Recommended Posts

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 form/submits 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.

I am using <?php session_start(); ?> at the beginning of every page (before any output). Other PHP code on the page is working fine. Only the above part seems to be the problem, and works too, as long as I don't submit a form.

 

- Could it be more weirder :-)

 

- I tried also to find a workaround, 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.

 

- Example contact.php attached

 

Any ideas of what the problem might be with the code above?

Will be grateful for any help you can give.

 

Regards

D

 

[attachment deleted by admin]

Link to comment
Share on other sites

Do you require the all the backslashes in your code?

echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"menu_horz.js\"></script>";

doesn't this work:

echo "<script language="JavaScript" type="text/javascript" src="menu_horz.js"></script>";

and besides that do you need the language attribute as well?

Link to comment
Share on other sites

- My understanding is the slashes are required to Escape the double quotes. Otherwise the Echo statement would not know where the end of the statement is...i believe.

- I did try using single quotes on the Echo, as PHP interprets them differently, depending on variables and literal strings. But I think for Escaping special characters the double quotes are needed.

 

- The weird thing though, in each PHP page I have, I also use another PHP part that does something similar, but it doesn't have an issue with the form/submit pages. The PHP part that does not affect form/submit is below:

 

<?php

if (isset($_SESSION['SESS_MEMBER_ID'])){

    echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"member-left.js\"></script>";

echo "<br />"; 

}

?>

 

The only real difference is there is no Else statement, only the If statement.

Link to comment
Share on other sites

NIce you were right I think ;) You do indeed to use a backslash.

BUt look at this topic: http://www.webdeveloper.com/forum/showthread.php?t=66923 the for last post gives some more stuff

-edit: Well i think i found out what is going  on Just looked in my books and this is what I found:

"You don't have to escape quotes when they don't conflict, such as a single quote inside of a double-quoted string: "he's lost!"  "

 

Single quoted strings only allow the \' and \\ escape characters --- all other escape characters can only be used in double quoted strings.

 

Well i am getting confused the more i read so if someone has a better insight now is the time to speak up ;)

as far as i know this should work

 

echo '<script type="text/javascript" src="menu_horz.js"></script>';

 

Link to comment
Share on other sites

- Thanks for your efforts here.

- I tried as you showed, and the same result....I get kicked back to the Index.php, instead of my form getting submitted.

 

The php code I used now is:

<?php

if (isset($_SESSION['SESS_MEMBER_ID'])){

    echo '<script type="text/javascript" src="menu_horz.js"></script>';

}

else {

echo '<script type="text/javascript" src="log_menu_horz.js"></script>';

}

?>

Link to comment
Share on other sites

Well I just opened your file (which i am not a big fan of downloading and opening files) But the problem I think is not your php part that is echoing javascript. Just checked and this just works:

<?php
      echo 'with single quotes <br />';
      echo '<script type="text/javascript">
          document.write("Hello world");
            </script><br />';
      echo 'with double quotes <br/>';
      echo "<script type=\"text/javascript\">
          document.write(\"Hello world\");
            </script><br />";
      ?>

 

What doesn't work, is your submit button. Besides that your script is really hard to read with all the tables and javascript depended mark up (why dont use div's?) Your button is missing a name attribute. like:

echo '<input type="submit" value="Om nom nom" name="submit" />';

at the  moment your button looks like this:

<input type="submit" value="Send Info" class="submitbutton">

Maybe its a wise thing to make a copy of your script, strip your it to its bare bones; no tables no inline css, just some bogus values and a submit button. right now its very hard to read and to isolate the problem.

 

if i have some time I'll recreate your form to it's bare bones

 

Link to comment
Share on other sites

In addition to the above, I just stripped your code to it's bare bones and noticed, that there was no way that the condition of

if (isset($_SESSION['SESS_MEMBER_ID'])){;}

could ever be true. In other words there was no moment the Session var was set to a certain value.

 

I havent tested the stuff below but as you can see I am giving the session var a value:

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

<title>English Agency (UK) - Contact Us</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="coolstyle.css" type="text/css">

<script  type="text/javascript" src="mouseover.js"></script>
<script type="text/javascript" src="javascripts.js"></script>
<link rel="Shortcut Icon" href="favicon.ico">
</head>

<body>

<?php
if (isset($_POST['submit'])){
    $_SESSION['SESS_MEMBER_ID'] = 'fatmonkey';
    echo 'SESSION_MEMBER ID is set<br />';
    
    
}else{
        echo 'press submit';
    }


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>";
}
// echo session var
echo $_SESSION['SESS_MEMBER_ID'];
?>

<form method="post" action="formmail.php" target="_top">

<input type="submit" value="Send Info" class="submitbutton" name="submit"> <input type="reset" value="Clear Form" class="submitbutton" name="reset"><br>
</form>
</body></html>

Link to comment
Share on other sites

Hi again :-(

 

- The DIV thing, yeah I guess i should switch over, but I will have to do that later,

 

- I originally believed it was because of the Submit button, but I couldn't explain why the form submission works without the PHP if , else statement.

 

- I am not sure if I want the $_SESSION['SESS_MEMBER_ID'] to be set any other way than if the person is already logged in from a previous page. The page we are dealing with (contact.php) doesn't require a user to be logged in, but only needs to display the relevant menu options in case that person is logged in.

- So I do expect isset($_SESSION['SESS_MEMBER_ID']) to be negative if the user is not logged in.

 

- I guess that is as clear as mud :-) but this is going on now for 3 weeks or so, quite frustrating.

 

Link to comment
Share on other sites

I see, well the thing is we can't see everything of your project, that's why i said, session member id = false and  figured something out. but besides that, quite some stuff in your html mark-up isn't consistent  (upper case here and than and than suddenly lower case (look at your html and body tags for instance).  Also end-tags are missing and the submit button I am pretty sure isn't formatted right either. This maybe isn't the problem you were facing and refering too, but its something that is sticking out. What might help for you is to place echo's  in the if statements. That way you know till what part the code works. And maybe first let a form work before adding additional values and tables. that way you can isolate problems. For instnace if your javascript echo is depending on the value of a session variable, echo out the session variable as a test. if it doesnt show there is your problem ;)

Ah well, i wish you best of luck and hope this helped in any way,

cheers!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.