-1

I have a jquery function include html select and input, it shows form with selectable value name is olcu

<script type="text/javascript">
$(document).ready(function() {
    $("#add").click(function() {
        var intId = $("#buildyourform div").length + 1;
        var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
        var fMiktar = $("<input type=\"text\" class=\"miktar\" name=\"miktar[]\" size=\"6px\" />");
        var fOlcu = $("<select class=\"olcu\" name=\"olcu[]\"><option value=\"Adet\">Adet</option><option value=\"Metre\">Metre</option><option value=\"Gram\">Gram</option></select>");
        var fMalzeme = $("<input type=\"text\" name=\"malzeme[]\" class=\"malzeme\" id=\"aramayap\" />");
        var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");
        removeButton.click(function() {
            $(this).parent().remove();
        });
        fieldWrapper.append(fMiktar);
        fieldWrapper.append(fOlcu);
        fieldWrapper.append(fMalzeme);
        fieldWrapper.append(removeButton);
        $("#buildyourform").append(fieldWrapper);
    });

});
</script>

I want to take values of olcu from database

And here is the database code

<script type="text/javascript">
    function showSelected(val){
        document.getElementById ('selectedResult').innerHTML = "" + val;
    }

</script>

<select name="olcu[]" onChange='showSelected(this.value)' class="olcu" id="olcu">Olcu Sec/option>

<?php

while($olculer=mysql_fetch_array($hangiolcu)){
echo "<option value='$olculer[olcu]'>$olculer[olcu]</option>";
}
?>
</select>

How can I append or replace that sql code into this jquery line

var fOlcu = $("<select class=\"olcu\" name=\"olcu[]\"><option value=\"Adet\">Adet</option><option value=\"Metre\">Metre</option><option value=\"Gram\">Gram</option></select>");
5
  • 1
    Why don't you use better jQuery selector for elements? by giving class name or ID ? Commented May 19, 2014 at 7:06
  • 3
    this looks strange (mix od javascript and php code), why dont you get data with ajax? Commented May 19, 2014 at 7:08
  • 1
    AJAX is the way to go here. Commented May 19, 2014 at 7:14
  • 1
    Your Database Code is not database code. It simply prints options from your database query. What you actually want to achieve is very unclear. What is that jQuery line supposed to do? I think you might have a XY Problem. Commented May 19, 2014 at 7:14
  • can you please add combined code? Commented May 19, 2014 at 7:23

1 Answer 1

2

You can create Javascript/jQuery variable with PHP code like this:

var foo = '<?php //echo or return php value here; ?>';
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please combine code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.