Case, when "document.ready" obstruct - php

I have php file, in this file I have this code:
<script language="JavaScript" type="text/javascript" src="jquery.js"></script>
<script language="JavaScript">
$(document).ready( function () {
var myvar = <?php echo json_encode($myvar); ?> ;
});
</script>
<script language="JavaScript" type="text/javascript" src="costum.js"> </script>
and in costum.js file I have code:
$(document).ready( function () {
alert(myvar );
});
this not working, error consol returns "myvar is undefined"
if in php file I write this (that is, without "document.ready")
<script language="JavaScript">
var myvar = <?php echo json_encode($myvar); ?> ;
</script>
in costum.js file, code alredy is working. Please tell why this happened?

try with
<script>
var myvar;
$(document).ready( function () {
myvar = <?php echo json_encode($myvar); ?> ;
});
</script>
your variable has to be declared as global (or in other words, in the outer scope) to be viewed from both document.ready functions.
As a side note language attribute is not necessary. Even type is not necessary (if you're using html5 doctype)

Your myvar is in the local scope of the ready-function. Move the var declaration outside to make it global and available to the other script.
However, as you just assign to a variable, you won't need to wait for DOMready anyway. Just use
<script type="text/javascript">
var myvar = <?php echo json_encode($myvar); ?>;
</script>
BTW, the language attribute is deprecated.

local variable inside the function is only visible in the function scope.
when you declare variable in the global scope, then it is the global variable.
You could expose it to global scope by:
$(document).ready( function () {
var myvar = <?php echo json_encode($myvar); ?>;
window['myvar'] = myvar;
});

Related

Declare variable in php file that will access in JS/jQuery

How can I declare a global variable in a PHP file and access directly in jQuery?
HTML
<script type="text/javascript">
var date_selected = <?php echo $from; ?>;
</script>
JS
$(function() {
alert("dsdsadasdas" + date_selected);
var date = date_selected;
var startDate;
var endDate;
});
Because on my test its not working.
You are missing the quotes
var date_selected = '<?php echo $a; ?>';

Java Script Function is not Defined

I am getting the following error from the chrome developer tool
Uncaught ReferenceError: searchRequests is not defined
searchProcess.php:174 onclick.
When I click on hyperlink produced from engine.php, I don't get the alert from the searchRequests function. I'm not sure what the problem is, I appreciate any advice given. Here is my code:
searchProcess.php
<?php
include '../include/engine.php';
?>
<html>
<head>
<script type="text/javascript" src="../jQuery.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
var instrID;
var cat;
$(window).load(function(){
});
var newheight = $(window).height();
function searchRequests(instr)
{
alert("in searchResults");
instrID = instr;
alert(instrID);
}
});
</script>
</head>
<body>
<?php
drawSearchResults($var1, $var2, $var3, $var3, $var4);
?>
</body>
</html>
engine.php
<?php
function drawSearchResults($var1, $var2, $var3, $var4, $var5)
{
while($row = mysql_fetch_assoc($result))
{
echo ("<tr>");
echo ("<td id='InstrumentID'><a href='javascript:void(0);' onclick='searchRequests($row[InstrumentID])'>$row[InstrumentID]</a></td>");
echo ("</tr>");
}
?>
The problem is that the function searchRequests is not in scope outside of the $(document).ready(). Move it outside of $(document).ready().
In general you shouldn't embed your javascript in the html. Much nicer:
$('#InstrumentID a').click(someFunctionThatIsInScope);
And you can put that code in the $(document).ready() block. In addition the function you call will get an event object that you can use to get any values you might need from the markup.
Because it is private. You are hiding it from global scope since it is inside the ready function. Do not use inline event handlers, use on() to attach events!

jquery and strings from php code

I have a problem how to pass for example $example=1; variable to this jquery like a value in bracket where is 20, 40, 60.....:
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(20);
$("#pb2").progressBar(40);
$("#pb3").progressBar(60);
$("#pb4").progressBar(70);
$("#pb5").progressBar(100);
$("#pb6").progressBar(100);
});
</script>
The main problem is that I want to example string have value from database, so that's why I need to make it outside of script.
If this code is in your .php page...
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(<?php echo $pb1 ;?>);
$("#pb2").progressBar(<?php echo $pb2 ;?>);
$("#pb3").progressBar(<?php echo $pb3 ;?>);
$("#pb4").progressBar(<?php echo $pb4 ;?>);
$("#pb5").progressBar(<?php echo $pb5 ;?>);
$("#pb6").progressBar(<?php echo $pb6 ;?>);
});
</script>
Where $pb1,$pb2,$pb3,$pb4,$pb5,$pb6 are Integers ..
This is simple:
<?php
//Insert your code to generate the values to $example
$example = ...
?>
// and here is your JS with php code inside it
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(<?php echo $example; ?>);
$("#pb2").progressBar(<?php echo $example1; ?>);
$("#pb3").progressBar(<?php echo $example2; ?>);
$("#pb4").progressBar(<?php echo $example3; ?>);
$("#pb5").progressBar(<?php echo $example4; ?>);
$("#pb6").progressBar(<?php echo $example5; ?>);
});
</script>
I hope it helps! Good Luck!
If you meant to pass php variable to client to dynamically replace numbers in jquery you can use like code in below
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(<?php echo example; ?>);
});
</script>
First off, having it in your database does not mean you cannot do so in the script. There are in fact several ways to do that. You could have your javascript file be php that generates javascript instead of html, or perhaps easier: you could use inline javscript. However, if you do want to keep the two separated it is still possible:
[..]
<script type='text/javascript'>var percentageDone = <?php echo intval($example) ?>;</script>
<script type="text/javascript">
$(document).ready(function() {
$("#pb1").progressBar(percentageDone);
});
</script>
[..]

