开发者

How to use a Javascript variable in a PHP block of code?

开发者 https://www.devze.com 2023-04-02 05:04 出处:网络
I am trying to append some digits to the class names of the elements I am trying to dynamically add on a page. The digit that I will append is by getting the number of the existing target element on t

I am trying to append some digits to the class names of the elements I am trying to dynamically add on a page. The digit that I will append is by getting the number of the existing target element on the page and I am getting it through the jquery function length.

Here is my code:

$("#add_ingr").click(function(){

                var id = $('.save').length + 1;

                var row = '<tr>'
                            +'<td>' 
                                +'<?php $data = array('name' => 'ingr_name[]', 'class' => 'ingr_name'); echo form_input($data); ?>'
                            +'</td>'
                            +'<td>'  
                                +开发者_如何学C'<?php $data = array('name' => 'ingr_amount[]', 'class' => 'amt'); echo form_input($data); ?>'
                            +'</td>'
                            +'<td>'
                                +'<?php $data = array('name' => 'ingr_unit[]', 'class' => 'unit'); echo form_input($data); ?>'
                                +'<span class="remove">X</span>'
                                +'<span class="save" id="' +count +'">Save</span>' 
                            +'</td>';                                                               
                         +'</tr>';

                $("#ingr_table > tbody").append(row);

            });

I'm having troubles because the var id is of javascript and I wanted to append it in a php array value but I am not successfully running the code because it generates errors when I do something like this:

$("#add_ingr").click(function(){

                var id = $('.save').length + 1;

                var row = '<tr>'
                            +'<td>' 
                                +'<?php $data = array('name' => 'ingr_name[]', 'class' => 'ingr_name_' +id); echo form_input($data); ?>'
                            +'</td>'
                            +'<td>'  
                                +'<?php $data = array('name' => 'ingr_amount[]', 'class' => 'amt_') +id; echo form_input($data); ?>'
                            +'</td>'
                            +'<td>'
                                +'<?php $data = array('name' => 'ingr_unit[]', 'class' => 'unit_' +id); echo form_input($data); ?>'
                                +'<span class="remove">X</span>'
                                +'<span class="save" id="' +count +'">Save</span>' 
                            +'</td>';                                                               
                         +'</tr>';

                $("#ingr_table > tbody").append(row);

            });

Does anybody have a solution for my problem? Thank you very much for those who would help.


Do you really need CodeIgniter's form_input helper?

function form_input($data = '', $value = '', $extra = '')
{
  $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);

  return "<input "._parse_form_attributes($data, $defaults).$extra." />";
}

As this will just return <input name=$name class=$class/>,
I guess you can convert it to this.

var row = '<tr>'
            +'<td>' 
                +'<input name="ingr_name[]" class="ingr_name_'+id+'"/>'
            +'</td>'
            +'<td>'
                +'<input name="ingr_amount[]" class="amt_'+id+'"/>'
            +'</td>'
            +'<td>'
                +'<input name="ingr_unit[]" class="unit_'+id+'"/>'
                +'<span class="remove">X</span>'
                +'<span class="save" id="' +count +'">Save</span>' 
            +'</td>';                                                               
         +'</tr>';


The unterminated string literal error is probably because of the ; after

+'</td>';    

The code is pretty difficult to read because you're mixing PHP and JavaScript. I would recommend moving all the PHP into one block, and then outputting only the PHP variables: <?php echo $data; ?> for clarity.

0

精彩评论

暂无评论...
验证码 换一张
取 消