duandingcu7010 2014-08-04 01:05
浏览 180
已采纳

在Js中插入PHP变量(JQuery)

I wanna start by saying that I have absolutely NO knowledge in JS ( which I think I'm gonna learn because I'm over-heating like never before ).

I'm making a website for a friend , and I wanted to add a little more stylish that just plain CSS3 & HTML, So I went on Google and did a little research and I downloaded a free usable jQuery plugin called "Simple Modal".

I want to display a PHP variables inside that Modal. Here's a screenshot of it:

enter image description here

And here is the minimum required to make the plugin work properly on any page:

> <link rel="stylesheet" href="simplemodal.css" type="text/css"
> media="screen" title="no title" charset="utf-8">
>     <script src="assets/javascript/mootools-more-1.3.1.1.js" type="text/javascript charset="utf-8"></script>
>     <script src="/js/simple-modal.js" type="text/javascript" charset="utf-8"></script>
>     <script src="assets/javascript/demo.js" type="text/javascript" charset="utf-8"> </script>

Now here comes the problem. The content of the Modal is in -> simple-modal.js in the following form:

  $("modal").addEvent("click", function(e){
        e.stop();
        var SM = new SimpleModal({"btn_ok":"Confirm button"});
            // Confirm Button
            SM.addButton("Confirm", "btn primary", function(){
                alert("Action confirm modal");
                this.hide();
            });
            // Cancel button
            SM.addButton("Cancel", "btn");
            SM.show({
              "model":"modal",
              "title":"Modal Window Title",
              "contents":"<p >Hey , how you doing? Nice weather today!</p>"
            });
      })

In order to edit the content that is displayed , i need to edit the "contents". I tried to put my PHP variable in it .. It didn't worked .. Googled it... And I found a solution. Which is putting a JS variable and then, PHP one on the main page.

 <?php $content = $user['username']; ?> 
  <script type="text/javascript"> var name = "<?= $content ?>"; </script>

So after my new found , I tried to do this:

"contents":"<p>Hey" + name + ", how you doing? Nice weather today!</p>"

Didn't work. I then, tried (To see , maybe the variable was the problem ):

"contents": name

And this time it worked. It seems , for an unknown reason to me, that I can't 'combine' 2 things(? if that's a proper way to name them all).

I even tried things such as "Good" + "Morning" but I got the same result.

I'm very confused on why is that.

So, if anybody could be of any help by explaining me why this is happening, how to fix it or at least give a precise path for researches, I would be very graceful.

Thank you.

  • 写回答

1条回答 默认 最新

  • doumei1772 2014-08-04 01:28
    关注
    <?php $content = $user['username']; ?> 
    <script type="text/javascript"> 
        var name = "<?= $content ?>"; 
        var fullcontent = "<p>Hey" + name + ", how you doing? Nice weather today!</p>";
    </script>
    

    and here it will be

    "contents":fullcontent
    

    Where as for the modal you can try EXTJS its quite efficient and easy to use.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误