Get True/False text result from drop down selection

Get True/False text result from drop down selection



I'm trying to build a drop down menu with a list of city names. For city names where the return value is "true," I want text on the same page to be displayed saying the value is true. If the selection is where the return value is false, I want it to display this as well. I'm working with form actions and php trying to function it and have completely lost it. It's a simple task that I can not for the life of me figure out.


<select name="City">
<option value="Richmond">Richmond</option> //True//
<option value="Bowling Green">Bowling Green</option>//True
<option value="Manakin">Manakin</option>//false//
<option value="Emporia">Emporia</option>//false//
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit']))
$selected_val = $_POST['City']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; "and this location is Not served"
// Displaying Selected Value

?>

</body>
</html>






You have to write your own logic to do that. Why is Richmond true while Emporia is false? If you can answer it, you have the logic.

– fabrik
Sep 18 '18 at 7:11


true


false






Handle this on server side or just show the list of true cities only.

– Chaska
Sep 18 '18 at 7:12


true






Create an array with the city names mapped to true/false: $cities = ['Richmond' => true, ...]; and check against that using something like: if ($cities[$_POST['City']]) // true else // false .

– Magnus Eriksson
Sep 18 '18 at 7:13



$cities = ['Richmond' => true, ...];


if ($cities[$_POST['City']]) // true else // false






Yes, I have the logic, but where I'm confused is how and where the logic is used to define the function. For example, Richmond is true because it's distance is true to service while Emporia is false because it's distance is false to service.

– user426404
Sep 18 '18 at 7:14




1 Answer
1



I would use an array which holds the state of the values, then you simply check the value which will determine the state.


<?php
$citys = [
'Richmond' => true,
'Bowling Green' => true,
'Manakin' => false,
'Emporia' => false
];
?>

<select name="City">
<?php foreach ($citys as $city => $avail): ?>
<option value="<?= $city ?>"><?= $city ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>

<?php
if (isset($_POST['submit']))
$selected_val = $_POST['City'] ?? ''; // Storing Selected Value In Variable

if (isset($citys[$selected_val]))
echo "You have selected:".$selected_val." and this location is ".($citys[$selected_val] ? '' : 'Not').' served';
else
// error, city not in array


?>






Well, that worked. You saved me from bashing the keyboard. Thank you!

– user426404
Sep 18 '18 at 7:20






No worries glad to help.. using an array allows you later to abstract out the values into a db etc.

– Lawrence Cherone
Sep 18 '18 at 7:21



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)