English 中文(简体)
以 javascrip 格式装入 php 变量
原标题:Load php variable in javascript

内装新

如何以 javascrip 装入可变php?

这是我的脚本 (pop.php)

<?php $id_user = $this->session->userdata( id ); ?>

<script type="text/javascript">rules: {
        user: {
  required: true,
  number: true,
  range: [1, 8]
}  <script>

示例变量。

$id_user = 10;

如何将 id_user 输入到 范围 : [1,8]

所以区域输出为 范围 : [1,10]

为我的坏英语感到哀痛

非常感谢你

钢架

普亚

最佳回答

试试

<script type="text/javascript">

var $uid = <?php echo json_encode($this->session->userdata( id )); ?>;
rules: {
  user: {
  required: true,
  number: true,
  range: [1, $uid]
} 
 <script>

为什么 json_encode? 它会将您传递给它的价值转换成有效的 JSON - 引用字符串、 跳出引号和其他特殊字符等 。 没有它, 您基本上会在您回响的变量包含一个简单的字母数字符串以外的东西时断开 。

问题回答

您可以在 php 中使用 echo 在 php 中创建 javascript 变量。 我个人会处理这种情况 :

<?php echo "<script>var user_id = ".$this->session->userdata( id ).";</script>"; ?>
<script>
  rules: {
    user: {
    required: true,
    number: true,
    range: [1, user_id]
   }
  }  
</script>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签