0

I'm still new with yii and now I want to add a Javascript code that can append dropdown list to the table, when I click the button.

Here is my table:

  <table class="table table-hover" id="table-add-more">
    <tr>
        <th style="width:50px;">No</th>
        <th style="width:200px;">Name</th>
        <th style="width:50px;">Count</th>
        <th style="width:30px;"></th>
    </tr>
    <tbody>
    </tbody>
  </table>

Here is my button:

  <a id="btn-add-more" class="btn btn-sm btn-success">Add More</a>  

Here is my script:

<script>
  $(document).ready(function(){
    var number = 1;
    var product = <?php $product = Product::model(); echo $form->dropDownList($product,'ID', CHtml::listData(Product::model()->findAll(), 'ID', 'name')); ?>;
        $('#btn-add-more').click(function() {
            var more_field  = "<tr>"+
                             "<td>"+number+"</td>"+
                             "<td>"+                                
                                <?php echo "product"; ?>
                             +"</td>"+
                             "<td><input type='number'/></td>"+
                             "<td><input type='checkbox'/></td>"+
                             "</tr>"
            $('#table-add-more').append(more_field);
            number = number + 1;    
        });
  });
  </script>

I got an error message:

"unexpected token <"

near the var product = <select name="Product[ID]" id="Product_ID"> but I can't found the mistake yet. Please help..

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    are you generating your js script from php or it's just a js file? If it is js file you can do this this way, beacuse php is working on server side and js on client side, and your php code in js is invalid (browser won't iterpretate it as php but as js) and thats why you got error – szapio Feb 13 '15 at 13:31
  • i used javascript file which has been connected with yii. – felicia limantara Feb 13 '15 at 13:48
  • so you probably just include your js file, so this won't work because you are sending php code inside javascript code to client browser and you expect that client browser will execute php code, it won't because browser do not have rest of your php code and do not have php interpreter, it's server job. – szapio Feb 13 '15 at 13:55
  • hmm if that so. it's impossible to add dropdown list with javascript? – felicia limantara Feb 13 '15 at 13:57
  • it's possible but not that way, i just trying to say that you can't mix your code this way, read this topic http://stackoverflow.com/questions/6369313/difference-between-javascript-and-php First you have to understand difference between js and php and how it works and then you can start programing, And answering your question, you can do it many ways, you can e.g store dropdown list in js in some variables/templates or you can also make an ajax call to php and ask for dropdown list and then on success append the result where you want – szapio Feb 13 '15 at 13:59
  • I've read that page.. but i can't understand that.. i used that code without using yii framework. and it's work. so i confused why it doesn't work on yii – felicia limantara Feb 13 '15 at 14:29
  • $('#btn-add-more').click(function() { var more_field = ""+ ""+ ""+ ""+ ""+ ""+ "" $('#table-add-more').append(more_field); number = number + 1; }); – felicia limantara Feb 13 '15 at 14:34
  • beacuse it's js file! php does not intepret it, its a browser job to do this, you can write php inside js, you can generate js using php. You have to read more and understand. For now you just say to browser: "Hey i'v got some js under this source 'yourPathToJsFile', do what is inside", and browser looks on this script and see php markup and browser says, "Sorry i dont understant" – szapio Feb 13 '15 at 15:06

1 Answers1

0

I think the better way is to have dropdown list with display: none at initialization and when you click that button, you can change display to block. But If you want to append dropdown to your table, I think you have tow problem in your javascript function:

1.You need to wrap this php code into "".

var product = "<?php $product = Product::model(); echo $form->dropDownList($product,'ID', CHtml::listData(Product::model()->findAll(), 'ID', 'name')); ?>";
  1. I think you need to have <?php echo $product; ?>, instead of <?php echo "product"; ?>
hamed
  • 7,939
  • 15
  • 60
  • 114