how to access PHP variables from within JavaScript?

前端 未结 3 944
北恋
北恋 2020-12-12 06:31

I want to access PHP(server side file variables) with JavaScript(Client side script) without using MySQL. e.g. i have $name=Tom; How do i access this $name variable in Ja

3条回答
  •  春和景丽
    2020-12-12 07:28

    You could do something like

    
    

    which should even let you do arrays and possibly objects. It requires PHP 5.2 or later, though. If you're stuck without json_encode, you could wrap quotes around a call to addslashes, but that won't let you do arrays and such.

    If your intent is to set the value within some form, you can do like

    
    

    and of course, you could access that element's value within your script if necessary.

    Two key points to take away here:

    • Since PHP is generating the page, it can output stuff as it pleases -- even right in the middle of a

提交回复
热议问题