2 Replies Latest reply: Dec 21, 2013 5:12 PM by whatalotofrubbish RSS

    Setting default values in a php dynamic list

    whatalotofrubbish Community Member

      I have a drop down list that is populated by a recordset consisting of two fields, company (a text field shown in the drop down list) and company_ID (an integer key )

      The default value for the field Company should be "unknown", and the form should not submit if the sleted value is "unknown"

      The selection once made is used to insert the value into  a new record, along with many others on the full form..

       

      The code is complicated by the fact that it also includes some java script code from a check form behaviour, shown in orange for completeness.

      How do I change the PHP code so that it shows the default value of "unknown" in the select box when run, instead of showing the last item in the companies list?

      Should be simple enough, but I am too close to see the solution.

       

          

              <td>

      <select name="Companies_ID" size="1" class="contclass" id="Companies_ID" >

                <%

      while (!compny.EOF) {

      %>

                <option value="<%=(compny.Fields.Item("ID").Value)%>" <%=((compny.Fields.Item("ID").Value == (compny.Fields.Item("ID").unknown))?"SELECTED":"")%> <?php if (!(strcmp("<%=(compny.Fields.Item(\"ID\").Value)%>", $row_rsCompany['ID']))) {echo "selected=\"selected\"";} ?>><%=(compny.Fields.Item("company").Value)%></option>

                <%

        compny.MoveNext();

      }

      if (compny.CursorType > 0) {

        if (!compny.BOF) compny.MoveFirst();

      } else {

        compny.Requery();

      }

      %>

      <?php

      do { 

      ?>

                <option value="<?php echo $row_rsCompany['ID']?>"<?php if (!(strcmp($row_rsCompany['ID'], $row_rsCompany['ID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsCompany['company']?></option>

                <?php

      } while ($row_rsCompany = mysql_fetch_assoc($rsCompany));

        $rows = mysql_num_rows($rsCompany);

        if($rows > 0) {

            mysql_data_seek($rsCompany, 0);

                  $row_rsCompany = mysql_fetch_assoc($rsCompany);

        }

      ?>

                </select>

                <input name="company" type="hidden" id="company" value="<?php echo $row_rsCompany['company']; ?>" /></td>

              <td>Select from list. If it does not exist, add a Company. </td>

       

            mysql_data_seek($rsTypes, 0);

                  $row_rsTypes = mysql_fetch_assoc($rsTypes);

        }

      ?>

                </select>

                <input name="company" type="hidden" id="company" value="<?php echo $row_rsCompany['company']; ?>" />

        • 1. Re: Setting default values in a php dynamic list
          bregent CommunityMVP

          Maybe I don't understand the problem, but you just need to add a static value to the list before calling the dynamic script to populate the rest of the values.

           

          <select name="Companies_ID" size="1" class="contclass" id="Companies_ID" >

               <option selected value="Unknown" >Unknown</option>

          • 2. Re: Setting default values in a php dynamic list
            whatalotofrubbish Community Member

            Thanks Bregent - your comment made me think again, and now the problem does not exist - in fact it never really did.

             

            I did not explain it properly.

            The form that I am using has many different items on it, some of which are static and some that are not.

            All the static fields show the word "unknown" when the page loads.

            None of the dynamic fields do, but I would like them to.

             

            Here is a section of newly created code as a test. I first created a drop down list with a default of "unknown" and then dynamically populated it. This produced the following code:

             

            <form action="" method="get">

            <select name="testing" size="1">

              <option value="unknown" <?php if (!(strcmp("unknown", $row_Recordset1['ID']))) {echo "selected=\"selected\"";} ?>>unknown</option>

              <?php

            do { 

            ?>

              <option value="<?php echo $row_Recordset1['ID']?>"<?php if (!(strcmp($row_Recordset1['ID'], $row_Recordset1['ID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['type']?></option>

              <?php

            } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));

              $rows = mysql_num_rows($Recordset1);

              if($rows > 0) {

                  mysql_data_seek($Recordset1, 0);

                  $row_Recordset1 = mysql_fetch_assoc($Recordset1);

              }

            ?>

            </select>

            </form>

            When I am in split view, all I see is a box with "unknown" in it, which is what I want.

            When I switch to live view, or view it in a browser, It shows the word "Z Smith & Co" - which is the last record on file in the current sort order.

            What I was trying to attain, was to put the static text "unknown" in this field, so that if the user does not select anything then the word "unknown" goes into the record of the (different) table that is being created.

            However, on reflection, I must allow the user to select whatever he likes into this field. If he does not know the company that should be there, he selects unknown from the list.

            Just shows how thinking wrongly can cause lots of problems.

            Have a merry Christmas and a Prosperous New Year.