About this processing page
Because this page is processing information sent by the GET method, we find it within the web page address area (URL) in a fashion as described in Stage 2 of our saga.
Here's what you would see by accepting the default value of 'Gold Ring':
marcoPoloSaga/mPoloApp3/processMPolo2.php?
giftNdx=4&submitBtn=Send+Gift
(Again, we broke it into two lines for convenience.)
Notice we didn't send the info 'Gold Ring'; we sent a value associated with it, which in this case was its room number (index) in the array in which it was stored.
To see the gift we actually sent, we had to look up that value in the array again and use that value in our output.
As the page was loaded, PHP did some magic and checked to see if there was transmitted content. Decisons were made accordingly.
'Cave of Wonders' Commentary
Here's the code that is processed before our page ever loads:
<?php
$arr = array("Partridge in a Pear Tree", "Turtle Dove", "French Hen", "Calling Bird", "Gold Ring", "Geese-a-Laying",
"Swan-a-Swimming", "Maid-a-Milking", "Lady Dancing", "Lord-a-Leaping", "Piper piping", "Drummer Drumming");
if(isset($_GET['giftNdx'])){
//DANGER WILL ROBINSON! Make sure the gift index is valid for the array at hand!
$numElements = count($arr);
$isNumeric = preg_match('/^[0-9]{1}[0-9]*$/', $giftNdx);
if($giftNdx < 0 || $giftNdx >= $numElements || !$isNumeric) $gift = "A gift with index '$giftNdx' is not in our inventory";
else $gift = $arr[$giftNdx];
}else{
$giftNdx = "=== No gift index was received ===";
}
?>
The weird code inside the isset block is there to prevent problems when/if 'meenies' put errant values for giftNdx in the URL itself!. See the comments below under '...what if Marco doesn't like his gift?'
Gee, we have to think of EVERYTHING!
After the page has loaded, we find this chunk of PHP in the main content of the web page body:
<div id="output">
<?php
if(isset($_GET['submitBtn'])){
echo "<h2>Here's a gift for Marco:</h2>";
echo "<p>$gift</p>";
}else{
echo "<p class=\"warning\">Doesn't look like a gift index was sent!</p>";
echo "<p>$giftNdx</p>";
}
?>
</div>
Once we are sure our select menu works properly, we could place it in a utility PHP file devoted to functions that simplify our work.
Incidentally...what if Marco doesn't like his gift?!
Because the gift index was sent via GET, he could modify that number in the URL and resubmit the form...and he could get what he wants!
Additionally, clever error-trapping with compound conditional checks and regular expressions prevented him from chosing anything out of range, or moronic input. Try it!
If you are unnerved by regular expressions, see if our ReX App can be of assistance.
12/17/19
Doesn't look like a gift index was sent!
=== No gift index was received ===
Back to form submission page.