I keep getting the above error referring to the second line of my code below and have no idea why! With the below part removed my project works ok with no problems but I keep gettig that error with it in, hence it wont run.
<?php
$myVar = $_POST['dropdown'];
?>
<script type="text/javascript">
var myVar = <?php echo '$myVar';?>
console.log(myVar);
</script>
This is probably your issue
var myVar = <?php echo '$myVar';?>
This should work, assuming you did want the single quotes around the PHP variable, and this also contains the javascript end-of-statement ; :
var myVar = <?php echo "'$myVar';";?>
If you did not want the single quotes then this should do :
var myVar = <?php echo $myVar . ';';?>
Related
I have the following code in a PHP page in ATOM:
<html>
<head>
<?php $testvar = "Test"; ?>
<script>
var test = <?php echo $testvar; ?>;
</script>
</head>
<body>
<p> This is a test. </p>
</body>
</html>
The page is doing exactly as planned on the PHP page but in Atom, everything after the </script> line is highlighting in red as in an error. What's going on here?
Here is a screenshot of the actual code I'm using. The above is an example but also has the same problem.
I have opened a Issue claim on the Atom support but I would like to try here as well to see if it's anything within the code.
Thanks!
Image
This is a bug in your code, not in Atom. Your echo statement produces an unenclosed string:
<script>
var test = Test;
</script>
This results in a JavaScript error ("Uncaught ReferenceError: Test is not defined") because you are telling the interpreter to use a variable named Test, but none exists.
You need to produce a properly enclosed string, which you can do with json_encode():
<script>
var test = <?php echo json_encode($testvar); ?>;
</script>
Based on your comment, you're actually trying to build an array, not just echo a string. You have this code:
var main_categories_array = [<?php echo '"'.implode('","', $main_categories_array ).'"' ?>];
If your array contains any quotation marks, like the simple "Test" example, you will get errors. So, your code should be:
var main_categories_array = <?php echo json_encode($main_categories_array); ?>;
That was an actual bug fixed in 1.13, released yesterday : 10th of January.
Sources :
https://github.com/atom/atom/issues/13532
http://blog.atom.io/2017/01/10/atom-1-13.html
This is a bug with Atom.
Posted here: https://github.com/atom/atom/issues/13532
I have a variable named $path. I want to pass this variable from PHP to a javascript function.
<button onclick='myFunctionContact(\"" . $row1['id'] . "\")'>
<img border='0' alt='Contacts' src='".$imgpth."peoplesmall.png'>
</button>
<script>
function myFunctionContact(id) {
window.open('!!!$path should go here!!!'+id, '', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
How do I get the URL in path to display inside of the function, in the desired place?
I tried printing the variable into a javascript variable and then placing that variable into the function, but the popup window no longer works when I do that.
function myFunctionContact(id) {
var test1 = <?php echo $path; ?>;
window.open(test1 +id, '', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
I Know I am doing it wrong, but I have no idea how. Any advice would be greatly appreciated.
I think the problem is how you are echo the path:
Instead of:
var test1 = <?php echo $path; ?>
i think it should be
var test1 = <?php echo '"'.$path.'";'; ?>
You can always use a hidden input field, and set it's value to whatever you need to be used in your JS code, then grab that value of in your JS, or maybe try an ajax call to get the value you need.
json_encode() fixed the problem.
var myValue = <?php echo json_encode($path); ?>;
The path needs to be a quoted string. The end result of your echoed string has to, itself, contain quotes.
Assuming $path is a string, window.open is expecting a quoted string as the parameter.
function myFunctionContact(id) {
window.open(' . $path . ' + id, '', 'toolbar=no,scrollbars=yes,resizable=yes,top=200,left=500,width=400,height=400');
}
</script>
just a simple question, can we assigned codeigniter open_form() to javascript var?
i have a code like this:
var openForm = '<?php echo form_open("controller/some_function",
array('class' => 'class_name', 'enctype' => 'multipart/form-data'));?>';
but when i run it, i got error in my console saying:
Uncaught SyntaxError: Invalid or unexpected token
but, when i try this:
var closeForm = '<?php echo form_close(); ?>';
it didn't show any error.
though i guess it's not about syntax error, i still have no idea what is wrong and what happens. can anyone explain?
Yes. You can use like this
var openForm = `<?php echo form_open("controller/some_function", array("class" => "class_name", "enctype" => "multipart/form-data")); ?>`;
openForm += '<?php echo form_close(); ?>';
$("#your_element)id").html(openForm);
In javascript You can't split a string across multiple lines. <?php echo form_open(); ?> add \n at the end which create Syntax error.
As well <?php echo form_open(); ?> adds double quotes, which also ends into escaping issues.
To avoid issues In such cases you can use the template literals which is `
For more details you can visit this site.
I hope it justifies your query.
I am trying to get a PHP session variable into an external JS file. In the JS file, I want to read and use the value of the session variable, but also assign a new value to the Session variable.
I tried this :
php file:
<?php session_start();?>
<html>
<head>
<title></title>
<script src="testjs.php" type="text/javascript"></script>
</head>
<body>
<?php $_SESSION['Count'] = 1; ?>
</body>
</html>
javascript file:
<?php header("Content-type: application/javascript"); ?>
alert("I am an alert box!");
var test = <?php echo $_SESSION['Count'] ?>;
alert(test);
<?php $_SESSION['Count'] = 2 ?>
test = <?php echo $_SESSION['Count'] ?>;
alert(test);
But I keep getting an empty value and my chrome inspector keeps giving me this error: Uncaught SyntaxError: Unexpected token ; for this line: var test = ;
Help needed :)
You need to start the session in the js(php) file:
<?php
session_start();
header("Content-type: application/javascript"); ?>
alert("I am an alert box!");
var test = <?php echo $_SESSION['Count'] ?>;
alert(test);
<?php $_SESSION['Count'] = 2 ?>
test = <?php echo $_SESSION['Count'] ?>;
alert(test);
This is because the browser makes a separate request to this file, as script tag is not the same as a php include.
NOTE interspersing php and js like this is not a good idea. Although the code above will work, it will blur the mental line between serverside (php) and client side, which can lead to broken code like the following:
var abool = false;
var test = <?php echo $_SESSION['Count'] ?>;
if(abool){
<?php $_SESSION['Count'] = 2 ?>
}
var test = <?php echo $_SESSION['Count'] ?>;
alert(test);
In the above, test will = 2, because the php code is executed before the javascript, so the js if block has zero impact on the php, which is essentially:
<?php
echo $_SESSION['Count'];
$_SESSION['Count'] = 2;
echo $_SESSION['Count'];
Looking at your php file, you have a rogue ; at the end of this line:
<?php $_SESSION['Count'] = 2 ?>;
The output would just be:
;
Move the semicolon and you're fine:
<?php $_SESSION['Count'] = 2; ?>
EDIT: It looks like you're not opening the session in your JS file... You're not using require() or include() so I imagine you should start with session_start() before sending headers.
You need to call session_start(); before you access $_SESSION variables. Reference to "php" file in <script> tag makes another separate request to the server.
http://php.net/manual/en/function.session-start.php
you can't call php code in js file ...
php will work when the file extension is php...
for this you need to do :
create/assign javascript variable/object in php file and access it in js file
Take in mind the following; the code below and programs associated at it is runs perfectly.
<script>
window.vorderby = "YEAR"
exibelivrosAJAX();
</script>
but, when I did the modify below gave me the following error: Uncaught ReferenceError: YEAR is not defined
<script>
window.vorderby = <?php echo $_POST['formorderby']; ?>;
exibelivrosAJAX();
</script>
Looking for and reading tons of messages I did the following:
<script type="text/javascript" src="funcoesJS.js">
window.vorderby = <?php echo $_POST['formorderby']; ?>;
exibelivrosAJAX();
</script>
and the error was solved. But, the function exibelivrosAJAX() don't run.
Below you can see the two pieces of code that I think can help you to understand a little better.
1st piece of code in the primary file:
echo " ";
2nd piece of code in another php file:
window.vorderby = ;
exibelivrosAJAX();
Could you help me to understand it? Thanks a lot! Marcos.
You still need the JS quotes:
<script>
window.vorderby = "<?php echo $_POST['formorderby']; ?>";
exibelivrosAJAX();
</script>
In the 2nd example, your code is not executed [and therefore you get no error] because of the src attribute of the script tag.
You forgot to enclose the outputed variable with qoutes
window.vorderby = "<?php echo $_POST['formorderby']; ?>";