在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.

查看全部
duandingcu7010
duandingcu7010
2014/08/04 01:05
  • variables
  • php
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复