使用 MySQL 查询生成的下拉列表中的选定值不会传递给变量 [英] Selected value in dropdown list generated using MySQL query does not pass to variable

查看:32
本文介绍了使用 MySQL 查询生成的下拉列表中的选定值不会传递给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Wordpress 页面上有一个下拉列表,它使用 MySQL 查询将值从 MySQL 填充到下拉列表

I have a dropdown on my Wordpress page which uses a MySQL query to populate the values from MySQL to the dropdown list

这是我的 Wordpress 页面上的代码

This is the code on my Wordpress page

<form method="POST" action="">
[wpse_233034_shortcode]
<br>
<input type="submit" name="submit">
[wpse_233036_shortcode]
</form>

用于生成动态MySQL查询的代码是我在functions.php文件中定义的[wpse_233034_shortcode]

The code used to generate the dynamic MySQL query is [wpse_233034_shortcode] which I have defined in the functions.php file

add_shortcode('wpse_233034_shortcode', function(){
global $wpdb;
$results1 = $wpdb->get_results ("SELECT `Compound` FROM PNaphtha ORDER BY 
`SrNo`");
echo '<td><select id="Compound" name="Compound">';
echo '<option value="">Select Compound</option>';
foreach ( $results1 as $result1 ) {
echo '<option>'.$result1->Compound.'</option>';
}
echo '</select></td>';
});

到目前为止,它似乎工作正常.当我下拉列表时,化合物的值是从 PNaphtha 表中选择的并填充在屏幕上.

It seems to be working fine until this point. When I dropdown the list, the value for the Compounds is selected from the PNaphtha table and populated on the screen.

接下来我想要的是将此下拉列表中的选定值传递到另一个短代码/MySQL 查询中,该查询将获取更多与复合关联的数据

What I want next is to pass the selected value from this dropdown into another shortcode/MySQL query which will fetch some more data associated with the Compound

这是我到目前为止所拥有的,但是它似乎无法将上述下拉列表中的值拉"到下一个 MySQL 查询中

This is what I have so far, however it does not seem to be be able to "pull" the value from the above dropdown into the next MySQL query

add_shortcode('wpse_233036_shortcode', function(){
global $wpdb;
$Compound = filter_input( INPUT_POST, 'Compound' );
$Compound = $Compound ? $Compound : 'acetone';
$query = $wpdb->prepare( "SELECT * FROM PNaphtha WHERE `Compound` = %s", 
$Compound );
$myrows1 = $wpdb->get_results( $query, ARRAY_A );
ob_start();
foreach ( $myrows1 as $row1) {
echo "Compound: ".$row1['Compound'].", "."Formula: ".$row1['Formula'].", 
"."Molecular Weight: ".$row1['MW']."<br>";
}
return ob_get_clean();
});

现在,短代码wpse_233036_shortcode"似乎有效",因为指定丙酮"的默认/静态值总是在页面上被查询和回显,甚至在我选择下拉列表并从下拉列表中选择任何值之前.这是我在页面加载时得到的

Now, the shortcode 'wpse_233036_shortcode' seems to be "working", as the default/static value specified 'acetone' is always queried and echoed on the page, even before I take a dropdown and select any value from the dropdown. This is what I get on page load

Compound: acetone, Formula: C3H6O, Molecular Weight: 58.08

我想要的是使用第一个 MySQL 查询填充下拉列表,就像现在一样

What I want is for the dropdown to be populated using the first MySQL query as it does now

"SELECT `Compound` FROM PNaphtha ORDER BY `SrNo`"

接下来,当我从此下拉列表中选择一个值时,所选值应传递给

Next, when I select a value from this dropdown, instead of the default 'acetone' the selected value should pass to

"SELECT * FROM PNaphtha WHERE `Compound` = %s", $Compound

根据 Sally 的回答,我能够使用静态/非 SQL 查询下拉列表使其工作,如下所示 这里

I was able to get this to work using static/non-SQL queried dropdown list as shown below based on Sally's answer here

<form method="POST" action="">
<select name="C_Option">
<option value=""></option>
<option value="abietic acid">abietic acid</option>
<option value="acenaphthene">acenaphthene</option>
...
<option value="acetone">acetone</option>
</select>
<input type="Submit">
[wpse_233032_shortcode]
</form> 

推荐答案

问题是因为您的短代码回调/函数(即 wpse_233034_shortcode())没有返回任何输出,虽然它echo 预期的输出.

The problem is because your shortcode callback/function (i.e. wpse_233034_shortcode()) is not returning any output, although it does echo the expected output.

或者更准确地说,wpse_233034_shortcode() 函数中的 select 字段在输出表单 HTML 之前 输出到浏览器.这意味着表单没有没有 select 字段,因此当您提交表单时,键 Compound 在超全局 $_POST.

Or more precisely, the select field in your wpse_233034_shortcode() function is outputted to the browser before the form HTML is outputted. Which means the form does not have the select field, hence when you submit the form, the key Compound is not available in the superglobal $_POST.

因此请确保您的简码返回正确的输出,但如果您需要在简码回调中echo它,那么请使用输出缓冲,就像您在 wpse_233036_shortcode() 函数.

So make sure your shortcode is returning the proper output, but if you need to echo it in the shortcode callback, then use output buffering just as you did in the wpse_233036_shortcode() function.

add_shortcode('wpse_233034_shortcode', function(){
    global $wpdb;
    $results1 = $wpdb->get_results ("SELECT `Compound` FROM PNaphtha ORDER BY `SrNo`");

    ob_start(); // turn on output buffering
    echo '<td><select id="Compound" name="Compound">';
    echo '<option value="">Select Compound</option>';
    foreach ( $results1 as $result1 ) {
        echo '<option>'.$result1->Compound.'</option>';
    }
    echo '</select></td>';

    return ob_get_clean(); // return the output
});

但在可能的情况下,避免使用输出缓冲,而是将输出分配给一个变量,如下所示:

But where possible, avoid using output buffering and instead, assign the output to a variable, like so:

add_shortcode('wpse_233034_shortcode', function(){
    global $wpdb;
    $results1 = $wpdb->get_results ("SELECT `Compound` FROM PNaphtha ORDER BY `SrNo`");

    $output = ''; // save the output to this variable
    $output .= '<td><select id="Compound" name="Compound">';
    $output .= '<option value="">Select Compound</option>';
    foreach ( $results1 as $result1 ) {
        $output .= '<option>'.$result1->Compound.'</option>';
    }
    $output .= '</select></td>';

    return $output; // return the output
});

这篇关于使用 MySQL 查询生成的下拉列表中的选定值不会传递给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