[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

How to pass a php variable to a javascript file (function)?

John J. Hughes II

10/1/2015 11:13:00 PM

I have a php-file which contains a javascript:
<script type="text/javascript">
function doIt() {
var some_string = <?php echo json_encode($sometext); ?>;
window.location.href = encodeURI("index.html");
}
</script>

So it opens a new html site. Then this html site calls javascript
function from a js-file:

test.js :

function doSomething() {
// here I want to use that some_string created in the php file....
}

So in doSomething I want to be able to use this some_string created in
php file. How can I do it? I tried couple of things, but I do not really
understand Ajax... so if the solution is Ajax I would need detailed
information. I checked on the internet but no Ajax example was detailed
enough for me to get it working. Or if there is non-Ajax way that is
also ok.

Or can I pass that some_string first to that html and then that html can
pass it to js-file (to doSomething function)?

But please with Ajax it would need to be detailed answer as I am a
newbie :). All google-Ajax examples failed when I tried them.

Thank you.



32 Answers

Evertjan.

10/2/2015 7:45:00 AM

0

JiiPee <no@notvalid.com> wrote on 02 Oct 2015 in comp.lang.javascript:

> I have a php-file which contains a javascript:

Impossible.

Javascript is a language [though some even doubt that].

Perhaps your text-file contains "Javascript script"-text.

> <script type="text/javascript">
> function doIt() {

This does not work, as doIt() is not called clientside.

> var some_string = <?php echo json_encode($sometext); ?>;

This is totally errorprone, you need to enclose the litteral string in '-s
or "-s, ensuring that the ones used do not appear in the inner text.

Have a look houw the resulting stream looks on the client-browser using
view-source.

> window.location.href = encodeURI("index.html");

There is no need to encodeURI() "index.html"'

If[!] doIt() is run then the clientside variable some_string is lost.

> }
> </script>
>
> So it opens a new html site.

No.

If[!] doIt() is run then the browser opens "index.html",
which is not a SITE but a PAGE,
from your server on the browser.

> Then this html site calls javascript
> function from a js-file:

No.

html-PAGES do not CALL javascript files,
they could include code to include a js-file.

> test.js :
>
> function doSomething() {
> // here I want to use that some_string created in the php file....
>}

That variable some_string, being a clientside javascript varaiable on
another PAGE, was lost when leaving that page.

> So in doSomething I want to be able to use this some_string created in
> php file. How can I do it?

So you cannot.

Why don't you create that string on the "index.html" PAGE,
that you probably will have to rename to "index.php".

I tried couple of things, but I do not really
> understand Ajax...

There is no mention of Ajax here, only of some lost serverside json-code.

> so if the solution is Ajax I would need detailed
> information. I checked on the internet but no Ajax example was detailed
> enough for me to get it working. Or if there is non-Ajax way that is
> also ok.

Seems now I am lost, wouldn't it be better if you told us what you really
want to achieve, instead of letting us guess by showing some unworkable
ideas?

> Or can I pass that some_string first to that html and then that html can
> pass it to js-file (to doSomething function)?

Eh?

> But please with Ajax it would need to be detailed answer as I am a
> newbie :). All google-Ajax examples failed when I tried them.

:) ?

Being a newbie is no joke, it is and should be hard.

Seems to me you need to explain what you want in a better way.

Programming primarily is about describing what you want to yourself in a
logical way, not copying code from the web you do not understand and
expecting it to do what you have not visualized.

And remember, only Javascript is on topic here, either clientside or
serverside, php is not.

> Re: How to pass a php variable to a javascript file (function)?

Seems you were talking about a clientside Javascript variable here,
those variables are only valid as long as the PAGE exists,
they do not exist on the [serverside] SITE.

Your "javascript file" surely exists on the server, so including something
there needs serverside scripting. You could howeve include something in the
stream that is sent to the client using php when renaming the js file to:
"myJSfile.js.php".

[Then again, probably you want something else than you say you want.]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Stefan Weiss

