-2

I have javascript code like this:

<script>
$("#ancbahan_add").click(function(){
 $('#tblbahan tr').last().after('');
});
</script>

And i want to put this php code:

<?php

echo "<select name='anggota'>";

$show=mysql_query("SELECT * FROM tb_satuan ORDER BY id_satuan");
echo "<option value='belum milih' selected>-satuan-</option>";

while($w=mysql_fetch_array($show))
{
    echo "<option value=$w[satuan]>$w[satuan]</option>";        
}
    echo "</select>";

?>

inside .after (''), i completely had no idea how to do it, since i'm new in using javascript.

So i hope someone can give me some solution, Thanks.

Ujang Hardman
  • 105
  • 1
  • 4
  • 19
  • 2
    Use [Ajax](http://en.wikipedia.org/wiki/Ajax_(programming)). – Ram Sep 29 '14 at 01:47
  • 1
    possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – michael.schuett Sep 29 '14 at 01:51
  • 1
    You *don't* necessarily need "AJAX" or anything fancy to access "data from PHP". [Here is an answer I wrote, which may be applicable](http://stackoverflow.com/questions/25281758/need-tricky-idea-to-use-js-variable-in-php/25281931#25281931). The big thing to note is that PHP executes on the server and it returns HTML. This HTML is parsed *after* the PHP executes and any JavaScript is also run *later*. However, the PHP (if it does not actually *require* a value from the JavaScript) can trivially emit JavaScript/JSON (or HTML) to be manipulated and/or consumed during the initial page fetch. – user2864740 Sep 29 '14 at 01:53

4 Answers4

3

If this content does not change, I would recommend you render the element on the page after the last tr, but just make sure the <select> element has the following attribute: style="display: none;". Then you can just use the following javascript to show it:

$("#ancbahan_add").click(function(){
  $('#tblbahan tr').last().show();
});

I should say that it looks like you are going to have HTML that looks like this:

<table>
 <tr></tr>
 <select></select>
</table>

This HTML is invalid as the select should be inside a table cell.

Scopey
  • 6,269
  • 1
  • 22
  • 34
  • well, i forgot to say this, but my javascript function is to add new table row dynamically, so `$('#tblbahan tr').last().after('');` function is to add new tr inside table, and the combobox is inside that tr, so when i click add, i want it to add new row plus the combobox.. – Ujang Hardman Sep 29 '14 at 02:04
  • Great! I would still recommend doing it like this unless the content changes. Just make sure the `tr` has the `display: none;` style. – Scopey Sep 29 '14 at 02:07
2

To make it cleaner I'd do something like this:

<?php
$select = "<select name='anggota'>";
$show=mysql_query("SELECT * FROM tb_satuan ORDER BY id_satuan");
echo "<option value='belum milih' selected>-satuan-</option>";
while($w=mysql_fetch_array($show))
{
  $select .= "<option value=$w[satuan]>$w[satuan]</option>";
}
$select .= "</select>";
?>

<script>
$("#ancbahan_add").click(function(){
  $('#tblbahan tr').last().after('<?php echo $select; ?>');
});
</script>

EDIT: to add a row like you requested:

<script>
$("#ancbahan_add").click(function(){
  $('#tblbahan').append('<tr><?php echo $select; ?></tr>');
});
</script>
sitesbyjoe
  • 1,861
  • 15
  • 21
  • my javascript function is to add new table row dynamically, so $('#tblbahan tr').last().after(''); function is to add new tr inside table, and the combobox is inside that tr, so when i click add, i want it to add new row and the combobox(inside that new row). when i tried your code, my add function didn't working. -- – Ujang Hardman Sep 29 '14 at 02:11
  • So just add a ''...'' around the select. Or do an append(). – sitesbyjoe Sep 29 '14 at 05:34
  • Well, i had no trouble with my add new row function, be it with append or last().after(). But when i add your code to my code (with last.after or with append), it make the add new row function didn't working.. – Ujang Hardman Sep 29 '14 at 07:05
0

Just go ahead and paste the whole PHP snippet (including opening and closing <?php ?> tags) between the quotes ' '. The PHP will run on the server and the correct values will appear in the javascript.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
  • i've tried it, something like this : `$('#tblbahan tr').last().after('";$tampil=mysql_query("SELECT * FROM tb_satuan ORDER BY id_satuan");echo "";while($w=mysql_fetch_array($tampil)){echo "";} echo "";?>');` , but what happened is my javascript completely didn't working, sorry my bad language. – Ujang Hardman Sep 29 '14 at 01:53
  • Firstly, use the cleaner answer from @sitesbyjoe. Check the source code in the browser, and if the correct text appears in your javascript call then there's a problem with your javascript. To fix that we need to see a bit more of what your javascript should result in. – worldofjr Sep 29 '14 at 02:02
  • well, my javascript code function is to add new row in my table, so when i click add, it will add new tr inside the table, when i tried @sitesbyjoe code, my add function didn't working (didn't add new row or do anything), -_- – Ujang Hardman Sep 29 '14 at 02:18
0
$('#tblbahan tr').last().after(function(){
    $.ajax({
      url: "test.php"//php page url
    }).done(function(data) {
      console.log(data);
    });
  }
}
)