简体   繁体   中英

Create a drop down from values in a database with PHP in a WordPress page

I multiple forms on various pages of a WordPress site that all have the same drop down menu. Rather than update all the values in every select each time they change, I have created a table in my WordPress database that is populated with the source names. I am attempting to use PHP in the WordPress pages in order to display the drop down, but am encountering issues. Below is the code I am using for the drop down within the HTML form on the various pages.

<?php

echo "<select id=\"source\" style=\"max-width: 165px;\" name=\"source\">";
echo "<option value=\"\">Please select...</option>";
global $wpdb;
$wp_source = $wpdb->get_results("SELECT * FROM wp_source;");

foreach($wp_source as $table){
echo "<option value=\"" . $table->name . "\">" . $table->name . "</option>";
}

echo "<option id=\"other\" value=\"other\">Other</option>";
echo "</select>";

?>

The table name is wp_source and I am using a Plugin named Exec-PHP which allows a user to utilize PHP within WordPress posts/pages/widgets/etc. What I am seeing in my drop down is the following text: name."\\">".$table->name." Any assistance you might offer would be greatly appreciated.

I did something like this using Javascript. Pass your data into the function and create an array called wp_source.

list = document.getElementById("source"); 
for (i=0; i < wp_source.length; ++i)
  {
     option = document.createElement("option");
     option.text = numbers[i];
     list.add(option);  
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM