将值加载到选择列表或在文本框中键入[关闭]

I have a requirement that

  • if a $data array has elements, then my form will contain a list with all of those values
  • if the $data array is empty, then one text field will be there.

Either editing in a text box or selecting from the list should work. The returned form values should match what's selected or typed in that field.

How can I implement this with PHP, JavaScript or Ajax?

As others mentioned, you need to make some effort but the following will help you achieve what you want:

<?php if (count($data) > 0) {  ?> // Count $data, if its more than 0 then make select object
<select name="mySelect">
    <?php foreach ($data as $key => $value) { ?>
    <option value="<?php echo $key; ?>"><?php echo $value; ?></option>
    <?php } ?>
</select>
<?php } else { ?> // If count is 0 then make an input text
<input type="text" name="myText" />
<?php } ?>