0
votes

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.

1
Try assigning the $table->name to a variable and using the variable - clearshot66
Hi, @clearshot66 Now I'm getting the following results: " . name . " - user3524193
what does var_dump($wp_source) give you? - BizzyBob
var_dump($wp_source) gives me a drop down that contains that statement, @BizzyBob - user3524193
put it after your last echo. I just want to see what the structure of the $wp_source output is. - BizzyBob

1 Answers

-1
votes

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);  
  }