RSX_ Posted May 10, 2011 Share Posted May 10, 2011 This has got to be possible, I have a chatbox script, which reads the person's rights by their username, which when displaying the chat username / text, I use javascript, so I need PHP to parse the javascript text, how can I do this? <?php $user = 'user_node[0].firstChild.nodeValue;'; ?> That returns "user_node[0].firstChild.nodeValue;", I can't include <script> in the echo text, due to it's inside of a Javascript function already. I need it to do this: <?php $user = 'user_node[0].firstChild.nodeValue;'; ?> var user = '<?php echo getRank($user); ?>'; aka var user = '<?php echo getRank('Username'); ?>'; I can do the second part, PHP variable to Javascript variable, just not the first part. Any help is appreciated, thanks. Edit: echo "chat_div.innerHTML += userN2;"; returns "Username" $var = "chat_div.innerHTML += userN2;"; echo $var; returns chat_div.innerHTML += userN2; Just to simplify the problem. Quote Link to comment https://forums.phpfreaks.com/topic/236039-parsing-javascript-variables-in-php/ Share on other sites More sharing options...
Psycho Posted May 10, 2011 Share Posted May 10, 2011 You're really not making this clear. I have a good idea of the general issue you are facing but not sure what code you need because the specifics are clouded in your explanation. But, let's go with your Edit since you state that will make it more clear: echo "chat_div.innerHTML += userN2;"; \\returns "Username" That is impossible. That will echo the exact text between the quotes. This is where it is confusing (at least to me). $var = "chat_div.innerHTML += userN2;"; echo $var; returns chat_div.innerHTML += userN2; Ok, that makes perfect sense. Now, what I think you are having a problem with is that userN2 doesn't have a value in the JS. Is that correct? If so, you just need to set the value using something like this echo "var userN2 = '" . echo getRank($user); . "'; \n"; echo "chat_div.innerHTML += userN2;\n"; Quote Link to comment https://forums.phpfreaks.com/topic/236039-parsing-javascript-variables-in-php/#findComment-1213467 Share on other sites More sharing options...
RSX_ Posted May 10, 2011 Author Share Posted May 10, 2011 Sorry, feeling a bit tired, which is why I make less sense. echo "var userN2 = '" . echo getRank($user); . "'; \n"; echo "chat_div.innerHTML += userN2;\n"; userN2 is declared, var userN2 = user_node[0].firstChild.nodeValue; I need Javascript to give the PHP variable ($user) a username to get the user's rank, I can give Javascript a variable from PHP, however, when someone types in the chatbox, it would read their user level, therefore making everyone else have the same rank. Still trying to make as much sense as I can, and again, I appreciate the help. var user_node = message_nodes[i].getElementsByTagName("user"); var text_node = message_nodes[i].getElementsByTagName("text"); var time_node = message_nodes[i].getElementsByTagName("time"); var userN2 = user_node[0].firstChild.nodeValue; That's all that's declared in the function, I know I don't have to have userN2 declared, I only declared it, for testing purposes, makes it easier / faster to type. Quote Link to comment https://forums.phpfreaks.com/topic/236039-parsing-javascript-variables-in-php/#findComment-1213470 Share on other sites More sharing options...
Psycho Posted May 10, 2011 Share Posted May 10, 2011 Yeah, still not following you. Here is what I *think* is going on. You are running some JS against records displayed in the chatbox but you are apparently using the rank from the current user and applying that against each user record in the chatbox. Not really sure if that is correct or not. But, it seems what you need to do is when creating the chatbox in PHP you need to retrieve the relevant variables for each record and store them as JS variables. Since you are apparently referencing the chatbox records by their DOM id, I would store the values accordingly. In your PHP that creates the initial chatbox you could create a JS array that matches the DOM array in the format var ranks = new Array(); ranks[0] = 3; ranks[1] = 1; ranks[2] = 5; //etc The PHP code would look something like this: $jsRanksArray = "var ranks = new Array();\n"; //Loop to create output $index = 0; while($row = mysql_fetch_assoc($result) { //-------------- //Code to produce chatbox output goes here //-------------- //Add to JS array code for current record $jsRanksArray .= "ranks[0] = '" . $row['rank'] . "';"; $index++; } To be honest I am just guessing here as you are leaving information out that is needed to really provide a solution. Quote Link to comment https://forums.phpfreaks.com/topic/236039-parsing-javascript-variables-in-php/#findComment-1213500 Share on other sites More sharing options...
RSX_ Posted May 11, 2011 Author Share Posted May 11, 2011 I have the ranks in an array, this was before I started the chat, however, thanks to your post, I see I'm looking to grab the user's rank in my main PHP file for the chat, and then calling it from the secondary (with Javascript) file. I may just be clueless as to what I'm doing, hopefully not, but I think I can take it from here, just realized it, I appreciate the help, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/236039-parsing-javascript-variables-in-php/#findComment-1213506 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.