I am trying to call
this js function that is stored on a file called default.js from a php file.
function myfunction(score) {
console.log("got here");
alert(score);
}
so this is the call for the js function on my php code but it does not work.
<!-- myfunction js file ↓ -->
<script type="text/javascript" src="../js/default.js"></script>
<?php
if ($grade=="100%"){
// not able to call
echo "myfunction($grade)";
}
else{
echo "alert(\"haha\")";
}
?>
Try this you will get the accurate result
<?php
if ($grade == "100%") {
?>
<script>
myfunction('<?=$grade?>');
</script>
<?php
} else {
?>
<script>
alert('in');
</script>
<?php } ?>
you forgot about the <script> tags
change your echo like this:
echo "<script>myfunction('$grade')</script>";
you need to wrap ' around the $grade because you're passing a string
or put <script> before the opening php tag and </script> after the end of php tag
This is your Code.
Define $grade too in PHP or you get a
<b>Warning</b>: Undefined variable $grade in <b>
<script type="text/javascript" src="../js/default.js"></script>
<script>
<?php
$grade = "10%";
if ($grade=="100%"){
echo "myfunction('$grade')";
}
else{
echo 'alert("haha");';
}
?>
</script>
Dont forget the <script></script> for javascript code inside html.
Related
I need to put echo as condition of if statement. So, for example:
<html>
...
...
<body>
<form onsubmit"<?php $c=false; if(echo "<script>example();</script>";){ $c=true; } echo "\""; if($c){ echo "action=\"./\"";} method="post">
...
</form>
...
...
<script>
function example()
{
alert("This is only an example");
if(..) return true;
return false;
}
</script>
</body>
</html>
So I need to create a function in JavaScript (this top is only an example, but, in realty, it is more complicated) and call it in the if statement.
Update
The code should be execute "action" of the form only if the Javascript function is true.
PHP runs in the server. JavaScript runs in the client. So php can't use a js variable or its function to server side action. What u can do is , u can call JS function from PHP like
echo "<script> myFunction() </script>";
But in the same same case, u can use php values for JS actions,
eg
<script>
var x = <?php echo $myVar ?>
if(x == true){
alert("yahooo");
}
</script>
if (!$errors) {
//Call Js function without any events
}
I want to call js function once inside if came,How to call js function inside if condition?Is it possible to call js fucntion without any events inside php tag?
with jquery
if (!$errors) {
echo "<script type='text/javascript'>
$(document).ready(function(){
myfunction();
});
</script>";
}
without jquery and wait unlit whole page is loaded
if (!$errors) {
echo "<script type='text/javascript'>
window.onload = function() {
myfunction();
};
</script>";
}
and without Onload
if (!$errors) {
echo "<script type='text/javascript'>myfunction();</script>";
}
You can also use this way :
<?php
// php code
if(condition){
?>
<script type="text/javascript">
jsFunction();
</script>
<?php
}
// php code
?>
You can also use jquery:
if (!$errors) {
echo "<script type='text/javascript'>
$(document).ready(function(){
myfunction();
});
</script>";
}
You could just make an echo and call it between two script tags.
Like :
if (!$errors) {
echo "<script> myfunction(); </script>";
}
I want to know if my php variable does not exists, then I want to execute my javascript.
For example-
<?php
if(!isset($_REQUEST['myVar'])){
?>
<script>
alert('variable not exist');
</script>
<?php
}
?>
Is this right way to use javascript code in php extension file
I know all other answers solve your issue but i prefer it to do this way
<script>
<?php
$isset = !isset($_POST['myVar']) ? 'true' : 'false';
echo "var isset = $isset;";
?>
if(isset) {
alert('variable not exist');
}
</script>
when php render your page it will give this output
<script>
var isset = true;
if(isset) {
alert('variable not exist');
}
</script>
Do it like this and it will work:
if (!isset($_REQUEST['myVar'])) {
echo "<script>alert('variable not exist');</script>";
}
you can try writing this piece of code where you want the script to be placed:
<?php
if (!isset($_REQUEST['myVar'])) {
echo '<script>
alert("variable not exist");
</script>';
}
?>
I am using the below code to call the javascript function from php script. Its not working while am adding the php variable in javascript($msg). Please help me to do this.
if ( isset($_GET['Error'])) {
$msg =$_GET["Error"];
echo '<script type="script.php">validate("Error",$msg);</script>';
}
You have to quote the $msg or it will be syntax error in javascript.
And the type is non sense.
Since the msg is from the $_GET, don't forget the escape it.
if ( isset($_GET['Error'])) {
$msg =$_GET["Error"];
echo '<script>validate("Error", "'.htmlspecialchars($msg).'");</script>';
}
Instead of an inline script, this should work:
<html>
<head>
<script type="text/javascript">
function testMe(x){
alert(x);
}
</script>
</head>
<body>
<?php
$phpVar = 'I am a PHP variable';
echo "Click Me";
?>
</body>
<html>
This question already has answers here:
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
(14 answers)
Closed 9 years ago.
I need to echo a PHP variable into JavaScript. But I have no experience about JavaScript, I did some research but nothing worked for me.
The code
function hide() {
document.getElementById("myDiv").style.display="none";
<?php echo $myVar; ?>
}
And the $myVar looks like:
$myVar = "Hi there";
you can do like this in js :
var php_val = '<?php echo $myVar; ?>';
alert(php_val);
use this
$myVar = 'var myVar="Hi here"'
or
function hide() {
document.getElementById("myDiv").style.display="none";
var myVar='<?php echo $myVar; ?>'
}
There is 2 ways to write PHP variable in javascript:
First Way:
JavaScript written in the php page or inline:
EX:
<?php
$myVar = 'hello';
?>
....
<script>
function hide() {
document.getElementById("myDiv").style.display="none";
<?php echo $myVar; ?>
}
</script>
Second way:
Put your variables on the top of your page, like baseUrl for example, and use it in separated JavaScript file but you have to write the variables before load JavaScript files.
EX:
<head>
<script>
var baseUrl = <?php echo $thisPageUrl; ?>
</script>
</head>
....
<script src="myscriptfile.js"></script>
<?php $var="blah"; ?>
<script type="text/javascript">
alert('<?php echo $var ?>');
</script>