calling javascript function from php

I am trying to call a javascript function from php. According to all of the examples I have been looking at the following should work but it doesn't. Why not?
<?php
echo "function test";
echo '<script type="text/javascript"> run(); </script>';
?>
<html>
<script type="text/javascript">
function run(){
alert("hello world");
}
</script>
</html>
Your html is invalid. You're missing some tags.
And you need to call the function after it has been declared, like this
<html>
<head>
<title></title>
<script type="text/javascript">
function run(){
alert("hello world");
}
<?php
echo "run();";
?>
</script>
</head>
<body>
</body>
</html>
In this case you can place the run before the method declaration, but as soon as you wrap the method call inside another script tag, the script tag has to be after the method declaration.
Try yourself http://jsfiddle.net/qdwXv/
the function must declare before use
it should be
<html>
<script type="text/javascript">
function run(){
alert("hello world");
}
<?php
echo "function test";
echo run(); ;
?>
</script>
</html>
As others have suggested, the function needs to be declared first. But, if you need to echo out the JavaScript from PHP first, you can either store it in a PHP variable to echo out later, or have your code wait for the dom to finish loading first...
document.ready = function() {
run()
}
If you're using jQuery or another framework, they probalby have a better way of doing that... In jQuery:
$(function(){
run();
})

JavaScript Redirection

Here is the code I have,
<script language="JavaScript">
var url = "http://localhost:8888/uploads/"+<? $name ?>+"/output.txt";
setTimeout("top.location.href = url",1000);
</script>
$name is a PHP variable which has the name of which directory I'm trying to redirect to.
Needless to say, it isn't working. What's wrong with it? I don't know a lot about javascript so I probably did something stupid
Thanks
If your mixing PHP with JavaScript, it's always advisable to check the output being sent to the browser: right click on your website and click view source!
JavaScript doesn't care whether the content being sent to it is static HTML, from a Database or generated by PHP. If its in the output, it'll parse it.
If you'd have done that, you'd notice that your not echo'ing the $name variable.
<script language="JavaScript">
var url = "http://localhost:8888/uploads/"+<? echo $name ?>+"/output.txt";
setTimeout("top.location.href = url",1000);
</script>
But that'd give you
<script language="JavaScript">
var url = "http://localhost:8888/uploads/"+ foo +"/output.txt";
setTimeout("top.location.href = url",1000);
</script>
Which isn't valid JavaScript, as foo is now a JS variable, not a string.
So you should have:
<script language="JavaScript">
var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
setTimeout("top.location.href = url",1000);
</script>
Furthermore, passing a string to setTimeout (or setInterval) is not recommend; for the same reasons against using eval(), so you should end up with something like this instead:
<script language="JavaScript">
var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
setTimeout(function () {
top.location.href = url
},1000);
</script>
Pass a function, not a string, to setTimeout.
var url = "http://localhost:8888/uploads/<?= $name ?>/output.txt";
setTimeout(function ()
{
top.location.href = url;
}, 1000);
I am assuming that the $name is properly filled in by PHP.
Try this:
setTimeout(function() { top.location.href = url; }, 1000);
Try like this:
var url = "http://localhost:8888/uploads/"+<? $name ?>+"/output.txt";
setTimeout(function() {
top.location.href = url;
}, 1000);
<?php echo "var url = 'http://localhost:8888/uploads/$name/output.txt'"; ?>
Remove pluses and braces - just put variable in url.
Also use Firebug to troubleshoot your JS code: http://getfirebug.com/
Use 'echo' or the slightly unreadable php short code <?= to echo out a variable.
<script language="JavaScript">
var url = "http://localhost:8888/uploads/<?php echo $name; ?>/output.txt";
setTimeout(function(){
top.location = url;
}, 1000);
</script>
I know I've created another closure, but feel it's more readable.
It should be:
window.location.href = url

Categories