/    Sign up×
Community /Pin to ProfileBookmark

PHP Session variables to JavaScript

Hi,

I’m a low-intermediate programmer and I am having problems conceptually grasping how I can use Javascript to grab a PHP session variable and change a form button’s innerHTML, as well as enabling or disabling it depending on the PHP Session variables.

Here is my code:

[CODE]
<?php if ((isset($_POST[‘submit_save’])) && (!isset($_SESSION[‘game_saved’]))
&& (isset($_SESSION[‘player_id1’]))) {
require_once (‘../mysql_connect_test.php’);
$query = “INSERT INTO games
SELECT NULL, game_date, p1_uname, p1_relationship, p1_name, p1wr, p1lr, p1tr,
p1wp, p1lp, p1tp, p1ws, p1ls, p1ts, p2_name, p2_uname, p2_relationship, type_game,
p1_md, p1_as, p2_md, p2_as, g1w, g1l, g1t, g2w, g2l, g2t, g3w, g3l, g3t
FROM games_temp WHERE game_id=$gid”;
mysql_query($query, $dbc);
if ($query) {
$_SESSION[‘game_saved’] = “game_saved”;
}
}
?>
<!–
These navigation buttons appear on 4 different pages
The ‘Save Game’ button is a form in and of itself to perform an insert query
into the database based on the php script above.
–>
<table align=”center” width=”378px”>
<tr><td height=”5px”></td></tr>
<tr>
<td width=”20%”><input type=”button” name=”score” value=”Score” style=”width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt” onclick=”self.location.href=(‘score.php’)” /></td>
<td width=”20%”><input type=”button” name=”charts” value=”Charts” style=”width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt” onclick=”self.location.href=(‘values_chart.php’)” /></td>
<td width=”20%”><input type=”button” name=”report” value=”Report” style=”width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt” onclick=”self.location.href=(‘#’)” /></td>
<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>
<td width=”20%”><input type=”submit” name=”submit_save” value=”Save Game” style=”width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt” /></td>
</form>
<td width=”20%”><input type=”button” name=”new_game” value=”New Game” style=”width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt” onclick=”self.location.href=(‘index.php’)” /></td>
</tr>
<tr><td height=”3px”></td></tr>
</table>
[/CODE]

If you notice, my ‘submit’ button is a form onto itself, which causes a MySQL query to take place under certain Session variable conditions. It works fine, but I would like to change the innerHTML of this button to ‘Game Saved’, as well as disabling it after it has been once clicked. I am confused about what syntax to use, where to use it, and whether I use Javascript to grab the PHP variables, or PHP to manipulate the javascript.

This is a great forum! Some of you guys are truly wizards.

Thanks, Bill

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bhardinauthorJun 04.2008 — Hi all,

I think the reason I didn't get any replies to my question was that my problem wasn't stated very clearly or correctly.

The resolution turned out to be adding some PHP code into the 'value' and 'disabled' declarations of the 'submit_save' button in the form that surrounded it. It had no complicated javascript functions at all.

The problem was to grab some PHP $_SESSION variables and have this button change text and disable or enable on these variables.

[CODE]
<?php
if ((isset($_POST['submit_save'])) && (!isset($_SESSION['game_saved']))
&& (isset($_SESSION['player_id1']))) {

require_once ('../mysql_connect_test.php');
$query = "INSERT INTO games
SELECT NULL, game_date, p1_uname, p1_relationship, p1_name, p1wr, p1lr, p1tr,
p1wp, p1lp, p1tp, p1ws, p1ls, p1ts, p2_name, p2_uname, p2_relationship, type_game,
p1_md, p1_as, p2_md, p2_as, g1w, g1l, g1t, g2w, g2l, g2t, g3w, g3l, g3t
FROM games_temp WHERE game_id=$gid";
mysql_query($query, $dbc);
if ($query) {
$_SESSION['game_saved'] = "game_saved";
}
}
?>
<!--
These navigation buttons appear on 4 different pages
The 'Save Game' button is a form in and of itself to perform an insert query
into the database based on the php script above.
-->
<table align="center" width="378px">
<tr><td height="5px"></td></tr>
<tr>
<td width="20%"><input type="button" name="score" value="Score" style="width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt" onclick="self.location.href=('score.php')" /></td>
<td width="20%"><input type="button" name="charts" value="Charts" style="width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt" onclick="self.location.href=('values_chart.php')" /></td>
<td width="20%"><input type="button" name="report" value="Report" style="width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt" onclick="self.location.href=('#')" /></td>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<td width="20%"><input type="submit" name="submit_save"

[COLOR="DarkGreen"]value="<?php // To change the name on the button
if (isset($_SESSION['game_saved'])) {
echo 'Game Saved';
} else {
echo 'Save Game';
}
?>"
<?php // To enable and disable the button
if (isset($_SESSION['game_saved']) || !isset($_SESSION['player_id1'])) {
echo 'disabled="disabled"';
} else { // Echo nothing so the button doesn't get stuck disabled.
echo '';
}
?>[/COLOR] style="width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt" /></td>
</form>
<td width="20%"><input type="button" name="new_game" value="New Game" style="width:100%; height:25px; font-style:normal; font-variant:normal; font-weight:normal; font-size:8pt" onclick="self.location.href=('index.php')" /></td>
</tr>
<tr><td height="3px"></td></tr>
</table>
[/CODE]

This problem probably could have been solved when first conceived if it had been perceived correctly. Even if I don't get answers to all my questions from this forum, I invariably get re-perceptions.

Bill
×

Success!

Help @bhardin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...