FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


[Resolved]How to use php to replace a javascript variable?





Hogwarts
How would I make PHP replace all the javascript variables with this name, no matter what their value, if they have a certain name?

This is an example variable:
Code:

example_variable = "WILDCARD"


It should be replaced with...

Code:

example_variable = "123"


I've tried preg_replace and all. This is so confusing to a php n0ob Sad
kv
PHP runs on server side, and works before the data is in the browser. Javascript is on client side (browser), and works once the data is sent to the browser.

It is not possible to replace the value of a javascript variable using php, unless you want to do it when you sent the script from server side.
Hogwarts
kv wrote:
PHP runs on server side, and works before the data is in the browser. Javascript is on client side (browser), and works once the data is sent to the browser.

It is not possible to replace the value of a javascript variable using php, unless you want to do it when you sent the script from server side.

I've made it so that the PHP gets the page, and stores it as a variable and echos that.

I need it to go through that and replace the javascript variable, before it's echoed of course.

Like, the javascript variable is definied before the php processes it, because it's defined inside the file (The value is not set by javascript. It's a constant - But it changes page to page.)
mariohs
Have you tried inserting the <script> tag with php?

Code:
<script type="text/javascript">
example_variable = "123"
</script>


Before you echo all the values with php, echo the <script> tag.

I don't have any idea if this will work, although I think it should. Just let me know.
Hogwarts
mariohs wrote:
Have you tried inserting the <script> tag with php?

Code:
<script type="text/javascript">
example_variable = "123"
</script>


Before you echo all the values with php, echo the <script> tag.

I don't have any idea if this will work, although I think it should. Just let me know.

Hmm, I don't believe you guys know what I mean Sad

I mean like this:
Code:
<script type="text/javascript" language="javascript">
example_variable_name = "1234567890" // The value of this variable needs to be replaced, no matter what the value.
--some funky thing that uses that variable--
</script>


The value of the variable needs to be replaced, no matter what the value of that variable is, for each time that that variable name appears.[/code]
mariohs
No, I definitely couldn't understand.
You have the example_variable set to "something". Then you want to change to "another_value", right? Why don't you just do:
example_variable = "another_value"?

Or even better... what exactly are you trying to do? Isn't there any better ways to do it? Wink
Hogwarts
mariohs wrote:
No, I definitely couldn't understand.
You have the example_variable set to "something". Then you want to change to "another_value", right? Why don't you just do:
example_variable = "another_value"?

Because, I've set the php up to get the file and display it, but I need it to change that variable before it displays it.
I.e.
Code:
$Example_php_variable = preg_replace('variable_name = "SOMEPHPWILDCARD";', 'variable_name = "1234567890";', $variable_name_that_it_needs_to_go_through)


All I need it to do is change that javascript variable from one value to another.
mariohs
Quote:
Code:
$Example_php_variable = preg_replace('variable_name = "SOMEPHPWILDCARD";', 'variable_name = "1234567890";', $variable_name_that_it_needs_to_go_through)


Why don't you do like I said before?

Code:
<?php
echo "<script type='text/javascript'>";
echo "example_variable = $new_value";
echo "</script>";
?>


This changes the value of example_variable (but remember that I don't really know if this works).
Hogwarts
mariohs wrote:
Quote:
Code:
$Example_php_variable = preg_replace('variable_name = "SOMEPHPWILDCARD";', 'variable_name = "1234567890";', $variable_name_that_it_needs_to_go_through)


Why don't you do like I said before?

Code:
<?php
echo "<script type='text/javascript'>";
echo "example_variable = $new_value";
echo "</script>";
?>


This changes the value of example_variable (but remember that I don't really know if this works).


Sorry. Misunderstanding..
I mean like a find and replace thing would work, so it just searches through the thing and find everywhere where that value is defined, and changes the value. It needs a wildcard though because the variable changes on different pages.

Your way wouldn't work because the variable would be redefined later on, wouldn't it? And that would negate the purpose of changing it.
mariohs
I'm going nuts... I can't understand anything anymore Very Happy
What exactly you want to do? And why mix javascript with php?
Hogwarts
mariohs wrote:
I'm going nuts... I can't understand anything anymore Very Happy
What exactly you want to do? And why mix javascript with php?

Hehe... Razz... Sorry.

I'm not really mixing javascript with php. The javascript is on a different page - Hosted at a different location. I just need the php to change that variable.
mariohs
Let me see if I got...
You have a php page that wants to change a javascript variable from another page? This is not possible, I'm afraid.
Hogwarts
mariohs wrote:
Let me see if I got...
You have a php page that wants to change a javascript variable from another page? This is not possible, I'm afraid.

It's not?
Er.. I think it is.

I've already written the php to get that page and effectively store it in a variable - I just need the script to find and replace that javascript variable.

Using something like preg_replace to go through that string, it should be able to change the variable? Sad thing is, I'm clueless how to use it to change the variable(s).
mariohs
You cannot get a javascript variable value from php, not in that way...
Hogwarts
mariohs wrote:
You cannot get a javascript variable value from php, not in that way...

I'm not trying to get it Confused
I'm just trying to change it.
I also worked out something....
Instead of using a wildcard, this could be done:
Code:
preg_replace("variable_name = ", "variable_name = \"1234567890\"; variable2_name = ", $sourcecode_of_the_html_file);

Although, I'm still not entirely sure how to do this Sad

Sorry for being so useless. Sad
mariohs
You also cannot change it... it is on another page.
The php page that gets another page with the javascript on it doesn't know that that page has any scripts on it... unless the script tag is on the page (not a separate .js file).

When you get the another page, you get the whole content as a string?
Hogwarts
mariohs wrote:
You also cannot change it... it is on another page.
The php page that gets another page with the javascript on it doesn't know that that page has any scripts on it...


I know.

An example page it's getting it from:
Code:
<html><head></head><body><script language="javascript" type="javascript/text">variable_name = "1234567890";</script></body></html>


The php would just go through that, find where
Code:
variable_name = "1234567890";
is and then replace that with
Code:
variable_name = "0987654321";


It doesn't matter about the <script> tags -- I'm sure that preg_replace or whatever would just go directly through the source and change what it needs to change.
mariohs
Cool! Finally I got it Very Happy

I was thinking you were trying to mix javascript and php (that's what I got from the topic).

You should use regular expressions to use the preg_replace.

Something like:

I'm not a pro at regula expressions, but take a look:
http://en.wikipedia.org/wiki/Regular_expression
http://www.regular-expressions.info/
Hogwarts
Ok, this is now -resolved-.
Thanks for all the help mariohs - Couldn't have done it without you, so kudos to you Wink
Related topics
Save webpage source into javascript variable
PHP Question
PHP to Javascript
Need help with PHP with Javascript
convertin Html codes to javascript or php! 10 frih$ each!
Javascript news feed?
php / javascript script works in FireFox browser, but not IE
Browser close run a php script.
Simple php or javascript divx webplayer playlist
PHP Remove Text?
please please beta test it (PHP coders)
javascript php html styling and good practice.
special formatting for the link i'm in.
Table data print feature using php or javascript?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.