Uncaught reference error for echo function in php - javascript

I have used the follwoing php code snippet inside a java script code(inside a parent html).
<script>
function setOptions(d) {
<?php
echo "test";
?>
}
</script>
When the content is saved and the web page is refreshed, I get the following error,
Uncaught ReferenceError: test is not defined
Note- echo works when directly used inside the html code.
Any idea on how to fix this problem?

What do you want to happen? When this code is parsed by PHP it sends this to the browser (not valid JavaScript in the function):
<script>
function setOptions(d) {
test
}
</script>
So what is produced needs to be valid JavaScript which something like this:
<script>
function setOptions(d) {
<?php
echo 'alert("test");';
?>
}
</script>
Would produce valid JavaScript:
<script>
function setOptions(d) {
alert("test");
}
</script>

Related

How to use a php inside js

When i use the php and js separately it is working correctly
<script> var test = 'asdasdasd'; </script> <?php $id =
"<script>document.write(test)</script>"; echo $id; ?>
But when I use the php inside the js function it is not working correctly
<script> var test = 'asdasdasd'; <?php $id =
"<script>document.write(test)</script>"; echo $id; ?> </script>
How can make work my second code?
The second code produces wrong HTML/JS:
<script> var test = 'asdasdasd'; <script>document.write(test)</script> </script>
- see for yourself in the generated page.
Since everything between <script> and </script> is considered to be JS code, it will try to parse the inner <script> as Javascript code...and fail.
I think the inner other of <script>...</script> tags (those that are in PHP markup) is not needed at all, just get rid of them and it will work.
You can insert any value from php to absolutely everywhere in your file. Literally everywhere.
While writing client-side code, in the right place open php tag <?php and echo what you need. Don't forget to write a closing tag ?> afterwards.
Here, you are echoing the opening script tag, which you already wrote before. That results in a syntax error:
<script>
<script>
...
</script>

javascript eval() doesn't work in php file

