I have a php array of products that I want to move to javascript but when I json_encode it errors out giving me
Uncaught SyntaxError: Unexpected token ;
The debugger is giving me
var phpPro=;
and the code reads
var phpPro=<?php echo json_encode($pro)?>;
I have also encoded the array in the php script and tried to assign to js variable from there.
php =
$jpro=json_encode($pro);
js =
var phpPro=<?php echo $jpro;?>;
This code was actually working and now that I'm trying to copy it to another site it is not working. All the products are in the php recordset when displayed so I know they are there.
Related
I have an array defined in PHP:
$outofstock = array ( 0, 0, 0, 0, 0, 0, 1, 0 );
$_SESSION['outofstock']=$outofstock;
I try to pass it to a JS script this way:
var outofstock = <?php echo json_encode($_SESSION['outofstock']); ?>;
and I get an error with execution stop:
Uncaught SyntaxError: Unexpected token '<' VM563 newhed14.39.js:1
The same statement works in another script within an HTML file.
What am I doing wrong?
Thank you!
The fact that your file is named newhed14.39.js suggests to me that you're serving this particular file as a .js file statically, rather than through PHP. This means that PHP will not run - the PHP commands will end up literally in the file and will thus obviously end up as syntax errors. It's easy enough to check if this is the case - just open up the developer console on your browser and take a look at the file which threw the error; the PHP commands should be visible there (whereas they shouldn't be if PHP had processed the file).
The solution would be to move the variable into the dynamic output generated by a PHP script in a <script> tag, and to edit your JS to consume that variable (for example, by having the <script> tag call a function defined in the .js file).
the reason is that "PHP scripts do not run in .js files". you need to do this:
in your HTML document:
<input type="hidden" id="myArray" value="<?php echo json_encode($_SESSION['outofstock']); ?>">
then you can access this array in js like this:
var array = document.getElementById('myArray').value;
console.log(array)
from this way you access your PHP array in your js. hope it helps ...
I am using the following code in a seperate js file to use a php $_SESSION variable. However, I am getting a syntax error of SyntaxError: missing ; before statement. I have tried putting the ; in the usual place, but still the same.
What is the correct way to use a php session in js file. Thanks
var companycode = '<?php echo $_SESSION['ls_idcode_usr']?>';
There may be special characters in the value of the session variable. Use json_encode() to output a valid Javascript literal:
var companycode = <?php echo json_encode($_SESSION['ls_idcode_usr']);?>;
You can only use PHP in files ending in .php, otherwise the web server won't parse them looking for code to execute, and it'll just get passed down to the client.
You can certainly make a whatever.js.php file, where PHP will be involved in the production of the Javascript file that is passed down to the browser.
The approach used in this question is WRONG. As mentioned in comments, PHP is server side processing and JS is client side processing (browser). The 2 should not be mixed.
Seems like a trivial problem with an apparently easy solution. I have a PHP file which is loaded via a post. In this document I can retrieve the posted value as such:
$userid = $_POST["userid"];
Then in my Javascript in $(document).ready(function() I am trying to assign the post value to Javascript variable as such :
var jsvariable = <?php echo($_POST["userid"])?>;
Keep geeting either variable undefined in js error or syntax error, unexpected T_VARIABLE error inPHP.
Please advise how I can successful retrieve this value.
There are two approaches for this:
First if your js is present inside a php file then in that case.
var jsvariable = "<?php echo $_POST["userid"]; ?>";
And if your js is present in a .js file then in that case.
var jsvariable2 = "<?php echo $_POST["userid"]; ?>";
and below this line. Call the js file.
<script src="whatever.js" type="text/javascript">
And inside the js assign the above created variable to it:
var jsvariable = jsvariable2;
Hope it helps.
try
var jsvariable = <?php echo('\"'.$_POST["userid"].'\"')?>;
instead
var jsvariable = <?php echo($_POST["userid"])?>;
if userid="ehdrbs0318" in php,
in javascript
var jsvariable = ehdrbs0318;
you have to assign string like
var jsvariable = "ehdrbs0318";
You could use json_encode:
var jsvariable = <?php echo json_encode($_POST["userid"]) ?>;
This will add the necessary quotes if the PHP variable is a string, but will also respond well to non-string values.
As a side note: you can use the PHP short-cut notation to output values:
var jsvariable = <?= json_encode($_POST["userid"]) ?>;
PHP error
The error unexpected T_VARIABLE in PHP you (sometimes) get, is unrelated to the code you have provided: it is a simple syntax error, and it means the parser found a variable name at an unexpected place. Maybe you forgot to end the previous statement with a semicolon, or maybe you forgot a closing parenthesis or bracket....
I have built a webform with Laravel and have it working the way I want it to on my localhost. However, when I put it on the server my form stops recognizing my php commands. For example see below.
In my working model, my code looks like this.This is using javascript to return an array of data elements that I need for my project.
var pageDownloadInfo=<?php echo json_encode($project); ?>;
However when I put my form on the server an error occurs and it points to this in the console.
var pageDownloadInfo=<br />
and the console gives me this notice error:
Notice: Undefined variable: project in C:\xampp\htdocs\QPA Form\resources\views\pages\datatest.blade.php on line 278
null;
The forms are exactly the same. I am not sure what is causing this.
try using quotes around the echo statement as such
var pageDownloadInfo="<?php echo json_encode($project); ?>";
I'm using CodeIgniter form helper, and this is what it does:
Uncaught SyntaxError: Unexpected token ILLEGAL
The code:
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"') ?>');
As you can see, I'm using a PHP inside of a JS, when I do <?= 'Test' ?> it works.
So it seems like its related with the CodeIgniter function.
As far as i know this error message can be caused by unknown/wrong characters in the code, and from what I saw in the firebug, this CI function is generating text with tabs and line breaks... and that is my problem I guess.
I may be wrong, so please correct me if so.
I will appreciate any solution for this problem.
Chances are you're screwing up your quotes and you need to change the way you're passing that last parameter to the dropdown.
$class = 'class="span3"';
jQuery('#window-1').append('<?= form_dropdown("hfdata", $hf_arr, set_value("hfdata"), $class) ?>');
To explain how PHP and Javascript works: Php is executed on the server, this give html output. You browser will get that output and can do anything with it. From this point javascript can do his job.
You are echoing the php function call to the users browser. Javascript can't call the php function. If you want to do this, then you need to make a php file call that returnes the result you want. But I guess there is an other problem. ( if you want to do this, escape the ' characters)
You need to execute the PHP code on the server. This will give a result. You send this result to the client. On the clients PC javascript will execute your javascript code.
If you want to generate with PHP the javascript code, then you can do something like this ( in PHP on the server!):
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata')); ?> ');
If this isn't what you want, then you are working on a design problem. Then it's a kind of dead end.
I found the answer myself... As I said, it was caused by the HTML output that was returned from the from_dropdown.
The solution is simple, remove all unwanted characters like line breaks:
<?php
$prepare = preg_replace('/^\s+|\n|\r|\s+$/m', '', form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"'));
?>
jQuery('#window-1').append('<?= $prepare ?>');