1

I've echoed the following php and it shows up properly in HTML so that can't be a problem:

PHP

if (mysqli_num_rows($result) > 0) {

//create the drop down menu
 $list ="";
 while($row = mysqli_fetch_assoc($result)) {
     $list = '<div class="dropOption">'.$row["item"].'</div>';
 }

This outputs three rows - apple, pear, strawberry - in the right div format.

And when I put the following php script into the jquery function below, the menu does contain the value strawberry (the last), however the first two are missing.

JavaScript

//drop down menu
$(document).ready(function(){
function createDropdown(){
    var drop = $('#customDropdown');
    var i;
    var htmlString = '<div id="dropContainer">';

    htmlString += '<?php echo $list;?>';

    htmlString += '</div>';
    drop.append(htmlString);
    }

   createDropdown();

I'm new to jquery and php so forgive me if the error is simple; however I'm pretty sure it's right, functionally speaking, because I'm getting something; so I figured the syntax must get be wrong somewhere. Can anybody help? Thanks in advance.

0

2 Answers 2

3

In your php it should be

$list .= '<div class="dropOption">'.$row["item"].'</div>';

and then output just like this <?=$list;?> in your js part

. is used to concatenate strings in php

Sign up to request clarification or add additional context in comments.

3 Comments

like this you mean - $list = '<div class="dropOption">".$row["item"]."</div>'; if so, it didn't seem to work
I mean leave it as it was in your JS part and add '.' in your php part as I wrote in my answer.
Please note, the echo part is still needed in the output, before the variable list.
1

You're only assigning the last item to the $list variable. You need to concatenate them all together.

Try:

if (mysqli_num_rows($result) > 0) {
    // Create the drop down menu
    $list = "";
    while($row = mysqli_fetch_assoc($result)) {
        $list .= '<div class="dropOption">' . $row["item"] . '</div>';
    }
}

Note the $list .= instead of $list = part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.