10/2/2015 10:14:00 AM

0

On 2015-10-02 09:44, Evertjan. wrote:
>
>> var some_string = <?php echo json_encode($sometext); ?>;
>
> This is totally errorprone, you need to enclose the litteral string in '-s
> or "-s, ensuring that the ones used do not appear in the inner text.

No, json_encode() already takes care of quoting and character escaping;
adding extra quotes around the result would be a syntax error.

That doesn't mean that json_encode() by itself is enough to safely
escape strings as JavaScript literals. What if $sometext contains
"</script>"?

What actually has to be escaped - in addition to characters like quotes,
newlines, etc - depends on which HTML version is used.
For HTML5, the process and some pitfalls are described here:
http://www.w3.org/TR/html5/scripting-1.html#restrictions-for-contents-of-scrip...

Short version:

| Note: The easiest and safest way to avoid the rather strange
| restrictions described in this section is to always escape "<!--" as
| "<\!--", "<script" as "<\script", and "</script" as "<\/script" [â?¦]

> That variable some_string, being a clientside javascript varaiable on
> another PAGE, was lost when leaving that page.
>
>> So in doSomething I want to be able to use this some_string created in
>> php file. How can I do it?
>
> So you cannot.
>
> Why don't you create that string on the "index.html" PAGE,
> that you probably will have to rename to "index.php".

Thats one way to do it. Unfortunately, we don't know enough about OP's
project to give any meaningful advice. For example, if we knew that
`some_string` is relatively short, we could suggest passing it along to
index.html in the query string.

location.href = "index.html?s=" + encodeURIComponent(some_string);

The script included from index.html could then read it back from the
query string.

In general, mixing dynamic and static files (PHP and plain HTML) like
this tends to get messy.


- stefan

John J. Hughes II

10/2/2015 10:36:00 AM

0

On 02/10/2015 08:44, Evertjan. wrote:
>> <script type="text/javascript">
>> function doIt() {
> This does not work, as doIt() is not called clientside.

What do you mean by "does not work"? I have this code and it does open
the index.html when this doIt is called. The purpose of doIt is to
prepare a string (some_string) and then open index.html. And index.html
contains js code and the purpose is that this js could somehow use this
some_string content. yes its a local variable here and is deleted, but I
mean we could pass it or put on some global variable to used it later in
that javascript file. Thats the purpose...

>
>> var some_string = <?php echo json_encode($sometext); ?>;
> This is totally errorprone, you need to enclose the litteral string in '-s
> or "-s, ensuring that the ones used do not appear in the inner text.

yes true.

>
> Have a look houw the resulting stream looks on the client-browser using
> view-source.
>
>> window.location.href = encodeURI("index.html");
> There is no need to encodeURI() "index.html"'

well, there was other code there as well, like variables, so I needed
that encode.... but yes not here is needed.

>
> If[!] doIt() is run then the clientside variable some_string is lost.
>

yes, but the question is that where can we save that some_string (or
pass it?) so that later on some javascript file could us it. Is a good
idea to pass it to index.html somehow and from there to that javascript
file?

>> Then this html site calls javascript
>> function from a js-file:
> No.
>
> html-PAGES do not CALL javascript files,
> they could include code to include a js-file.

yes and that code calls a js function. thats what happens

>
>> test.js :
>>
>> function doSomething() {
>> // here I want to use that some_string created in the php file....
>> }
> That variable some_string, being a clientside javascript varaiable on
> another PAGE, was lost when leaving that page.

yes, but can we save it somehow somewhere to be able to use here?? thats
the question. or can we have some route to pass it all the way here from
there?

>
>> So in doSomething I want to be able to use this some_string created in
>> php file. How can I do it?
> So you cannot.
>
> Why don't you create that string on the "index.html" PAGE,

hmmm, yes i could possibly create it in that index.html (although to
create it i need php code... but I guess its doable in html file....
surely) page (as I can pass information to that file from that php file
to form that string). But how could this help?

> that you probably will have to rename to "index.php".

yes, that is a good idea.... at least to try. and migh work for me...
but index.html is quite big file I think, i hope the change happens
without problems. I can try....

>
> I tried couple of things, but I do not really
>> understand Ajax...
> There is no mention of Ajax here, only of some lost serverside json-code.

not here , but I tried Ajax elsewhere

> :) ?
>
> Being a newbie is no joke, it is and should be hard.
>
> Seems to me you need to explain what you want in a better way.
>
> Programming primarily is about describing what you want to yourself in a
> logical way, not copying code from the web you do not understand and
> expecting it to do what you have not visualized.