The function in script section works very well but when i change it to file.php and open it on a server, the eval function doesn't work.
<?php
echo "
<!DOCTYPE html>
<html>
<body>
<button onclick=\"myFunction()\">Try it</button>
<script>
function myFunction() {
eval(\"alert('it worked')\") ;
}
</script>
</body>
</html>
";//end of echo
?>
Pay more attention.
eval(\"alert('it worked')\")
Must be coded:
eval("("+alert('it worked')+")").
The bracket "(" can make the code as a object instead of statement.
Some server may disabled the eval function for security reason,
Try to check whether eval function is enabled or not by making the following code
<?php
if(function_exists('eval')) {
echo "eval function is enabled";
}
else {
echo "eval function is not enabled";
}
?>
If you got the else part in the code, this would be reason for not executing your code.
Note: Your code works perfectly works for me. so I dont think there is issue in your code.

a javascript function called by javascript echoed by php doesn't response

I executed 2 javascript function, echoed by php code, in different ways. here is code.
<head>
<script>
aJavascriptFunction(){
document.write( 'php echo writted javascript to call another javascript function ,outside php echo, that write this html' );
}
</script>
</head>
<html>
<head>
</head>
<body>
<?php
echo "<script>document.write('php echo writted javascript to write this html')</script>"; //first case
echo "<br>";
echo "<script>aJavascriptFunction();</script>"; //second case
?>
</body>
</html>
first case, echoed javascript code that directly make function, works well. but second case that echoed javascript code that call another javascript function outside php echo doesn't work.
Could anyone explain why first case works well, but second case doesn't work?
I'm making php file that contain html, javascript and php code inside 'a' file index.php. I want to write html code basically by php echo function. What I'm trying to do is defining javascript functions, php functions separately to call from anywhere in html code(in same file) written by php echo function. I think that separating javascript and php code by separate file will be ultimate answer to my project. but for now, I'm not completely understanding how javascript and php code works in client and server. so, I need to complete my project using style above and remain understanding for latter.
Your code is kinda messy so it is hard to work with. But I will tell you this:
Syntax Error on line 14, unexpected <
It is an issue with your quotes and semicolons.
You have messed up your code.
<html>
<head>
<!-- Your script has to be inside head or body -->
<script type="text/javascript">
// Here is a valid javascript function
function myTestFunction(){
// Print inside browser console
console.log('I was executed.');
// Anyway if need to write
document.write('I was executed.');
}
</script>
</head>
<body>
<?php
echo '<script type="text/javascript">myTestFunction();</script>';
?>
</body>
</html>

error javascript method is not defined calling using php

I am unable to make a javascript function call through php on click of a button. I am placing the method before calling it in my php code. However, i get the error
Uncaught ReferenceError: pouchITSave is not defined on the console. what is strange about the error is that on the console it says line number profile.html:1. This is the previous file that make a call to the searchcode.php. I am not sure what is wrong here why would it say line number of a previous file when in reality the method is present in searchcode.php ? Can someone help?
Error:
Uncaught ReferenceError: pouchITSave is not defined profile.html:1
onClick
searchcode.php
<?php
ob_start();
header("Access-Control-Allow-Origin: *");
header('Content-type: ' . $image['mime_type']);
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" >
function pouchITSave(){
alert("PouchITSavecalled = ");
}
</script>
</head>
<body>
<?php
echo '<input type="submit" class="btn" id="'.$row['UniqueAdvertisingCode'].'" onclick="pouchITSave()" value="POUCH" >';
?>
</body>
</html>
Your javascript code must not be placed in searchcode.php. It must be placed at the top of the file calling searchcode.php.
The main reason behind this is the script must be loaded on the OnLoad event of your main file and if it was not loaded then the function will not be recognized by the interpreter.
In additional you must also echo those tags you've put on searchcode.php.
Try replacing your echo:
echo "<input type='submit' class='btn' id='".$row['UniqueAdvertisingCode']."' onclick='pouchITSave();' value='POUCH' >";
As you had a mixture of " and ', which I suggest you opt for " for outside quotations, easier to understand and maintain in my opinion, and make sure as Vhevhang said that you're retraiving the js code too, if not place it at the bottom of the page in your html file.

JavaScript jQuery inside PHP function

This is a theoretical question.
My question is whether a jQuery function or script can be written inside a PHP function.
E.g.
<?php function phpfunc(){
$a=10; ?>
<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>
Is this possible and legal?
Yes. It is possible and Legal one too. we generally use the same when we require any server side value to be set on client-side on runtime.
Hope this answers your query.
Thanks Much!
When php code is interpreted by the sever writing something like:
<?php
function foo()
{
<script type=text/javascript> ... </script>
}
?>
As part of the code in <?php ?> is interpreted as php and string inside the function doesnt represent any of php functions
You can echo javascript code (or any content of a HTML document) through your php code like:
<?php
function foo(){
echo "<script type=text/javascript> alert('it works!)'; </script>";
} ?>
so when you execute the function, you wil add the javascript to the document by echoing it and therefore execute it.
You can also use php variables to echo variables to javascript like:
<?php
function foo(){
echo "<script type=text/javascript> alert('{$phpVariable}'); </script>";
} ?>
or
<?php
function foo(){
echo "<script type=text/javascript> var variableFromPHP = {$phpVariable}; </script>";
} ?>
Yes, it's okay to do that. No, it's probably not a good idea. But there's nothing really stopping you.
Just be aware that if your variable happens to have a ' in it, you'll get messed up.
So, whenever you want to pass a variable from PHP to JavaScript, be sure to use json_encode:
<script type="text/javascript">
alert(<?php echo json_encode($something); ?>);
</script>
Note that there's no quotes in the JavaScript part - json_encode will add quotes if needed. This can be used to pass almost any kind of variable.
The short answer is no(edit : yes).
javascript is executed on the client and only on the client.
You can however echo javascript to the client.
So something like this :
$JSfunction = "<script>alert('This is working')</script>";
can be echoed to the page by doing echo $JSfunction;
edit :
Since you didn't mention where that function is located, I assumed you meant the PHP function on the server side.
To be clear, If it's written on the html page itself, it's perfectly fine and can be done.
complete answer
<? function phpfunc(){
$a=10; ?>
<script>var a ='<?php echo $a ?>'</SCRIPT> <?php } ?>
<?php phpfunc() ?>
<script>console.log(a);</script>
You must echo that $a
Yes you may use it like in normal html page. Use script include inside echo inside the php script.
You may use it outside the php script normally. Use container tag for the same in the php page.
I hope it would help.

Categories

Resources