Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing php session variables via javascript

Status
Not open for further replies.

rekenaar

Technical User
Feb 16, 2005
38
0
0
ZA
Hello

Please bear with me, I am a php & javascript newbie still trying to learn.

Currently I have a page that dynamically create radiobuttons from a table in a MySQL database. These radiobuttons are
a list of business units for a company.


$result = mysql_query("SELECT Name FROM t_bizunits",$conn);

while ($myrow = mysql_fetch_array($result))
{
$bizcount += 1;
if ($myrow["Name"] == $_SESSION["bizunit"])
{
echo ("<label><inputtype=\"radio\"name=\"bizchoice\" value=\"{$bizcount}\" CHECKED onclick=reload({$bizcount}) >");
}
else
{
echo ("<label><input type=\"radio\" name=\"bizchoice\" value=\"{$bizcount}\" onclick=reload({$bizcount})>");
}
printf ($myrow["Name"]);
echo ("<br>");
}


I want it so that if the user clicks on the radiobutton it changes the session variable $_SESSION["bizunit"] to the selected
bizunit in the radiobutton and reloads the page.

Here is the JavaScript function reload:

echo ("function reload(biznr){");
// I want to change the session variable
// "bizunit" here.
echo ("document.location.reload()");
// This will then reload the page showing the details of the // currently selected bizunit
echo ("}");
echo ("</script>");

Anybody have an idea how I can achieve this? Any input will be appreciated.

Thanks in advance.
 
you cannot alter PHP session variables via javascript, as javascript is client-side, and PHP is server-side. you could append a ?bizunit=blablabla though
 
You can achieve your goal by:
1. submit the form vie e.g. a client side onClick event (or alternatively a submit button)
2. pick up the $_POST var from the radio button and assign it to the $_SESSION var.

So, it's just a different sucession of steps:
User clicks radio button (or submits via submit button)
Page reloads and through the reload the session var is changed when you assign it in the script from the submitted value.
 
Thanks for the replies I have been giving it a try using $_POST var. I did set register_globals to ON in php.ini

I get this error everytime I click on a radiobutton:
Notice: Use of undefined constant bizchoice - assumed 'bizchoice' in c:\inetpub\ on line 10


Here is the code to assign the post variable in tut.php (the main page):
<?php
session_start();

if (!isset($_SESSION["bizunit"]))
{
$_SESSION["bizunit"] = "Main Office";
}
else
{
$_SESSION["bizunit"] = $_POST[bizchoice];
}
?>

Here is the code to create the radiobuttons:
echo("<form name=\"bizselectform\" action=\"tut.php\" method=\"post\">");

They are all dynamically created similiar to this.
echo ("<label><input type=\"radio\" name=\"bizchoice\" value=\"{$myrow["Name"]}\" CHECKED onclick=reload() >");

The reload() script is just:
document.location.reload()
basically.

How can I get tut.php to see the bizchoice variable that is being passed?

Thanks in advance.
 
To get rid of the warning message (it's a warning not an error), add quotes to this line:
Code:
        $_SESSION["bizunit"] = $_POST[[red][b]'[/b][/red]bizchoice[red][b]'[/b][/red]];

Also, to make your code more readable, I suggest using single quotes to surround strings that contain double quotes. That way your lines:
Code:
echo("<form name=\"bizselectform\" action=\"tut.php\" method=\"post\">");
and
Code:
echo ("<label><input type=\"radio\" name=\"bizchoice\" value=\"{$myrow["Name"]}\" CHECKED onclick=reload() >");
become
Code:
echo '<form name="bizselectform" action="tut.php" method="post">';
and
Code:
echo '<label><input type="radio" name="bizchoice" value="' . $myrow['Name'] . '" CHECKED onclick=reload() >';

Please when posting code, please surround the lines with the [red][ignore]
Code:
[/ignore][/red]
tags.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top