error javascript method is not defined calling using php - javascript

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.

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>

Uncaught reference error for echo function in php

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>

display php file content in the javascript of another php file

So basically i want to html() a php file but i seem to fail, maybe it isn't possible but i want to make sure.
This is an example code (not the real one):
page.php:
<? //some code generating html ?>
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<? include('php/content.php'); ?>");
</script>
php/content.php:
<div class='realContent'>Awesome Stuff Here</div>
Note: I know there is .load(), $.post and many others. I just want to know if this practic is possible, .html()-ing a php file's contents.
Thank you in advance, awesome community!
d3nm4k.
The code in my example does what you want.
Maybe you got the problem with the short opening tags that is < ? ? >.
The file I'm trying to load in my example is located in app/View/Tests/def.php and works just fine.
Hope it helped you!
<div class='content'></div>
<?php
$fileLocation = APP . 'View/Tests/def.php';
// Ensure the file exists
echo "FILE EXISTS: <b>" . (file_exists($fileLocation) ? "TRUE" : "FALSE") . "</b><br /><br />";
?>
<?php
// Alternatively, you can try this one
//include $fileLocation;
?>
<script type='text/javascript'>
$('.content').html("<?php include($fileLocation); ?>");
</script>
You can do it, the code you gave i tried it just by adding ?php
<div class='content'></div>
<script type='text/javascript'>
$('.content').html("<?php include('php/content.php'); ?>");
</script>
When the php is executed on server it replaces content with actual html inside html() function. And on client side javascript plays its role to render it.

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>

Categories

Resources