/    Sign up×
Community /Pin to ProfileBookmark

Calling a Javascript function from within PHP

Hi, I am sorry if this has been covered elsewhere, but I’m still stuck after reading quite a lot of them.

Basically I’m trying to run a small javascript function from within some PHP script. Here is a very basic example of what i’m doing. (My javascript is in a separate .js file):

// Returns the day from a mySQL date string
function getDay(dateString){
day = dateString.substring(8,10);
return day;
}


———————————————–

-Then in the PHP code:

echo(“<script language=’javascript’ src=’functions.js’>getDay($date)</script>”);

Can anyone help? I realise i’m probably missing something very simple. Thanks

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJan 13.2009 — PHP is a server side language and javascript is client side, so you can't call a javascript function from PHP. What you are doing in the code you posted is building the html which [b]will[/b] run the javascript when the html is executed in the browser, but by then PHP has been and gone.

Also (and i'm not a js coder particularly so I could be wrong), i dont think the javascript you posted would appear to do anythng even when run from the browser because you are not doing anything with the returned value from the program.

This should let you see the return from the js function:

<i>
</i>echo "&lt;script language='javascript' src='functions.js'&gt;&lt;/script&gt;";
echo "&lt;script language='javascript'&gt;alert(getDay($date));&lt;/script&gt;";
Copy linkTweet thisAlerts:
@coldscooterauthorJan 13.2009 — Thanks for your post. Something definately clicked for me then.

I've solved the problem by using this simple php function:

<?php

// This function converts a string in the format returned by MySQL into dd-mm-yyyy.

function convertDate($dateString){

$day = substr($dateString,8,2);

$month = substr($dateString,5,2);

$year = substr($dateString,0,4);

$wholeDate = $day."/".$month."/".$year;

return $wholeDate;

}

?>
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — Actually, Javascript could be used on the server end also but not many people know that
Copy linkTweet thisAlerts:
@MindzaiJan 13.2009 — Or you could do:

[code=php]$date = date('d/m/Y', strtotime($dateString));[/code]
Copy linkTweet thisAlerts:
@MindzaiJan 13.2009 — Actually, Javascript could be used on the server end also but not many people know that[/QUOTE]

Yes but I'm guessing the OP is talking about CSJS ?
Copy linkTweet thisAlerts:
@TheTeenScripterJan 13.2009 — You're right, that is the case. I was just pointing it out for the hell of it ?
×

Success!

Help @coldscooter 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.19,
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,
)...