Hi All,

I am stuck. I have some javascript code for 'FreeRichTextEditor' which is a WYSIWYG editor for a textarea, and I cannot seem to retain the SESSION data for it if you either go back with the back button or my script to go back (java 'javascript:history.go(-1)') and its not the java, its my PHP.

NOTE: this form is one page. I check for SUBMIT at the start, then process if isset

if(isset($_POST['Submit'])) {
rest of the code

Here is the function for the RTF editor...

function freeRTE_Preload($content) {
	// Strip newline characters.
	$content = str_replace(chr(10), " ", $content);
	$content = str_replace(chr(13), " ", $content); 
	// Replace single quotes.
	$content = str_replace(chr(145), chr(39), $content);
	$content = str_replace(chr(146), chr(39), $content);
	// Return the result.
	return $content;
}
// Send the preloaded content to the function.
$content = freeRTE_Preload("");

//then call it in my form
<script src="http://www.site/staff/includes/js/richtext.js" type="text/javascript" language="javascript"></script>
<!-- Include the Free Rich Text Editor Variables Page -->
<script src="http://www.site/staff/includes/js/config.js" type="text/javascript" language="javascript"></script>
<!-- Initialise the editor -->
<script>
initRTE('<?php echo $content; ?>', 'example.css');
</script>

Now, if I I hit Submit, with this code at the beginning. (I entered 'test' in the description text field).

echo '1) '.$description.'<br>';
$description = $_SESSION['description'];
echo '2) '.$description.'<br>';

I get...

1) test
2)

Then if I go back, I get...

1)
2)

So the description was there, then calling the SESSION['description'] cleared it.

How can I retain this, or is it not possible because this is a javascript app?

