在使用Ninjaform插件的Wordpress中,我具有用于查询mysql数据库的自定义函数,我希望能够基于用户对供应商名称的下拉选择,在数据库中查询供应商代码,而无需访问该页面。刷新。
这是我在functions.php中必须去的地方。函数的第一部分将供应商的名称拉入一个下拉列表,该列表按预期方式工作。函数的第二部分是我尝试基于$ supplier变量填充供应商代码,但是当用户选择了供应商名称时,该字段将返回空白。
<?php
add_filter('ninja_forms_render_options','my_pre_population_callback', 10, 2);
function my_pre_population_callback($options, $settings) {
if( $settings['key'] == 'supplier_list' ) {
$connection = mysqli_connect( "serverip:port", "username", "password", "db" );
if (!$connection) { die('Not connected : ' . mysqli_error());}
$query = "SELECT SUP_Name FROM sup_supplier ORDER BY SUP_Name ASC";
$results = mysqli_query($connection,$query);
if (!$results) {
die('Invalid query: ' . mysqli_error());
}
$options = array();
$options[] = array("label" => "N/A", "value" => "");
while($row = mysqli_fetch_assoc($results))
{ $options[] = array ('label' => $row['SUP_Name'],'value' => $row['SUP_Name'] ); }
wp_reset_postdata();
}
if( $settings['key'] == 'supplier_code' ) {
$connection = mysqli_connect( "serverip:port", "username", "password", "db" );
if (!$connection) { die('Not connected : ' . mysqli_error());}
$supplier = $_GET['SUP_Name'];
$query = "SELECT SUP_AccountNo FROM sup_supplier WHERE SUP_Name = '".$supplier."' ORDER BY SUP_Name ASC";
$results = mysqli_query($connection,$query);
if (!$results) {
die('Invalid query: ' . mysqli_error());
}
$options = array();
while($row = mysqli_fetch_assoc($results))
{ $options[] = array ('label' => $row['SUP_AccountNo'],'value' => $row['SUP_AccountNo'] ); }
wp_reset_postdata();
}
return $options;
mysqli_close($con);
?>
我知道它与$ supplier变量有关,但是我无法找到或确定正确的语法来检索用户输入的供应商名称。
一如既往的任何帮助将不胜感激。