Add php variable to javascript

Possible Duplicate:
passing variables from php to javascript

I'm dynamically generating a list. I want to make each row hover on mouseover and clickable to a link. I want the link to pass the id of the content of the row.

Basically:

foreach ($courses as $cid=>cinfo){ 
  $univ = $cinfo['univ'];
  $desc = $cinfo['desc'];
  $size = $cinfo['size'];
  $start = $cinfo['start'];
  print "
"."$desc
"."$univ
". "Number of students: $size
". "Started at: ".date('F d, Y',strtotime($start))."
". }

The issue is in the js/jquery. I want to be able to grab the $cid and pass it to the student.php page upon click. The php code above works but the js won't of course. I know the fundamental of client-side vs server-side languages. This question doesn't warrant a lecture. I know I cannot do this exactly, but it is what I want to happen ultimately. Any thoughts on how I can achieve this simply? Thanks in advance my friends!

asked Jul 13, 2012 at 16:38

1

Yes, if you include the

In your case, you would be looking into the following code structure:


The reason why this is possible is because although the Javascript is run on the client-side, it's processed on the server-side first prior to being presented on the page. Thus, it'll replace all necessary instances of $cid with the one you have included.

Enjoy and good luck!

EDIT:


answered Jul 13, 2012 at 16:40

Add php variable to javascript

Daniel LiDaniel Li

14.6k6 gold badges42 silver badges59 bronze badges

2

You can do it like this :

    $(location).attr('href','');

The javascript code doesn't know it comes from php, it appears as a constant (a literal) for it.

answered Jul 13, 2012 at 16:40

Denys SéguretDenys Séguret

361k84 gold badges763 silver badges733 bronze badges

0

Yes it is possible. All you have to do is put your in where you need it


brettererbretterer

5,6395 gold badges31 silver badges53 bronze badges

0

There is almost no way to actually communicate PHP and JavaScript. However, the best and simplest way is to set the ID within an attribute. The new HTML5 data attributes would be perfect.

For example, have a anchor tag with

some event

and then:

$(this).attr('href','student.php?'+$(this).attr('data-id'));

Or just use


if it is not external JS file

answered Jul 13, 2012 at 16:42

CrazyVipaCrazyVipa

9177 silver badges10 bronze badges

0

Try this code

foreach ($courses as $cid=>cinfo){ 
  $univ = $cinfo['univ'];
  $desc = $cinfo['desc'];
  $size = $cinfo['size'];
  $start = $cinfo['start'];
  print "
"."$desc
"."$univ
". "Number of students: $size
". "Started at: ".date('F d, Y',strtotime($start))."
". }

answered Jul 13, 2012 at 16:50

kiranvjkiranvj

29.2k6 gold badges67 silver badges74 bronze badges

I think you block should be :


UPDATE

But you don't really need to do this, you have

"
"."$desc
"."$univ
". "Number of students: $size
". "Started at: ".date('F d, Y',strtotime($start))."
".

You can try this

$(function (){
      $('.rc_desc').hover(function ()
      {
        $(this).toggleClass('.tr');
      });
      $('.rc_desc').click(function ()
      {
        attrId = $(this).attr("data-id");
        $(location).attr('href','student.php?'+id);
      });
});

answered Jul 13, 2012 at 16:44

Add php variable to javascript

DavuzDavuz

4,79413 gold badges39 silver badges58 bronze badges

Can I put PHP variable in JavaScript?

Inside the JavaScript, we can use the PHP tag to echo the PHP variable assigning it to a JavaScript variable. For example, assign a value Metallica to a variable $var inside the PHP tags. Write a script tag and inside it, write a PHP tag. Inside the PHP tag, echo a JavaScript variable jsvar .

How set PHP variable value in JavaScript?

1) First add a cookie jquery plugin. 2) Then store that window width in a cookie variable. 3) Access your cookie in PHP like $_COOKIE['variable name'].

Can you mix PHP and JavaScript?

Besides, PHP and JavaScript similarities, these two languages are a powerful combination when used together. Large numbers of websites combine PHP and JavaScript – JavaScript for front-end and PHP for back-end as they offer much community support, various libraries, as well as a vast codebase of frameworks.

Can I use PHP echo in JavaScript?

The echo keyword is used to display the values of variables var1 and var2 to the HTML document which is readable by the browser normally. This property can be taken into an advantage in writing to JavaScript console using PHP.