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>
[..]
Related
I want to show data in jQuery while the data got from php in json_encode function.
There are three file in all.
json.html
<html>
<head>
</head>
<body>
<script type="text/javascript" src="jquery-3.0.0.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</body>
</html>
test.js
$(document).ready(function(){
$.getJSON("json.php",function(json){
alert(json.a);
console.log(json.a);
});
});
$(document).ready(function(){
function getA(){
$.getJSON("json.php",function(json){
echo json.$a;
})
}
}
json.php
<?php
header('Content-type:application/json');
$a=3;
echo json_encode($a);
?>
when I run the code,It shows as follows:
test.js:4SyntaxError: Unexpected identifier 'json'
Thanks to what you said, the former problem was solved.But something unbelieviable happens.
I have changed test.js as follows:
$(document).ready(function(){
$.getJSON("json.php",function(json){
document.write(json.a1.length);
document.write(json.a.length);
});
});
json.php
<?php
// header('Content-type:application/json');
$a = array('a1' =>aaaaa ,'b2'=>bb ,'c3'=>cc);
echo json_encode($a);
?>
This time,browser tells me 'a' is not an object ,but 'a1' is a object.Why did the elements not array passed to js?
if you want to get the output in your test.js then you should modify your code in json.php as follows
<?php
$data array('a' => 3);
echo json_encode($data);
json_encode($data) will output a {"a":3} which is a javascript object that which allow you to use your code in test.js
$.ajax({
method: "POST",
url: "json.php",
success: function(data) {
alert(json.a);// will alert 3
console.log(json.a);// will print 3
}
});
Replace echo json.$a; in test.js to alert(json.a);
echo is a PHP command and not JavaScript. See the example:
$(document).ready(function(){
$.getJSON("https://viacep.com.br/ws/89208540/json/",function(json){
console.log(json.localidade);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
</head>
<body>
</body>
</html>
In your json.php file use array to assign your value e.g,
<?php
$a=array('a'=>3);
echo json_encode($a);
?>
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();
})
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;
});
I have two $_SESSION variables impossible to access in any script of my page but it's sure they exist in the PHP code of the same page when I use echo to display their values.
I can display in jQuery every classical PHP variables I want with the following code, but it's impossible when they are $_SESSION variables :
<?php if( isset($_SESSION['id']) ){
echo $_SESSION['id']; // it displays the value
} ?>
<script type="text/javascript">
$(document).ready(function() {
$("#some_button").click(function(){
var a = <?php echo $_SESSION['id']; ?>;
alert(a);
});
});
</script>
I don't understand why honestly...
If you are using PHP 5.2.0 or later, change this:
var a = <?php echo $_SESSION['id']; ?>;
To this:
var a = <?php echo json_encode($_SESSION['id']); ?>
That will put quotation marks around the result if necessary and escape characters for JavaScript as needed.
If you want to use something earlier than PHP 5.2.0, you can do something like this:
var a = '<?php echo $_SESSION['id']; ?>'
Ideally, though, you'd want to use a regexp and/or escaping/replacing functions unless you know that $_SESSION['id'] will only have safe characters. json_encode() has that stuff baked in already, so it's preferable.
I don't know if the example is in the same page but i suspect you 're missing session_start() so you can't use session variables
It is working to me:
Ready and test the code below, it is quite similar to your code, but I think you forgot to call jquery api.
<?php>
session_start();
$_SESSION['id'] = 1;
if ( isset($_SESSION['id']) )
{
echo $_SESSION['id'];
echo json_encode($_SESSION['id']);
}
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#some_button").click(function(){
var a = "Result: "+ <?php echo json_encode($_SESSION['id']); ?>;
//var a="<?php echo $_SESSION['id']; ?>";
alert(a);
});
});
</script>
<form method="post" name="form" action="#">
<input type="submit" id="some_button" value="Clique" />
</form>
If its a string:
var a= "<?php echo $_SESSION['id']; ?>";
I'm drawing a complete blank why this isn't working. I can do it with one variable passing through, but not two. When I use actually numbers like getnt(1,2) it works. It's just not working with two PHP variables.
<script type="text/javascript">
function getnt(nid,pnid) {
window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
}
</script>
<body>
<?php
echo "<a href='#' onclick='getnt($nid,$pnid)'>VIEW</a>";
?>
</body>
I can make the code work with echo "<a href='nt.php?nid=$nid&pnid=$pnid'>VIEW</a>";, but that's no good if I want to add in alerts and javascript commands.
If the ID and pnID are strings, enclose them with brackets like this.
<body>
<?php
echo "VIEW";
?>
</body>
If still not working, You can debug your code
View the source code in browser,
make sure it generates correctly.
Put some alert messages in the
javascript function. Install Firebug
if you have Firefox or see
Javaascript console if you get any javascript errors.
You could always try:
<script type="text/javascript">
function getnt(nid,pnid) {
window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
}
</script>
<body>
VIEW
</body>
Your question is probably best answered by looking at the rendered HTML source.
In any case, here's how I'd do it using graceful degradation
<script type="text/javascript">
function getnt(element) {
var href = element.href;
var nid = element.getAttribute("data-nid");
var pnid = element.getAttribute("data-pnid");
return true;
}
</script>
<p><a href="nt.php?nid=<?php echo $nid ?>&pnid=<?php echo $pnid ?>"
data-nid="<?php echo $nid ?>"
data-pnid="<?php echo $pnid ?>"
onclick="return getnt(this)">VIEW</a></p>