/    Sign up×
Community /Pin to ProfileBookmark

In a php file i can acces from javascript to the php variables but not functions

I’ve an html file with php extension (index.php) and i use a mix of javascript and php code.
The problem is that i’m unable to launch a php function from JS..

this don’t work

[code=php]
<?php
function updateVideo(){
//some code
}?>

<script>
function aggiornaVideo(){
<?php updateVideo(); ?>
}

</script>[/code]

Yes, i know that PHP is a server side language while JS a client-side, but for what reason if i attempt to access to php variables along the document it’s possibile such they are stored in memory while the same thing isn’t possible with function?
For exemple, this code normally retrieve me a variable from php

[code=php]
<?php
$page=4
//some code
} ?>

<script>
function aggiornaVideo()
{
var pro='<?php echo $page; ?>’
alert(pro)
}

</script>[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NoEffinWayJul 29.2013 — What you are doing with the variables is basically converting them to Javascript. When you echo out the variable, it basically writes code that now says:
[code=html]<script>
function aggiornaVideo()
{
var pro='4'
alert(pro)
}

</script>[/code]


What you should do is make an AJAX call to a page with the PHP function you want to execute.

Here is a simple tutorial: http://www.tizag.com/ajaxTutorial/index.php
Copy linkTweet thisAlerts:
@American_horizoauthorJul 29.2013 — Yes i can use the ajax call, but technically i don't uderstand how is possibile to retrieve php variable such they are store in client memory, while function don't

What you are doing with the variables is basically converting them to Javascript.[/quote]
yes but this assumes that once the page is executed, the php variable is keeped in the client and can be retrieved. Why the rest of code not?
Copy linkTweet thisAlerts:
@iBeZiJul 29.2013 — PHP is executed before the page is loaded, JavaScript runs after the page has loaded. The PHP variable isn't kept on the client, what you've done there is use PHP to create a JavaScript variable.

It's impossible to call anything set in PHP using JavaScript without sending a request to another page using something like AJAX.
Copy linkTweet thisAlerts:
@American_horizoauthorAug 09.2013 — PHP is executed before the page is loaded, JavaScript runs after the page has loaded. The PHP variable isn't kept on the client, what you've done there is use PHP to create a JavaScript variable.

It's impossible to call anything set in PHP using JavaScript without sending a request to another page using something like AJAX.[/QUOTE]


so, doing

var someVar ='<?php echo $phpVar;?>'

is like a sort of call to the server-side script?
×

Success!

Help @American_horizo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...