The code:
<!DOCTYPE html>
<html>
<head>
<title>Greeting Service!</title>
</head>
<body>
<center>
<form method="post" action="">
<h1>What's Your Name?</h1>
<input type="text" name="name" placeholder="Name Here" />
<h4>Greet me in:
<select name="language">
<option value="option1">English</option>
<option value="option2">Chinese</option>
<option value="option3">French</option>
</select>
</h4>
<input type="submit" value="Greet Me!" />
<?php
if (isset($_POST['language'])) {
switch ($language) {
case "option1":
$result = "Hello, $name!";
break;
case "option2":
$result = "你好, $name!";
break;
case "option3":
$result = "Bonjour, $name!";
break;
}
}
?>
</form>
</center>
</body>
</html>
I'm not sure why it doesn't display the name i enter in from the input text box with the greeting according to each different language. I have used $_POST to submit the form and used a switch
and case
in my php code. Can someone help me out, i would like the name that is entered in the textbox to display when i have chosen which language i want the greeting to come out with. Thanks.
You don't have $language
defined. What you should use is $_POST['language']
:
if (isset($_POST['language'])) {
switch ($_POST['language']) {
EDIT: didn't notice the $name
variable as said by Meenesh Jain. And the last thing: you never do anything with the result. Not sure if that's intended or not, but I would add an echo $result;
at the end.
$name
is not defined
change your code like this
if (isset($_POST['language'])) {
switch ($_POST['language']) {
case "option1":
$result = "Hello, {$_POST['name']}!";
break;
case "option2":
$result = "你好, {$_POST['name']}!";
break;
case "option3":
$result = "Bonjour, {$_POST['name']}!";
break;
}
}
?>
This is happening because you are not initializing any value to $language just place code like $language = $_POST['language'];
above the switch statement.
declare a varibale $name
and assign $_POST['name']
to it
<!DOCTYPE html>
<html>
<head>
<title>Greeting Service!</title>
</head>
<body>
<center>
<form method="post" action="">
<h1>What's Your Name?</h1>
<input type="text" name="name" placeholder="Name Here" />
<h4>Greet me in:
<select name="language">
<option value="option1">English</option>
<option value="option2">Chinese</option>
<option value="option3">French</option>
</select>
</h4>
<input type="submit" value="Greet Me!" />
<?php
if (isset($_POST['language'])) {
$name = $_POST['name'];
switch ($language) {
case "option1":
$result = "Hello, $name!";
break;
case "option2":
$result = "你好, $name!";
break;
case "option3":
$result = "Bonjour, $name!";
break;
}
}
?>
</form>
</center>
</body>
</html>
tried using php to solve your question but could not get it through but Javascript does it seamlessly. Tested though. Find code below
<center>
<form method="post" action="index.php">
<h1>What's Your Name?</h1>
<input type="text" name="myName" placeholder="Name Here" id="myName"/>
<h4>Greet me in:
<select name="lang" onchange="getValue()" id="language">
<option value="English">English</option>
<option value="Chinese">Chinese</option>
<option value="French">French</option>
</select>
</h4>
<input type="submit" value="Greet Me!" />
<div id="show"></div>
<script type="text/javascript">
function getValue(){
var myName = document.getElementById("myName").value;
var lang = document.getElementById("language").value;
if(myName == null){
document.getElementById("show").innerHTML = "No name selected";
}
else{
switch(lang) {
case 'English':
document.getElementById("show").innerHTML = "Hello" + ' ' + myName;
break;
case 'Chinese':
document.getElementById("show").innerHTML = "你好" + ' ' + myName;
break;
case 'French':
document.getElementById("show").innerHTML = "Bonjour" + ' ' + myName;
break;
}
}
}
</script>
Copied the code and replace it where you have same. Glad i could help.