I was build a code on PHP to get arguments by a form in HTML, this args must be compare to an array created earlier that contains the day of week in Spanish and English. The target of this web program is translate every day of weekend to english or spanish chosen by a checkbox. I have many troubles because i want to show all days of week passed and with my code only have one that show.
This is my code. Its not very graceful but i am learning programing and i did before this.
<!DOCTYPE HTML>
<html>
<body>
<h2>PHP autovalidado</h2>
<p><span >* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Dia 1<input type="text" name="day1"><span >*</span>
Dia 2 <input type="text" name="day2"><span >*</span>
Dia 3 <input type="text" name="day3"><span >*</span>
Dia 4 <input type="text" name="day4"><span >*</span>
Dia 5 <input type="text" name="day5"><span >*</span>
Dia 6 <input type="text" name="day6"><span >*</span>
Dia 7 <input type="text" name="day7"><span >*</span>
<br>Traducir al ingles o español:
<input type="radio" id="ENG" name="translate" value="ES">
<label for="ENG">Spanish</label><br>
<input type="radio" id="ES" name="translate" value="ENG">
<label for="ES">English</label><br>
<input type="submit">
</form>
<?php
$translatees = "";
$translateeng = "";
$day1 = "";
$day2 = "";
$day3 = "";
$day4 = "";
$day5 = "";
$day6 = "";
$day7 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$translate = test_input($_POST["translate"]);
$day1 = test_input($_POST["day1"]);
$day2 = test_input($_POST["day2"]);
$day3 = test_input($_POST["day3"]);
$day4 = test_input($_POST["day4"]);
$day5 = test_input($_POST["day5"]);
$day6 = test_input($_POST["day6"]);
$day7 = test_input($_POST["day7"]);
}
//Filtro de caracteres
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$dayweekes = array("Lunes",
"Martes","Miercoles", "Jueves", "Viernes",
"Sabado", "Domingo");
$dayweekeng = array("Monday",
"Tuesday","Wednesday", "Thursday", "Friday",
"Saturday", "Sunday");
if ($translate == "ES"){
if ($day1 == $dayweekeng[0]){echo $dayweekes[0];}
elseif ($day2 == $dayweekeng[1]){echo $dayweekes[1];}
elseif ($day3 == $dayweekeng[2]){echo $dayweekes[2];}
elseif ($day4 == $dayweekeng[3]){echo $dayweekes[3];}
elseif ($day5 == $dayweekeng[4]){echo $dayweekes[4];}
elseif ($day6 == $dayweekeng[5]){echo $dayweekes[5];}
elseif ($day7 == $dayweekeng[6]){echo $dayweekes[6];}
}elseif($translate == "ENG")
{if ($day1 == $dayweekes[0]){echo $dayweekeng[0];}
elseif ($day2 == $dayweekes[1]){echo $dayweekeng[1];}
elseif ($day3 == $dayweekes[2]){echo $dayweekeng[2];}
elseif ($day4 == $dayweekes[3]){echo $dayweekeng[3];}
elseif ($day5 == $dayweekes[4]){echo $dayweekeng[4];}
elseif ($day6 == $dayweekes[5]){echo $dayweekeng[5];}
elseif ($day7 == $dayweekes[6]){echo $dayweekeng[6];}}
?>
</body>
</html>
`
CodePudding user response:
Use a loop to generate the 7 form fields (you are free to use client-side (like javascript/etc) or server-side (php) scripting to achieve this.
$maxInputs = 7;
for ($i = 1; $i <= $maxInputs; $i) {
printf('Dia %d$1<input type="text" name="day[%d$1]"><span >*</span>', $i);
}
When your form submits, you can access the translate
and day
elements of the POST payload. Use a lookup array to translate perfectly spelled words.
// translated to Spanish by default
$trans = [
"Monday" => "Lunes",
"Tuesday" => "Martes",
"Wednesday" => "Miercoles",
"Thursday" => "Jueves",
"Friday" => "Viernes",
"Saturday" => "Sabado",
"Sunday" => "Domingo"
];
if ($_POST['translate'] === 'ENG') {
// translate to English
$trans = array_flip($trans);
}
for ($i = 1; $i <= $maxInputs; $i) {
printf(
'%s %d: "%s"',
$_POST['translate'] === 'ENG' ? 'Day' : 'Dia',
$i,
$trans[$_POST['day'][$i] ?? ''] ?? ''
);
}
This will attempt to read upto 7 input values from the submission. If a given day field is left blank, then the null coalescing operators (??
) will fallback to empty strings.
There will be hundreds of ways to do this task. There is no real way of knowing what is the "best way".