Page 1 of 1
Assign some form value into php Session
Posted: Sat Feb 20, 2010 6:32 pm
by harrylynn
Hi everyone,
I'm absolutely new to php

. I just started picking up the language yesterday

. If i were sounding stupid, please bear with me

. I'm trying to achieve something like below using php but seems the code isn't working anymore

. 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.
Re: Assign some form value into php Session
Posted: Sat Feb 20, 2010 6:49 pm
by dzelenika
I suggest You reading excellent online PHP manual:
http://www.php.net/manual/en/
Re: Assign some form value into php Session
Posted: Sat Feb 20, 2010 8:55 pm
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.
Re: Assign some form value into php Session
Posted: Sat Feb 20, 2010 9:31 pm
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.
Re: Assign some form value into php Session
Posted: Sun Feb 21, 2010 5:19 am
by harrylynn
Thanks. It all make sense now.