Thanks as always,
Don

    Are you using [man]session_start/man at the beginning of each script that reads from and/or writes to the $_SESSION array?

      Hi NogDog,

      I had it at the start of the page, but thought about it and I believed that will start the session over and erase my data.

      The page that leads to this page has

      session_start();
      

      at the top.

      And since this page is self-contained, my explanation, it has the process code 1st then the form, that would delete the session data.

      Am I wrong?

      Thanks ND,

      Don

        No, session_start() does not delete any data. It looks to see if the current request has an already existing session ID (either from a session cookie or a session ID in the GET or POST data). If it finds it, then it establishes a session using that ID, giving the user access to anything previously saved in the $SESSION array (under the auspices of that particular session ID). If it does not find an existing session ID for that request, then it generates a new session ID and uses that for any subsequent saving of $SESSION data. Every page that needs to read from or write to the $_SESSION array must have a session_start() before any such access (and before any headers are sent).

          PS: perhaps a better way to thing of session_start() is as session_continue_if_exists_else_start_a_new_session(). 🙂

            OK, I have the session started 1st thing.

            And I do some error checking for missing fields and if I print_r the $_POST data after filling in the comments field, I get this.

            [$_SESSION] => Array
            (
            [\'comments\'] => 6465465
            )

            Now I contacted the author of the javascript WYSIWYG code and he suggested...

            I am not quite sure if this method you are trying to do would work, you would preload it somethng like initRTE($_SESSION["description"], ............ etc etc.

            which I fixed and did this...

            initRTE('<?php echo $_SESSION['comments']; ?>', 'example.css');

            Which now gives me what I showed above.

            Whats still strange is I do a

            echo '$_SESSION comments = '.$_SESSION['comments'];
            

            right after my session start and I do not get any values at all, after hitting Submit?

            Strange,

            I appreciate your help,

            Don

              Not quite following all this, but here are some session basics and tips that might help:

              1) session_start(); is required for pages to load session information that already is out there. If no session is available, it creates a new session to work with.
              2) Tip: If you are building a website with a consistent interface, write the interface into a header.php and include it on each page you create. In the header.php include your session_start(). That way you never forget it...
              3) You can set variables and retrieve them with the $SESSION['variable_name'] function. So, if I set $SESSION['first_name']="Joe", this variable is available on every page that calls session_start() whether I use it or not.
              4) Session information must be passed from page to page somehow. PHP first tries to write a cookie on the user's computer. If the user has their browser set to reject cookies, then session data can be passed by a predefined variable called PHPSESSID.
              5) If you are posting data from a form, you can include a hidden input within the form as thus: <input type="hidden" name="PHPSESSID" value="$PHPSESSID" />. This only works if PHP is processing the data first, like with a PHP echo statement. So, in reality, it would probably look more like this: echo "<input type=\"hidden\" name=\"PHPSESSID\" value=\"$PHPSESSID\" />";
              6) You can also send the information via a GET statement, which carries the PHP session data on the URL itself. Example: echo "<a href=\"www.somewebsite.com?PHPSESSID=$PHPSESSID\">link</a>"; This isn't considered as secure, as someone can mess with the session information. You can mask this from being seen by hiding it within a one window frame, but it still isn't that secure.
              7) When you start session_start(), it first checks for the cookie, then it looks for the PHPSESSID variable either through GET or POST.
              8) Once you are done with the session, like when a user logs out, you will want to use session_unset() to clear all data within the session, followed by session_destroy() to destroy the session itself.

              Because Javascript is processed after PHP processes data, you can use PHP variables to set data that javascript can use afterwards. This gives you some more flexibility in your programming.

              I hope this helps...

                Hi jkurrle,

                I appreciate the thorough explanation. It helps put it all together.

                I will be using a header.php which is where I store all my scripts, like you suggested.

                On this issue I cant quite apply your logic. The script for the input field (a rich text editor) is Java and in the Java code the field is labelled like such.

                // Name of the hidden form field.
                rteFormName = "freeRTE_comments";

                Then in the PHP form the function was originally called like this.

                <script>
                initRTE('<?= $content ?>', 'example.css');
                </script>
                

                and based on the Authors advice, I changed it to this.

                <script>
                initRTE('<?=$_SESSION['freeRTE_comments'];?>', 'example.css');
                </script>
                

                And after the session start in my script, I do an echo.

                echo '$PHPSESSID = '.$PHPSESSID.'<br>';
                echo '$_SESSION freeRTE_comments = '.$_SESSION['freeRTE_comments'];
                

                and I get

                $PHPSESSID = 86ca90227dde42e60bcd8f1e2f06a0be
                $_SESSION freeRTE_comments =

                when I go back to the form after submitting it.

                So, I am at a loss where to go.

                I have tried dozens of different approaches and none worked.

                Please tell me you see something I do not. 🙁

                Thanks again,
                Don

                  Ok, your session variable freeRTE_comments is not being set.

                  It may be because you are declaring the PHP code with <?. This is a shorthand declaration that some PHP programmers like to use, but not all PHP implementations allow this. I would try replacing <? with <?php. The closing tag of ?> stays the same.

                  Where in your code do you set freeRTE_comments in the first place? I see you calling it, but where do you initially set it?

                  One of these questions might steer you in the right direction...

                    FYI, Java and Javascript are two totally different languages...

                      OK, I changed it to

                      initRTE('<?php $_SESSION['freeRTE_comments'];?>', 'example.css');
                      

                      normally I would code it like this.

                      initRTE('<?php echo $_SESSION['freeRTE_comments'];?>', 'example.css');
                      

                      but if the 1st is compliant, then OK.

                      As far as where I call freeRTE_comments in my code, I dont. Its is assigned in the javascript (sorry, didn't know that) I showed above.

                      Is that the issue?

                      Thanks,
                      Don

                        I just saw this, in the Javascript call the code is enclosed by single quotes and the variable of the SESSION is also, should I change it to

                        initRTE("<?php echo $_SESSION['freeRTE_comments'];?>", 'example.css'); 
                        

                        double quotes?

                        Don

                          I don't think you can share variables between javascript and PHP, unless you pass them with GET or POST. I could be wrong though. Hopefully, someone with more experience than I can give a more definative answer.

                          One trick I like to do is POST information back to the page I'm working on, so I can further define how my scripts work. I'm not sure if this is possible for you to do this, but if you can, it might offer a possible solution.

                            OK, the saga continues.

                            I tried just about everything and nothing worked. So I found another Javascript WYSIWYG html editor and replaced the code in my joborder page and it worked.

                            So I then replaced the script in my editjoborder page and it did not work.

                            After checking both for a common issue, I found nothing and then went back to the joborder page and it stopped working?!

                            I decided to put a copy of the site on my localhost server (WAMP current version) and when I go to either page I get this error on my machine.

                            Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

                            I have register_globals off and I am running PHP 5.2.3.

                            My host has register_globals on and is running PHP 4.4.6.

                            Whatever is wrong I believe is tied to this somehow.

                            Does any off this make sense or lead anyone to a possible resolution?

                            Thanks,

                            Don

                              Write a Reply...