1

so i want try pass variable php to function javascipt where variables is

<?php $code1=""; ?>

and for function is

<script type="text/javascript">
        var $code1 = $(this);
            $txt=new Array();
            var $code1 = <?php echo $code1("code1"); ?>;
            $(function(){
                $('#go').on('click',function(){
                    console.log($('form').serialize());
                })

                $('body').on('keydown','.last',function(){
                    $('.last').removeClass('last');
                    $('#go','body').before(
                    '<table><tr><td><input class="last" type="text" id="code1" name="code'+(Number($(this).attr('name').match(/[0-9]+/g))+1)+'" value=" SHOULD BE HERE "></td></tr></table>');          
                })
            })
        </script>

should be value is ="$code1" so i try value= "<?php echo $code1; ?>" but it's cant can help me pls?

3
  • php executes on the server, javascript executes on the client. you cannot have JS set/modify a php variable. Commented Oct 8, 2014 at 19:46
  • @MarcB I think they want it the other way around, to use a php variable in JavaScript, which is just fine Commented Oct 8, 2014 at 19:48
  • Thx for comment, yes i want use php variable in javascript function, not must variable php, i just dont know how to fill in the value = "$code1" where $code1 is variable php. sry newbie for javascript programmers Commented Oct 8, 2014 at 20:04

1 Answer 1

3

json_encode() is what you want, but all the quotes and what not are handled for you by the JSON-encoding. Don't add more quotes. Also, it's easiest if in your JavaScript, you assign stuff to their own variables.

var data = <?php echo json_encode($data); ?>;

Also, I recommend that you don't simply concatenate data into HTML like this. Otherwise, you're lacking escaping for HTML which can cause ambiguity and injection problems.

$('<input>').val(data);
5
  • thx for comment i already try var code1 = <?php echo json_encode($code1); ?>;, but what if used in a javascript function? how writing inside if value = "" ? Commented Oct 8, 2014 at 19:51
  • Can you re-write, to type text and blank values​​, sorry my starter javascript so i write $('#code1').attr("",YourValue) what is in YourValue? Commented Oct 8, 2014 at 20:08
  • @Vilthering It's best to not just embed it directly into a JavaScript function... you're making hard-to-read code. But if you did, nothing changes. $('#code1').val(<?php echo json_encode($val); ?>);. No need for those extra quotes. Commented Oct 8, 2014 at 20:16
  • I done try var data = <?php echo json_encode($data); ?>; and $('#code1').val(<?php echo json_encode($val); ?>); but tht function is failed, no working. Commented Oct 9, 2014 at 1:39
  • @Vilthering It does work. Without knowing the context, I won't be able to help you. You should post a new question with the specific code you've tried, along with the specific error you're getting. Commented Oct 9, 2014 at 1:41

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.