0

I can append html form elements to a table with jquery. How can I append menus populated with php/my sql query so that the php menu is between td and input... below??

<script type="text/javascript">
$(document).ready(function() {
      $("#link td input.rdelete").live("click", function () {
              $(".newrow").attr("disabled",false);
            var srow = $(this).parent().parent();
                        srow.remove();                  
            });
});


function AddNew() { 
           if($("#link tr").length > 12) {
              $(".newrow").attr("disabled",true);
           }
           var count = 0;
           count += 1;

        $("#link").append('<tr><td></td><td> <input type=\"button\" value = \"Delete\" class=\"rdelete\"/></td></tr>' );
           }
</script>

The php menu is:

<select name="sub_discipline" id="sub_discipline">
            <option>Select Sub-discipline...</option>
            <?php 
$query = mysql_query("SELECT * from sub_discipline ORDER BY sub_discipline_name ASC");
while ($row = mysql_fetch_assoc($query)){
?>
            <option value="<?php echo $row['sub_discipline_pk']; ?>"><?php echo $row['sub_discipline_name']; ?></option>
            <?php 
}
?>
          </select>

EDIT: Thanks to Explosion Pills I now have:

<script type="text/javascript">
$(document).ready(function() {
$("#jMenu").jMenu();
      $("#link td input.rdelete").live("click", function () {
              $(".newrow").attr("disabled",false);
            var srow = $(this).parent().parent();
                        srow.remove();                  
            });
});


    function AddNew() { 
               if($("#link tr").length > 12) {
                  $(".newrow").attr("disabled",true);
               }

    $.getJSON('../scripts/link_menu.php', function (json) {
       $("#links").empty();
       $.each(json, function () {
           $("#links").append(new Option(this.link_title));
       });
    }); 

            $("#link").append('<tr><td></td><td><select name="links[]" id="links"> </select><input type=\"button\" value = \"Delete\" class=\"rdelete\"/></td></tr>' );

    </script>

Problem is that only the first appended menu is being populated.

2
  • 1
    Generally, you should add an api in your php app, then using XHR in javascript to get this menu and showing them. Commented Jan 31, 2013 at 4:07
  • Thanks, do you know of an example? Commented Jan 31, 2013 at 4:09

1 Answer 1

1

You need to disabuse yourself of the fact that PHP and JavaScript operate together. They are agnostic with respect to each other (i.e. they don't know about each others' existence, but they're not willing to make an absolute claim that the other doesn't exist).

You can create a simple PHP script that echoes the MySQL results as JSON.

$query = mysql_query("SELECT * from sub_discipline ORDER BY sub_discipline_name ASC");
$rows = array();
while ($row = mysql_fetch_assoc($query)){ $rows[] = $row; }
echo json_encode($rows);

Then, in your JavaScript:

$.getJSON('/agnostic-php-script.php', function (json) {
   $("#sub_discipline").empty();
   $.each(json, function () {
       $("#sub_discipline").append(new Option(this.subdiscipline_name));
   });
});

You should also stop using ext/mysql, and it's better to not append in a loop if you can avoid it.

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

5 Comments

Hey thanks for this. This is now working but only for the first appended menu, subsequent menus appended are empty. Any ideas? See added code in original question (didn't know how else I could add this new block of code here...)
@user1911619 well, IDs have to be unique. Do you have more than one #sub_discipline?
I have change it to look for the class, rather than ID (I want users to add as many instances of the same menu as they need...) so, $.each(json, function () { $(".links").append(new Option(this.link_title)), however with each new instance, the preceding menus grow by one option, presumably through the $.each...
You can't use $(".links"), you have to use whatever the specific <select> element in question is and append to that (unless you actually do want to append to all of them)
Come to think of it, this may work better with just one multiple select list...rather than multiple instances of the same menu, in that case you answer fits perfectly!

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.