Reputation: 5129
I'm trying to implement a simple multi-language form in HTML. The form has buttons that open database tables in a div, and also has a select to choose the language.
The buttons are like:
<button type='submit' name='table' value='x'>
The select is like:
<select name='lang' onchange='changeLang()'>
...
function changeLang() {
document.getElementById('form1').submit();
}
When I click a button, it shows the table (and a parameter like 'table=x' shows in the URL). But when I change the language, only the 'lang=y' parameter shows, the 'table' parameter disappears.
I tried the solutions here, here and here, but it's not working. The hidden parameters add to the existing button and I get two 'table' parameters on the URL. How can I keep all the parameters on the URL without duplicating them?
EDIT: I was trying to do something on this line:
<?php session_start();
$erroConn = include(".../dbfile.php");
if (isset($_GET['lang'])) { // language
$lang = $_GET['lang'];
leDic($lang);
} else {
$lang = NULL;
}
if (isset($_GET['table'])) { // table
$table = $_GET['table'];
} else {
$table = NULL;
}
if (isset($_GET['people'])) { // people
$people = $_GET['people'];
} else {
$people = NULL;
}
?>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Example</title>
<script type='text/javascript'>
function changeLang() {
document.getElementById('form1').submit();
}
</script>
</head>
<body>
<form id="form1" name="form1" method="get" action="">
<h1>titulo</h1>
<?php
if (is_null($table)) {
echo "<button type='submit' name='table' value='0'>tabelas</button>"; # only shows the button if not yet pressed
} else {
echo "<input type='hidden' name='table' value='$table' />"; # if pressed keep the variable (but is doubling it?)
$people = NULL;
}
if (is_null($people)) {
echo "<button type='submit' name='people' value='0'>people</button><BR>";
} else {
echo "<input type='hidden' name='people' value='$people' />"; # if pressed keep the variable (but is doubling it?)
$table = NULL;
}
echo "<input type='text' name='msg' />";
echo "<input type='submit' value='ok' />";
$dir1 = glob('./coisas_txt*'); # all languages available as simple txt (key=value)
echo "<div id='langs' class='langs'>"; # class to make the language bar float
echo "<select name='lang' onchange='changeLang()'>";
foreach ($dir1 as $fname) {
$sigla = mb_substr($fname,-2,NULL,'UTF-8');
if ($sigla == $lang) {
echo "<option value='$sigla' selected>$sigla</option>";
} else {
echo "<option value='$sigla'>$sigla</option>";
}
}
?>
</select>
</div>
</div>
<div id='table'> <!-- where the table goes -->
<?php
if ($table !== NULL) {
}
?>
</div>
</form>
</body>
</html>
Upvotes: 0
Views: 673
Reputation: 51
Hidden fields should work.
For each table have a form element with hidden input for the tables unique id and a select for languages. For Example:
<form id="form1" name="form1">
<input type="hidden" id="table_1" name="table_1" value="x" />
<select id="lang" name='lang' onchange='changeLang()'>
</form>
<form id="form2" name="form2">
<input type="hidden" id="table_2" name="table_2" value="x" />
<select id="lang" name='lang' onchange='changeLang()'>
</form>
And have the changeLang function submit the form it's in only. Native DOM elements that are inputs also have a form attribute that points to the form they belong to so this should work. Just find the parent of the input that calls the JavaScript function in your JavaScript. Might be able to use closest here as well. Play with it.
function changeLang() {
var form = ______.form;
form.submit();
}
Then interpret the $_GET['table'] AND $_GET['lang'] variables using PHP to decide what to show. Like so:
<?php
//switch based on language...
switch($_GET['lang']){
case 'lang1':
//display table_1 if we should
if($_GET['table_1']){
?>
<!-- table_1 display in lang1 HTML here. -->
<?php
}
//display table_2 if we should
if($_GET['table_2']){
?>
<!-- table_2 display in lang1 HTML here. -->
<?php
}
break;
case 'lang2':
//display table_1 if we should
if($_GET['table_1']){
?>
<!-- table_1 display in lang2 HTML here. -->
<?php
}
//display table_2 if we should
if($_GET['table_2']){
?>
<!-- table_2 display in lang2 HTML here. -->
<?php
}
break;
default
//display table_1 if we should
if($_GET['table_1']){
?>
<!-- table_1 display in default lang HTML here. -->
<?php
}
//display table_2 if we should
if($_GET['table_2']){
?>
<!-- table_2 display in default lang HTML here. -->
<?php
}
break;
}
?>
Something like that.
Hope it helps!
Upvotes: 1