so far I pretty much understand what is happening and the sites work ok
.. But am a bit stuck with this one. I can pass php variables to js code
in a same file, no problems.

>
> And remember, only Javascript is on topic here, either clientside or
> serverside, php is not.

ok but those languages are very much linked in real life....

>> Re: How to pass a php variable to a javascript file (function)?
> Seems you were talking about a clientside Javascript variable here,

I have php file with contains this js code which creates this variable I
want to forward to other javascript files:
php-file:
- contains a js-code which creates a js variable (and/or php variable...
does not matter)

index.html:
- php-file opens this
- this file contains a js code/fucntion which is located in a separate
js-file

js-file:
here, in one function, I want to get hold of that string which was
created in php file above.


> those variables are only valid as long as the PAGE exists,
> they do not exist on the [serverside] SITE.
>
> Your "javascript file" surely exists on the server, so including something
> there needs serverside scripting. You could howeve include something in the
> stream that is sent to the client using php when renaming the js file to:
> "myJSfile.js.php".
>

ok thanks, at least I can try your idea of changing that index.html to
php... maybe it works...

John J. Hughes II

10/2/2015 10:43:00 AM

0

On 02/10/2015 11:14, Stefan Weiss wrote:
> On 2015-10-02 09:44, Evertjan. wrote:
>>> var some_string = <?php echo json_encode($sometext); ?>;
>> This is totally errorprone, you need to enclose the litteral string in '-s
>> or "-s, ensuring that the ones used do not appear in the inner text.
> No, json_encode() already takes care of quoting and character escaping;
> adding extra quotes around the result would be a syntax error.
>
> That doesn't mean that json_encode() by itself is enough to safely
> escape strings as JavaScript literals. What if $sometext contains
> "</script>"?
>
> What actually has to be escaped - in addition to characters like quotes,
> newlines, etc - depends on which HTML version is used.
> For HTML5, the process and some pitfalls are described here:
> http://www.w3.org/TR/html5/scripting-1.html#restrictions-for-contents-of-scrip...
>
> Short version:
>
> | Note: The easiest and safest way to avoid the rather strange
> | restrictions described in this section is to always escape "<!--" as
> | "<\!--", "<script" as "<\script", and "</script" as "<\/script" [â?¦]
>
>> That variable some_string, being a clientside javascript varaiable on
>> another PAGE, was lost when leaving that page.
>>
>>> So in doSomething I want to be able to use this some_string created in
>>> php file. How can I do it?
>> So you cannot.
>>
>> Why don't you create that string on the "index.html" PAGE,
>> that you probably will have to rename to "index.php".
> Thats one way to do it. Unfortunately, we don't know enough about OP's
> project to give any meaningful advice. For example, if we knew that
> `some_string` is relatively short, we could suggest passing it along to
> index.html in the query string.

yes. well... this string is actually possible quite long as it is a
(random) text from a document. maybe passing like this is not good... as
its a bit long

>
> location.href = "index.html?s=" + encodeURIComponent(some_string);
>
> The script included from index.html could then read it back from the
> query string.

yes that is an idea. And then index.html could send it to the
js-function it "calls"? maybe better to change html to php first.

>
> In general, mixing dynamic and static files (PHP and plain HTML) like
> this tends to get messy.
>
>
> - stefan

John J. Hughes II

