0

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

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>

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

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.

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

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

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

enter image description here

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

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> 
Quark
  • 49
  • 1
  • 14
  • What is `$Compound` between these two statements and after??? `$Compound = filter_input( INPUT_POST, 'Compound' ); $Compound = $Compound ? $Compound : 'acetone';` – AbraCadaver Jan 29 '19 at 19:54
  • $Compound = filter_input( INPUT_POST, 'Compound' ); Compound is also the – Quark Jan 29 '19 at 19:57
  • I was stating that you should `echo` it and verify what it actually is at different spots. – AbraCadaver Jan 29 '19 at 20:42
  • Thank you AbraCadaver, it returns a blank after the first and defaults to acetone after the second. If I change the default to something other than acetone, it queries the database for that compound and shows the formula and molecular weight. The problem seems to be with $Compound not getting the value from the dropdown. – Quark Jan 29 '19 at 20:50
  • On the first page load it will be blank and default to acetone. You say it does this even after you submit the form? – AbraCadaver Jan 29 '19 at 20:56
  • It echoes the acetone formula and molecular weight on page load. When I select a compound from the dropdown it submits the form but does not update the acetone to the option I selected in the dropdown. Acetone and its properties continue to stay there on page load and on form submit. However, if I change the default acetone to say acenaphthene in functions.php, I start getting the properties associated with acenaphthene, on page load and submit. – Quark Jan 29 '19 at 21:01
  • @Quark, sorry about my previous comment (which I've removed) - I didn't notice the `select` name is `Compound`. Anyway, check my answer. – Sally CJ Jan 30 '19 at 08:34

1 Answers1

1

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.

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.

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
});
Sally CJ
  • 15,362
  • 2
  • 16
  • 34
  • Thank you very much Sally. I really appreciate your help. I will review your suggestion in a few hours and let you know in case I need more help. – Quark Jan 30 '19 at 10:16
  • Brilliant. Both the solutions work perfectly, I also read up about output buffering. – Quark Jan 30 '19 at 20:38
  • As always, I'm glad I could help! :) – Sally CJ Jan 31 '19 at 12:40
  • Hi @Sally CJ. I hope all is well. I am sorry to bother you again, I had a PHP question similar to this question, I was hoping if you could please take a look at it [here](https://stackoverflow.com/questions/55542349/echo-google-visualization-table-code-using-php) and advise. – Quark Apr 06 '19 at 12:10
  • Hi @Sally CJ. I was able to figure it out, I was missing a "," in my php code. I really appreciate all your help so far. Have a nice day! – Quark Apr 06 '19 at 22:25
  • 1
    Hi Quark. I'm glad you got it sorted, and you too have a nice day! :) – Sally CJ Apr 08 '19 at 01:29