Assign some form value into php Session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
harrylynn
Forum Newbie
Posts: 6
Joined: Sat Feb 20, 2010 6:26 pm

Assign some form value into php Session

Post by harrylynn »

Hi everyone,

I'm absolutely new to php :cry: . I just started picking up the language yesterday :mrgreen: . If i were sounding stupid, please bear with me :crazy: . I'm trying to achieve something like below using php but seems the code isn't working anymore :banghead: . The code is between <head> tag.

Code: Select all

 
<head>
<script language="javascript" type="text/javascript">
...
if(some condition is true)
{
                <?php session_start(); ?>
        <?php $_SESSION['username'] ?> = document.getElementById("usernameField").value;
        <?php $_SESSION['password'] ?> = document.getElementById("passwordField").value;
        <?php $_SESSION['filename'] ?> = document.getElementById("filename").value;
}
...
</script>
</head>
 
Any suggestions ? Thanks all in advance.
dzelenika
Forum Newbie
Posts: 10
Joined: Sat Feb 20, 2010 2:29 pm

Re: Assign some form value into php Session

Post by dzelenika »

I suggest You reading excellent online PHP manual:

http://www.php.net/manual/en/
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Assign some form value into php Session

Post by califdon »

Start out by understanding the difference between a client-side language (Javascript) and a server-side language (PHP). The difference is when and where they are executed. By the time the browser receives the page, including the Javascript, there is no more PHP, so you can't put PHP code in a Javascript conditional block and expect it to do something after the page has been sent to the browser. All PHP can do is build the Javascript code based on what it knows before anything has been sent to the browser. You will have to learn to think through your logic, knowing this. You could use Ajax, but you sure don't want to try learning Ajax before you understand PHP! What is normally done is to submit a form to the same or another PHP script that receives the $_POST variables and creates a new page to send back to the browser.
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

Re: Assign some form value into php Session

Post by xtiano77 »

Califdon made some excellent and very valid points. If all you want is to get data from a form into Session varialbes, you could try something like this:

Code: Select all

 
<?php
    session_start();
    $_SESSION["username"] = $_POST["username"];
    $_SESSION["password"] = $_POST["password"];
    $_SESSION["filename"] = $_POST["filename"];
?>
<head>
<script type="text/javascript">
 
    ... Your JavaScript code here
 
</script>
</head>
 
Now, if you want to put PHP data into JavaScript varialbes simply reverse your line of thought.

Code: Select all

 
<head>
<script type="text/javascript">
 
    var username = <?php $_SERVER["username"];?>;
    var password = <?php $_SERVER["password"];?>;
    var filename = <?php $_SERVER["filename"];?>;
 
    ... the rest of your JavaScript code here
 
</script>
 
This creates a JavaScript variable and assigns the PHP data to it by displaying its value when the code is echoed to the browser.
harrylynn
Forum Newbie
Posts: 6
Joined: Sat Feb 20, 2010 6:26 pm

Re: Assign some form value into php Session

Post by harrylynn »

Thanks. It all make sense now. :D
Post Reply