I always get this error trying to pass a variable from PHP code to javascript.
Expression expected
That's my code in Laravel 7.0 :
<script> let variable = #json($array); </script>
The code is still working. So I can use the variable $array in my javascript code. But it's always showing the error. Does anyone know how to fix this ?
You can use for example:
<script>
let variable = {!! json_encode($array) !!};
</script>`
Reference: https://laravel.com/docs/5.4/blade#displaying-data
Or if it still shows error then you can try:
<script>
let variable = [].concat(#json($array));
</script>
Related
In my template view file have code below:
<script>
// When the document is ready
$(function() {
var treedata = <?php (($treedata=="" ? "":$treedata)); ?>;
$('#treeview').treeview({data: treedata});
});
</script>
but I am not using this treeview in every page, so whenever no treedata being send the output will be like this
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: treedata
Filename: views/admin.php
Line Number: 355
How can I leave it blank when no treedata being send by controller?
You could use this approach.
var treedata = <?php echo isset($treedata) ? '"'.$treedata.'"' : "''" ; ?>;
echo to display the result.
isset() to determine if variable exists
wrap the result to avoid ReferenceError: a is not defined in javascript.
You've multiple options here. My preferred stance is generally to engineer your code so that you never have undefined variables. In the absence of that code structure, you might ensure it's set:
$treedata = isset( $treedata ) ? $treedata : "";
Or, you might consider disabling the NOTICE warning altogether. How you do this is situation dependent, but you might try something like:
ini_set('error_reporting', E_ALL & ~E_NOTICE);
Additionally, do you also have Javascript errors when $treenode is empty? I believe when $treenode is empty, your Javascript line turns into:
var treedata = ;
Which is an error. Suggest you wrap the quotes in PHP instead:
var treedata = <?php (($treedata=="" ? '""':$treedata)); ?>;
You should use isset() function for checking variable existance.
So, you should replace:
$treedata==""
To
isset($treedata)
isset() — Determine if a variable is set and is not NULL
I just want to be able to access variable userid in document.ready function. It's accessible in the JSP page but not in jQuery. I am able to print value of ${userid} on the same JSP page but not in jQuery. I have passed userid obj with model and view object to this JSP page. However I am not able to access that object.
$(document).ready(function(){
var examid = ${userid};
alert("Hello !");
//alert(username);
});
If the value of ${userid} is a string then you need to wrap it in quotes, otherwise you'll be getting a syntax error thrown in your JS code.
For example var examid = '${userid}';
– Rory McCrossan Apr 4 at 10:45
I am trying to develop a plugin to internet explorer browser using csharp and I try to inject a javascript to the loaded page.
To inject the javascript i used the following code. The code is injected and the alert is working fine.
but code given below should return the value of "msg" to output.
when i run this code i get null value for output. kindly help.
var output= HTMLDocument.parentWindow.execScript("msg()","JScript");
function msg(){
var msg = "This is sample";
alert(msg);
return msg;
}
According to this page:
https://msdn.microsoft.com/en-us/library/ie/ms536420%28v=vs.85%29.aspx
The execCode method returns some sort of null value. Use eval if you want the value of msg().
IE cannot eval functions (Presumably for security reasons).
The best workaround is to put the function in an array, like this:
var func = eval('[' + funcStr + ']')
Hello Im trying to pass value stored in php to javascript code.
I try to pass the value in $_SESSION[user]
If I have this script in my header:
<script>
var user = ? //How to pass the value?
</script>
In my buttons I do something like this:
onclick="foo('<?php session_start(); echo $_SESSION[user];?>')
But how do I pass it without the user click?
Thanks for helping
EDIT
My JS function located in other file and I reference them by this code in my header:
<script src="class_functions"></script>
How do I pass the same parameter to the other file?
Just echo the PHP value out like you would any content:
<script>
var user = '<?php echo $_SESSION[user]; ?>';
</script>
JavaScript is just client-side code like HTML is.
If you're PHP script is rendering the page, I would go with the script tag approach.
<head>
<script type="text/javascript">
var user = '<?php echo $_SESSION["user"]; ?>';
</script>
</head>
<body>
</body>
Now that you have added an additional factor to your question, it is a little bit more interesting. If you need to set a variable when the functions that use this variable are located in a different file, then you need to decide the scope of the variable you are passing. There are several options; I will just list two:
Global scope. That is, the variable will be "known" to the other functions because of where it it located. You want to be careful of this, but if that's your approach you can use any of the answers given, e.g.
<script>
var x = '<?= $_SESSION["user"];>';
</script>
Local scope. Now you will need to include a function in class_functions that sets the variable. You might end up with something like
<script>
set_user('<?= $_SESSION["user"];>');
</script>
Where the set_user() function is defined in your other file, and ensures that the variable is available to the other functions with the correct scope.
I would prefer using method 2 - it is much cleaner.
I think this is better:
<script>
var user = '<?php echo htmlentities($_SESSION["user"]); ?>';
</script>
Because if there are quotes $_SESSION['user'], you will get an error.
For example if you have:
$_SESSION['user'] = "something with 'quotes'";
the js would like so:
var user = 'something with 'quotes'';
Which is incorrect.
You Can use Jquery ,if you want it to happen automatically.
just Use the following code inside your tags
$(document).ready(function()
{
var user= <? echo $_SESSION['user']?>;
//Code Goes here......
});
Hope that solves Your Problem
Note:This can be one of the many possible solutions
i need to access a json file dynamically and am using the following code. In this case, 'bpicsel' and 'temp' are variables. The end result would be something like 'data[0].extit1'
var title="data["+bpicsel+"].extit"+temp;
$.getJSON('labs.json', function(data){
title2 = eval(title);
});
this works - but i am often told not to use eval - is there a better way?
This should work just fine
var title = data[bpicsel]['extit'+temp];
Now get your data as follows:
$.getJSON('labs.json', function(data){
var title = data[bpicsel]['extit'+temp]; //no need for eval here
});