Использование PHP для заполнения HTML массива select может отображать только последний элемент в массиве - PullRequest
0 голосов
/ 03 августа 2020

Большая часть этого объясняется в примечаниях в заголовке кода HTML ниже. Я не хочу использовать JavaScript, JQuery или какие-либо другие методы. Я хочу сделать это, используя только HTML и PHP, если это возможно. Это отображается в виде таблицы с одной строкой. В приведенном ниже коде используется код PHP для заполнения массива PHP, а затем эти элементы назначаются в качестве параметров в списке выбора HTML. Эта часть работает. Также, когда я выбираю любой вариант в списке выбора и публикую его с помощью submit, он публикует ОК, а на странице refre sh выбранное значение сохраняется в поле ввода, что я и хочу. Это переменные, которые появляются в последнем столбце таблицы, которые всегда устанавливаются на последний элемент в массиве select (последний параметр в списке), как будто входная переменная сбрасывается. Я вроде как вижу это, но не знаю, как это исправить. Я чешу затылок в течение нескольких дней и не нашел другого (никакого JavaScript) совета по net. Не уверен, что именно я не понимаю.

Код ниже. Если вы скопируете и вставите его на рабочий сервер, он должен быть полностью функциональным.

        <!DOCTYPE html>
        <html>
            <head>
                <!--
                    Notes for newer newbies than me.
                    In HTML a select list is an array, where the name of the array is the name assigned with
                    the name attribute in the select tag, and each option tag is an element in the array.
                    This code establishes a select list in HTML, and uses PHP to create another array and a 
                    FOR loop to fill the array with numerical values. This is more convenient than typing in
                    over 400 <option></option> values, and allows some flexibility to what may be in the select
                    list.
                    The HTML table has three cells: a label, the input field, and the result of the POST, which
                    just verifies what will later be saved into an XML file on the server.
                    
                    PROBLEM!: The select list fills perfectly. However, the result in the last cell of the table
                    (which is what will get saved to XML) is only always set to the last option in the list, which
                    is the last element in the select array. WHY?
                    Any value can be choosen in the option list and posted (and it appears to actually be POSTED
                    corretly). Why is the input variable not keeping its selected value past the line where
                    the option value is assigned in the select array (should be line 90 if you copy and paste
                    into your own editor.) The page if fully functional if you copy it to a server.
                    Thanks for any advice.
                -->
                <style>
                    table {border-collapse:collapse; border:5px solid black; padding:1em; width:100%; }
                    td {border:1px solid black;}
                    th {border:2px solid black;}
                    .colINPUTdisplay {background-color: #88CCCC;}
                    .colXMLdisplay {background-color: #00ffff; padding-right:1em;}
                    input, select {background-color: #000000; color: #00ff00; width:90%}
                </style>
            <head>
        <body>

        <form action="autofillSelectArrayTest.php" method="post" autocomplete="off">

        <table style="border-color:blue; border-width:7px">
        <col />
        <col class="colINPUTdisplay" />
        <col class="colXMLdisplay" />
            <tr>
                    <?php 
                        if(isset($_POST["AllowedNumbers_input"])){$AllowedNumbers_input = $_POST["AllowedNumbers_input"];}
                        else{$AllowedNumbers = $AllowedNumbers_input = "ain't nothin been posted yet";}
                        
                        
                            
                                // Declare an array of allowed numbers.
                                $AllowedNumbersArray = array();
                                
                                // Fill the array.
                                $AllowedNumbersArray[0] = "1/32";
                                $AllowedNumbersArray[1] = "1/16";
                                $AllowedNumbersArray[2] = "1/8";
                                $AllowedNumbersArray[3] = "1/4";
                                $AllowedNumbersArray[4] = "1/2";
                                for($arrayIndex = 5; $arrayIndex <= 405; $arrayIndex++){$AllowedNumbersArray[$arrayIndex] = ($arrayIndex - 5);}
                                $AllowedNumbersArray[410] = "1/64";
                                $AllowedNumbersArray[411] = "1/128";
                                $AllowedNumbersArray[412] = "1/256";
                                $AllowedNumbersArray[413] = "1/512";
                                $AllowedNumbersArray[414] = "1/1024";
                                $AllowedNumbersArray[415] = "1/10";
                                $AllowedNumbersArray[416] = "1/100";
                                $AllowedNumbersArray[417] = "1/1000";
                        
                                //These were placed in the different places of the options to see what was displayed where on the page.
                                //Not being used on current page.
                                $A = "Value of Top Option";
                                $B = "Text of Top Option";
                                $C = "Value of Bottom Option";
                                $D = "Text of Bottom Option";
                    
                    
                    ?>
                <td>Dimension Selection: </td>
                <td>
                    <!-- NEXT LINE FOR DEBUGGING ONLY -->
                    <?php echo "This is at TOP of SELECT <br/>&#36;AllowedNumbers_input = " . $AllowedNumbers_input . "<br/>"; ?>
                    
                    <select name="AllowedNumbers_input">
                        <option selected="selected"><?php echo $AllowedNumbers_input; ?></option>
                        <!-- NEXT LINE FOR DEBUGGING ONLY -->
                        <option value=""><?php echo "<br/> This is UNDER &lt;option selected='selected'&gt; <br/>&#36;AllowedNumbers_input = " . $AllowedNumbers_input . "<br/>"; ?></option>
                                    
                        
                            <!-- looping through the array building option list -->
                            <?php foreach($AllowedNumbersArray as $AllowedNumbers_input){ ?>
                            
                            <!-- NEXT LINE FOR DEBUGGING ONLY -->
                            <option value=""><?php echo "<br/> This is UNDER the FOR EACH <br/>&#36;AllowedNumbers_input = " . $AllowedNumbers_input . "<br/>"; ?></option>
                            
        <!-- Problem is here --><option value="<?php echo $AllowedNumbers_input; ?>"><?php echo $AllowedNumbers_input; ?></option>
                            <?php } ?>
                            
                            <!-- NEXT LINE FOR DEBUGGING ONLY -->
                            <option value=""><?php echo "<br/> This is AFTER the OPTION <br/>&#36;AllowedNumbers_input = " . $AllowedNumbers_input . "<br/>"; ?></option>
                    </select>
                    
                        <!-- NEXT LINE FOR DEBUGGING ONLY -->
                        <?php echo "<br/> This is at BOTTOM of SELECT <br/>&#36;AllowedNumbers_input = " . $AllowedNumbers_input; ?>
                </td>                                    
                        <?php
                                if($AllowedNumbers_input == "1/2"){$AllowedNumbers_num = .5;}
                            else if($AllowedNumbers_input == "1/4"){$AllowedNumbers_num = .25;}
                            else if($AllowedNumbers_input == "1/8"){$AllowedNumbers_num = .125;}
                            else if($AllowedNumbers_input == "1/16"){$AllowedNumbers_num = 0.0625;}
                            else if($AllowedNumbers_input == "1/32"){$AllowedNumbers_num = 0.03125;}
                            else if($AllowedNumbers_input == "1/64"){$AllowedNumbers_num = 0.015625;}
                            else if($AllowedNumbers_input == "1/128"){$AllowedNumbers_num = 0.0078125;}
                            else if($AllowedNumbers_input == "1/256"){$AllowedNumbers_num = 0.00390625;}
                            else if($AllowedNumbers_input == "1/512"){$AllowedNumbers_num = 0.001953125;}
                            else if($AllowedNumbers_input == "1/1024"){$AllowedNumbers_num = 0.0009765625;}
                            else if($AllowedNumbers_input == "1/10"){$AllowedNumbers_num = .1;}
                            else if($AllowedNumbers_input == "1/100"){$AllowedNumbers_num = .01;}
                            else if($AllowedNumbers_input == "1/1000"){$AllowedNumbers_num = .001;}
                            else{$AllowedNumbers_num = $AllowedNumbers = $AllowedNumbers_input;}
                            
                        ?>
                        
                        <?php 
                            $AllowedNumbers = $AllowedNumbers_input;
                            $AllowedNumbers_str =  "{" . $AllowedNumbers  . "}";
                        ?>
                    
                <td class="alignNumber"><?php echo $AllowedNumbers_str . "<br/>"; echo $AllowedNumbers . "<br/>"; echo $AllowedNumbers_input; ?></td>
            </tr> 
            
            <tr>
            <!-- Submit Form Button -->
                <td class="center" colspan="3"><input class="center" width="100%" type="submit" value="Click To POST selected number"></td>
            </tr>
            
        </table>

        </form>

        </body>

        </html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...