10/2/2015 10:58:00 AM

0

I guess my main question is that if I have a php file how can I send
data/string from there to a javascript file.

php file:
contains some data/string

test.js file:
wants to get hold of that string created in php file

Stefan Weiss

10/2/2015 11:38:00 AM

0

On 2015-10-02 12:58, JiiPee wrote:
> I guess my main question is that if I have a php file how can I send
> data/string from there to a javascript file.
>
> php file:
> contains some data/string
>
> test.js file:
> wants to get hold of that string created in php file

Ah. Now that the navigation is out of the way, it's simple enough.
test.js has access to the same variables as any inline script in the
document. You can make some_string a global variable; local variables in
functions are never visible outside the function.


- stefan

John J. Hughes II

10/2/2015 11:53:00 AM

0

On 02/10/2015 12:38, Stefan Weiss wrote:
> On 2015-10-02 12:58, JiiPee wrote:
>> I guess my main question is that if I have a php file how can I send
>> data/string from there to a javascript file.
>>
>> php file:
>> contains some data/string
>>
>> test.js file:
>> wants to get hold of that string created in php file
> Ah. Now that the navigation is out of the way, it's simple enough.
> test.js has access to the same variables as any inline script in the
> document. You can make some_string a global variable; local variables in
> functions are never visible outside the function.
>
>

ok, am currently testing this. But my code in php is not even able to
call javascript function (in an external file). What am i doing wrong:

php file:
------------
<script type="text/javascript" src="test.js"></script>

....// some button triggers callExternalJSFunction()

<script>...
function callExternalJSFunction() {
sayGoodbye();
}
</script>


test.js file
----------
function sayGoodbye(){
alert("Bye!");
}
------------------------

What is wrong here? why sayGoodbye is get called? how should i do this?
after that I guess it will be easy to send the data as well, but I
cannot even get the function call working now.. :) my brain does not work :)



John J. Hughes II

10/2/2015 12:47:00 PM

0

On 02/10/2015 12:53, JiiPee wrote:
>
>
> What is wrong here? why sayGoodbye is get called? how should i do
> this? after that I guess it will be easy to send the data as well, but
> I cannot even get the function call working now.. :) my brain does not
> work :)
>
>
>

ok, there is something else wrong in my project. I got this working on
another project.... so will next test the global variable idea.

Ben Bacarisse

10/2/2015 12:52:00 PM

0

JiiPee <no@notvalid.com> writes:
<snip>
> ok, am currently testing this. But my code in php is not even able to
> call javascript function (in an external file). What am i doing wrong:
>
> php file:
> ------------
> <script type="text/javascript" src="test.js"></script>
>
> ...// some button triggers callExternalJSFunction()
>
> <script>...
> function callExternalJSFunction() {
> sayGoodbye();
> }
> </script>
>
>
> test.js file
> ----------
> function sayGoodbye(){
> alert("Bye!");
> }
> ------------------------
>
> What is wrong here? why sayGoodbye is get called? how should i do
> this? after that I guess it will be easy to send the data as well, but
> I cannot even get the function call working now.. :) my brain does not
> work :)

You are not helping us to help you. Put an actual example online so we
can go and find out what's wrong. If you can't do that, post the actual
files that make up your test.

If you do put up an example, it helps if you also make a text file for
every PHP file. I.e. if your test page is called test.php, you also put
an exact copy called test.php.txt on the server. If you don't do this
we have no way of seeing the PHP code.

--
Ben.

Evertjan.

10/2/2015 2:35:00 PM

0

JiiPee <no@notvalid.com> wrote on 02 Oct 2015 in comp.lang.javascript:

> On 02/10/2015 08:44, Evertjan. wrote:
>>> <script type="text/javascript">
>>> function doIt() {
>> This does not work, as doIt() is not called clientside.
>
> What do you mean by "does not work"? I have this code and it does open
> the index.html when this doIt is called.

That is the key: "when this doIt is called"!

Please do not let us assume.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)