-3

I am trying to pass a php variable to JavaScript that hold the name of the table, data will be written in. However I can not see the value when I console.log it. The script is activate on click, so it should not be an issue with page load, but I am not very good with JavaScript, so I have no idea what I am doing wrong

global $user;
            $user = user_load($user->uid);

            $params = Array (   
                                'company'   =>  $user->field_active_company[LANGUAGE_NONE][0]['value'] ,
                            );
$id = $user->name;          
$cu = "db_".$params['company'];
//Here I can see the value fine
echo $cu; 

.
.
.
.
//here I can not.
 var write = '<?php echo json_encode($cu); ?>';
 console.log(write);
var write2 = '<?php echo $cu; ?>';
console.log(write2);

Edit

To clarify:

  • I am asking why it does not work (or how to fix it).
  • the JavaScript has a function around it and all the needed tags. I am console logging textboxes prior to the JavaScript/PHP part.
4
  • You can not mix php with javascript like that. Is this exacly the code you are using? Commented Sep 5, 2014 at 7:41
  • Can you provide a var_dump($cu) or print_r($cu) ? Commented Sep 5, 2014 at 7:42
  • echo: db_0099 print_r: db_0099 Commented Sep 5, 2014 at 7:46
  • 1
    @Hossam Not a dublicate with that. I am not asking how, but why it does not work (it should). Commented Sep 5, 2014 at 7:48

2 Answers 2

1

console.log() doesn't print anything if the value is undefined. So maybe php is not rendering the value, or if the value is a string it's lacking double quotes "". You can see what is rendering php in the browser html code. Hope it is just double quotes.

Sign up to request clarification or add additional context in comments.

Comments

0

I still do not know what the issue was. I ended up passing the value through hidden field. This solved my issue, but could not find answer to my question. Daniel was right about the the value not coming through.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.