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

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>

Related

Add JavaScript in head

I understand that good practice is to add the .js files in head of HTML.
My problem is that one of my .js files (or code+script) also has a beginning function, and it looks something like this (and all this is now in head):
<?php
// Countdown timer
function timer($date, $id){ ?>
<script>
(function()
{
// random stuff
}
</script>
// end function timer
<?php } ?>
How should I include/require or <script> src=" x "</script> this kind of file in the head of my HTML? I have hard time figuring out how to add this file in the proper way because of the beginning function.
First of all, replace this:
<script> src=" x "</script>
with this:
<script src=" x "></script>
Now if I understand it correctly you want to include a .php file into the HTML header of your page, and this .php file should include a js script.
Something like:
index.html
<head>
<title>My Weird App</title>
<?php include "myphpfile.php" ?>
</head>
myphpfile.php
<?php
some_func() { ?>
<script src="myjsfile.js"></script>
<?php } ?>
myjsfile.js
function some_other_function() {
//do stuff
}
If you want to execute some javascript code when your php function gets called. This would do, but if you're hoping to be able to use some value returned from php on javascript, or the other way around, then it won't work as you expected. The preferred method of client/server communication is AJAX.
If you want to include the .js file based on a condition, this is done better by:
myphpfile.php
<?php
some_func(some_params) {
if(some_cond) {
return "<script src="myjsfile.php"></script>\n"
} else {
return "";
}
}
?>
then on index.html
<head>
<title>My not so weird app</title>
<?php
include 'myphpfile.php';
echo some_func(some_arguments);
?>
</head>
Well, I hope that helps, if I didn't get it, please comment, (or make your question clearer).

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>

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.

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.

Is it possible to put a javascript function in a php file?

Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>

Categories

Resources