Forums

PHP

This topic is locked

Session variables - form processing

Posted 02 Oct 2004 18:54:23
1
has voted
02 Oct 2004 18:54:23 Dave Desormeaux posted:
It's the little things that can kill good code...

I'm working on a multilingual site and decided to use session variables to determine the language of choice.

I am using a simple HTML form to POST the data to the server.

My problem is that I am using radio buttons and while it works ok, users can still select more than one radio button - which I don't want. I WANT USERS TO BE ABLE TO SELECT ONE BUTTON ONLY. It's when I try to use the same name for all the radio buttons that I run into a brick wall of knowledge (clearly I am not a PHP guru).

Any help on this would be appreciated:

This is the code for the index page, where users select the language:

<?php
session_start();
header("Cache-control: private"; // IE 6 Fix.
?>

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<title>Dario Domingues</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<table width="804" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="4"><img src="images/spacer.gif" width="1" height="1" /><img src="images/spacer.gif" width="3" height="600" /></td>
<td valign="bottom" background="images/splash.gif">
<FORM ACTION="home.php" METHOD="POST" name="frmLanguage" id="frmLanguage">
<table width="149" border="0" align="right" cellpadding="4" cellspacing="0" bordercolor="#996600">
<tr>
<td width="149"><p><font size="2">
<input name="English" type="radio" value="English">
<img src="images/flag_english.gif" width="15" height="9" border="0" id="English" /> English<br />
<input name="French" type="radio" value="French">
<img src="images/flag_francais.gif" width="15" height="9" border="0" id="French" /> Francais <br />
<input name="German" type="radio" value="German">
<img src="images/flag_deutsch.gif" width="15" height="9" border="0" id="German" /> Deutsch <br />
<input name="Spanish" type="radio" value="Spanish">
<img src="images/flag_spanish.gif" width="15" height="9" border="0" id="Spanish" /> Espanol<br />
<br />
</font></p>
</td>
</tr>
<tr>
<td><div align="center">
<input name="image" type="image" value="Submit" src="images/go.gif" alt="Submit">
</div></td>
</tr>
</table>
</FORM>
<p> </p>
<p> </p>
<FORM ACTION="home.php" METHOD="POST" name="frmLanguage" id="frmLanguage">
</FORM>

</td>
</tr>
</table>

</body>
</html>


This is the code for the page where content is displayed:

<?php session_start();
header("Cache-control: private"; // IE 6 Fix. ?>

<?php error_reporting(E_ALL ^ E_NOTICE);?>
<?php require_once('Connections/connDario.php'); ?>
<?php
mysql_select_db($database_connDario, $connDario);
$query_rsHome_e = "SELECT * FROM dario_txt WHERE dario_text_id = 0";
$rsHome_e = mysql_query($query_rsHome_e, $connDario) or die(mysql_error());
$row_rsHome_e = mysql_fetch_assoc($rsHome_e);
$totalRows_rsHome_e = mysql_num_rows($rsHome_e);

mysql_select_db($database_connDario, $connDario);
$query_rsHome_f = "SELECT * FROM dario_txt WHERE dario_text_id = 1";
$rsHome_f = mysql_query($query_rsHome_f, $connDario) or die(mysql_error());
$row_rsHome_f = mysql_fetch_assoc($rsHome_f);
$totalRows_rsHome_f = mysql_num_rows($rsHome_f);
?>
<?php
// Get the user's input from the form
$Language = $_POST['English'];
$Language2 = $_POST['French'];
$Language3 = $_POST['German'];
$Language4 = $_POST['Spanish'];

// Register session key with the value
$_SESSION['English'] = $Language;
$_SESSION['French'] = $Language2;
$_SESSION['German'] = $Language3;
$_SESSION['Spanish'] = $Language4;
?>

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head><title>Dario Domingues Template</title>
<link rel="stylesheet" type="text/css" href="leftnav.css" /> <!-- CSS for the navigation menu -->
<link rel="stylesheet" type="text/css" href="content.css" /> <!-- CSS for the content - fonts, font size, colour, etc. -->
<link rel="stylesheet" type="text/css" href="pageformat.css" /> <!-- CSS for the structure of the page. -->

</head><body>
<div id="frame">
<div id="contentleft">
<h1> </h1>
<h1> </h1>
<h1> </h1>
<h1> </h1>
<?php
// Display the correct navigation language based on session information from index.php form:

if($_SESSION['French']){
include('leftnav_fr.htm');
} else if($_SESSION['German']) {
echo '<br /><br />This is the German content';
} else if($_SESSION['Spanish']) {
echo '<br /><br />This is the Spanish content';
} else
include('leftnav_en.htm');
?>
</div>

<div id="contentright">
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<?php
// Display the correct content based on session information from index.php form:

if($_SESSION['French']){
echo $row_rsHome_f['home_txt'];
} else if($_SESSION['German']) {
echo '<br /><br />This is the German content';
} else if($_SESSION['Spanish']) {
echo '<br /><br />This is the Spanish content';
} else
echo $row_rsHome_e['home_txt'];
?>
</div>
<br clear="all" /><!-- without this little <br /> NS6 and IE5PC do not stretch the frame div down to encopass the content DIVs -->
</div>

</body>
</html>
<?php
mysql_free_result($rsHome_e);

mysql_free_result($rsHome_f);
?>




Replies

Replied 03 Oct 2004 12:55:42
03 Oct 2004 12:55:42 Dave Desormeaux replied:
Solution: I figured it out, I think, but would appreciate any comments on this code - how to improve it or other ways of accomplishing the same results:


New code for the form (each radio button now has the same name, to allow users only one choice):

<input name="language" type="radio" value="English" /><img src="images/flag_english.gif" width="15" height="9" border="0" id="English" /> English<br />

<input name="language" type="radio" value="French" /><img src="images/flag_francais.gif" width="15" height="9" border="0" id="French" /> Francais <br />

<input name="language" type="radio" value="German" /><img src="images/flag_deutsch.gif" width="15" height="9" border="0" id="German" /> Deutsch <br />

<input name="language" type="radio" value="Spanish" /><img src="images/flag_spanish.gif" width="15" height="9" border="0" id="Spanish" /> Espanol<br />


New code for the page displaying content:

<?php
// Register session key with the value
$_SESSION["language"] = $_POST["language"];
?>

The above code replaces all of the following:

<?php
// Get the user's input from the form
$Language = $_POST['English'];
$Language2 = $_POST['French'];
$Language3 = $_POST['German'];
$Language4 = $_POST['Spanish'];

// Register session key with the value
$_SESSION['English'] = $Language;
$_SESSION['French'] = $Language2;
$_SESSION['German'] = $Language3;
$_SESSION['Spanish'] = $Language4;
?>

Then where data is displayed, I used:

<?php
// Display the correct navigation language based on session information from index.php form:

if($_SESSION['French']){
include('leftnav_fr.htm');
} else if($_SESSION['German']) {
echo '<br /><br />This is the German content';
} else if($_SESSION['Spanish']) {
echo '<br /><br />This is the Spanish content';
} else
include('leftnav_en.htm'); // english navigation
?>
</div>

and the same idea for the page content...


Reply to this topic