0

I have a bunch of CSS properties stored in a MySQL database accessed via PHP. I need to make these properties available to JavaScript after the page has finished loading.

So what I did is foreach row, put the values in a Javascript object like so:

foreach ($cellcontent as $cellproperty) {
    echo 'var '.$cellproperty->cell_id.' = {cellwidth:"'.$cellproperty->cell_width.'"};';
}

(For simplicity's sake I've only included one object property here but in reality there are many more.)

My problem is that at runtime, via JavaScript I get the cell_id reference which is somewhere in the html page like so:

var dacell = $(this).closest("div");
var cellid = dacell.attr("id");

So at this point, cellid is equal to the name of my var from the php output.

But when I try to get the property of my object (cellwidth) via JavaScript it doesn't work. Says its undefined when I try to see the value in an alert:

alert(cellid.cellwidth);

I think I'm just not referencing the actual object at this point and just trying to get a property of what has now become a string.

Is there a way to get back the reference to the object itself?

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
Erick P.
  • 31
  • 4
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) –  Jan 11 '16 at 02:54
  • You are asking the wrong question. JS and PHP are two different things that execute in two different places. See http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming –  Jan 11 '16 at 02:54
  • What is the value of cell_id? If you edit your post to include the JavaScript output we can probably easily see what the issue is - view the HTML source to double check. Are there any errors in your JavaScript console? – Clay Jan 11 '16 at 02:55
  • It is much easier to read your question if you format it properly – Moshe Katz Jan 11 '16 at 03:01
  • Sorry Moshe. First time posting here. Wasn't aware I had options to format like this. – Erick P. Jan 11 '16 at 03:09

3 Answers3

2

var cellid = dacell.attr("id");

The variable cellid is a string. Your hopes would be that the variable your are looking is in the global namespace which you can access via the following:

window[cellid].cellwidth

jkris
  • 5,851
  • 1
  • 22
  • 30
-1

It's an awfull practice to pollute the global namespace with so much stuff. Fetch all the values you need to inject into the JS, create an associative Array and inject it as a single JSON into the Page.

Thomas
  • 3,513
  • 1
  • 13
  • 10
  • That's not correct. `var cellid = ...` has not overwritten anything from above. – Moshe Katz Jan 11 '16 at 03:02
  • Ah, ok, I got something wrong. I throught that "cellid" is one of the values that is output by `$cellproperty->cell_id`. I changed the Answer. – Thomas Jan 11 '16 at 03:09
-2

Nevermind everyone. The eval() javascript function fixed it all.

Instead of doing:

alert(cellid.cellwidth);

I did:

alert(eval(cellid).cellwidth);

and everything worked.

Thanks for all your time.

Cheers,

Erick P.

Erick P.
  • 31
  • 4
  • 1
    This is poor use of eval, use jkris's answer instead. The main point is that global variables are part of the window object ave can be accessed using property access instead of eval. – Ruan Mendes Jan 11 '16 at 03:17