I'm trying to create a simple PHP script that creates canned responses for different customer issues. I'd like a few different questions that can accept multiple answers via checkboxes or a dropdown. FYI I'm a beginner, so excuse my ignorance. I tried it with a switch statement, but ran into issues with needing to use more than one variable. Then I tried multiple elseif statements but couldn't get anything to evaluate past the first question. Each checkbox would essentially have a little excerpt that echos when it is the one selected. So with three questions, the output would actually create three snippets that all display in order. This way I could simply copy and paste the output. If anybody could help me out or even just point me in the right direction, I'd appreciate it. I've been doing a lot of googling and haven't gotten what I need.
Here is some unfinished test code I was messing with to see if I could figure it out. When I try this, only the part for colors works. The next part for food always goes to the evaluates as the 'else' portion, regardless of what's actually selected.
<?php
if(isset($_POST['submit'])) {
$color_val = $_POST['Color'];
$food_val = $_POST['food'];
$drink_val = $_POST['drink'];
if ($color_val == "Red") {
echo "Red blurb";
echo "<br>";
}
elseif ($color_val == "Green") {
echo "Green blurb turb";
echo "<br>";
}
elseif ($color_val == "Blue"){
echo "True blue homey";
echo "<br>";
}
else {
echo "none selected";
echo "<br>";
}
if ($food_val == "Wings") {
echo "wingy wings";
echo "<br>";
}
elseif ($food_val == "Pizza") {
echo "pizza, pizza";
echo "<br>";
}
else {
echo "nothing selected";
"<br>";
}
?>
form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
<select name="food">
<option value="wings">Wings</option>
<option value="pizza">Pizza</option>
</select>
<select name = "drink">
<option value="soda">Soda</option>
<option value="water">Water</option>
<option value="beer">Beer</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
You are on the right track, but you need to either handle the variation in the values to the strings that you are comparing to or be consistent with the values that you are using in the html and your tests in php.
$str = "Wings";
$test = "wings";
if ($str == $test){ //false
echo "$str == $test";
}
if (strtolower($str) == $test){ //true
echo "$str == $test";
}
Your issue is with case sensitivity, Wings != wings
notice the uppercase 'W' is different from 'w'. So, we need to convert the strings to either uppercase or lowercase.
Try this:
if(isset($_POST['submit'])) {
// convert the strings to lowercase
$color_val = strtolower($_POST['Color']);
$food_val = strtolower($_POST['food']);
$drink_val = strtolower($_POST['drink']);
if ($color_val == "red") {
echo "Red blurb";
echo "<br>";
}
elseif ($color_val == "green") {
echo "Green blurb turb";
echo "<br>";
}
elseif ($color_val == "blue"){
echo "True blue homey";
echo "<br>";
}
else {
echo "none selected";
echo "<br>";
}
if ($food_val == "wings") {
echo "wingy wings";
echo "<br>";
}
elseif ($food_val == "pizza") {
echo "pizza, pizza";
echo "<br>";
}
else {
echo "nothing selected";
"<br>";
}