What is this Javascript Syntax Error - javascript

I have this code which sends a variable to JS from a PHP file.
<script type="text/javascript">
var pids = new Array(<?php echo implode(', ', $pids); ?>);
var permalink = "My name is <?php the_permalink(); ?>";
</script>
For reasons beyond my comprehension, when take away "My name is", it's a syntax error.
<script type="text/javascript">
var pids = new Array(<?php echo implode(', ', $pids); ?>);
var permalink = "<?php the_permalink(); ?>";
</script>
There is no output, the script dies on.
[Syntax Error]
= new Array(67, 68, 69, 70, 71,
The first code was working, now has spontaneously stopped, despite no changes. Now, neither works.
I discovered the problem is the preceding code:
When the less-than sign is changed to less-than-or-equal to, there is a javascript syntax error. Somehow this must break the array or the implode function
$pids = array();
$i=0;
$result = count($wpdb->last_result);
while($i < $result) {
$pids[] = $wpdb->last_result[$i]->pid;
$i++;
}
?>
<script type="text/javascript">
var pids = new Array(<?php echo implode(', ', $pids); ?>);
var permalink = "My name is <?php the_permalink(); ?>";
</script>

try it:
var permalink = "My name is '<?php the_permalink(); ?>'";

Try this :
var permalink = "My name is " + <?php the_permalink(); ?>;

you probably have ' or " inside one of the php output functions, try to escape them.
this could happened also if one of your pids is a wrong type and implode fails to finish.

Related

How to pass json_encoded variables from .php to external .js?

that's my first post :)
I have a problem with passing json_encoded variables from PHP VIEW file to an external JS. I am using FuelPHP. The following is part of the VIEW:
1. These are the PHP variables:
<?php
$sensor_id_num = $sensor->id_num;
$sensor_name = $sensor->name;
$sensor_unit = $sensor->unit;
$sensor_lati = $sensor->lati;
$sensor_longi = $sensor->longi;
?>
2. Here the variables are json_encoded and their value is given to JS vars:
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript">
var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
var sensor_name = <?php echo json_encode($sensor_name); ?>;
var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
3. The above mentioned mapmarkers.js is the external JS that I want to pass the vars to. In that JS I am using google.maps javascript API to draw a map and one marker for each map. Every marker is representing a sensor's location, so that's why I'm passing latitude and longitude. That should be part of the php VIEW. That View shows some sensor's text info along with the map.
4. So the text info and the map are visualized, but not the markers. The problem is in the JS file. When I try to use the vars from tag into the JS, the browser console shows their value is 'undefined'. I'm accessing the vars with 'window.name_of_var'. Even when I access them without 'window.' their value is not shown, "Uncaught ReferenceError: sensor_lati is not defined" is shown instead. That's part of the JS:
var myLatLng = new google.maps.LatLng(window.sensor_lati,window.sensor_longi);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: window.sensor_name,
html: window.sensor_name
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(html);
infowindow.open(map, marker);
});
Does anybody has an idea where could the problem be? I don't have much experience with FuelPHP and JavaScript. Any help would be appreciated ;)
You cannot have a <script> element with a src attribute and javascript code within it. Well, you can, but it's pretty much undefined what browsers do with that so your results will vary from browser to browser.
The solution would be to first define the variables, then include the remote script:
<script type="text/javascript">
var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
var sensor_name = <?php echo json_encode($sensor_name); ?>;
var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript"></script>
Set the variables before calling the external js file
<script type="text/javascript">
var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
var sensor_name = <?php echo json_encode($sensor_name); ?>;
var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript">
You should divide your <script /> into 2 parts.
Probably you don't need json_encode php function here. Also add wrapping doublequotes.
mapmarkers.js must be added after defining your variables
<script type="text/javascript">
window.sensor_id_num = "<?php echo $sensor_id_num; ?>";
window.sensor_name = "<?php echo $sensor_name; ?>";
window.sensor_unit = "<?php echo $sensor_unit; ?>";
window.sensor_lati = "<?php echo $sensor_lati; ?>";
window.sensor_longi = "<?php echo $sensor_longi; ?>";
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript" />

Problems in passing PHP array to JavaScript

I understand that the format for passing a PHP array to Javascript is:
<script type="text/javascript">
var obj = <?php echo json_encode($php_variable); ?>;
</script>
I have a php function that stores some values in a longitude and latitude array. These array do hold the right values within php since print_r() within php shows me that the array is correct.
print_r($latitude_array);
print_r($longitude_array);
Now, I pass on this array to JS in this manner:
<script>
var lati_array = "<?php echo json_encode($latitude_array); ?>";
var longi_array = "<?php echo json_encode($longitude_array); ?>";
alert(lati_array[0]);
</script>
In the beginning, when I open the HTML file, it shows me an empty array (which is expected because the PHP arrays aren't filled yet). Then user enters something, the php arrays are filled up with longitudes and latitudes. These values should now be passed to JS. However, it doesn't alert anything after that. I can't be sure if array is successfully passed to JS. What am I missing?
Try this:
<script>
var data = <?php echo json_encode( $data ); ?>;
</script>
Try like below:
<?php
$array_var = array(111, 222, 333, 444);
?>
<script>
var array_var = "<?php echo json_encode($array_var); ?>";
console.log(array_var);
array_var = JSON.parse(array_var);
console.log(array_var);
alert(array_var[0]);
</script>
You are getting a string in lati_array , Try to convert it into json like this:
<script>
var lati_array = "<?php echo !empty($latitude_array) ? json_encode($latitude_array) : ''; ?>";
var longi_array = "<?php echo !empty($longitude_array) ? json_encode($longitude_array) : ''; ?>";
lati_array = JSON.parse(lati_array);
alert(lati_array[0]);
</script>

How can I use a php variable in javascript

If I have created a variable in php, say $test, how can I set a variable in javascript, say var test, to be equal to it.
I have already tried var test = <?php $test ?>
I guess
var test = <?php echo json_encode($test) ?>
The naive way var test = '<?php echo ($test) ?>' will fail if $test contains quotes or newlines, let alone is not of the string type (e.g. array, object)
var test = '<?php echo $test; ?>'
try like this :
var test = '<?php echo $test ?>';
var test = '<?php echo $test; ?>';
Or using shorthand echos, like this:
var test = '<?= test;?>';
var test = <?php echo json_encode($test); ?>
You can use
<pre>
var test = '<?php echo $test?>';
</pre>
below the definition of $test.
Change
var test = <?php $test ?>
to
var test = <?php echo $test; ?>
You are missing two things:
var test = <?php $test ?>
1) Echo statement.
2) Single Quotes around PHP snipplet.
So, the corrected code should be:
var test = "<?php echo $test ?>";
Within your page where you want your PHP to output to type:
var MyJavascriptVariable = <?php echo $myPHPVariable; ?>
Advise NOT to use short tags () as this can be disabled by webhosts and may break your code.

Run PHP echoed Javascript

I am just getting into the world of PHP, Javascript and HTML and would like some help from the community regarding the following code. Basically I want to pass variables plucked from a ODBC_connection by PHP into textboxes. Lines 1-3 were for me to test to get the box to update which it does but anything echoed by PHP does not run. I am completely new to this so I realize I must be missing something trivial.
I welcome any suggestions or comments about what I can do to fix this or what I can do better in general.
Thank you.
<script type='text/javascript'>
document.getElementById('modeltxt').value = "test2";
</script>
<?php
echo "<script type='text/javascript'>";
echo "document.getElementById('modeltxt').value =\"TEST3\";";
echo "document.getElementById('customertxt').value = $customer;";
echo "document.getElementById('endusertxt').value = $enduser;";
echo "document.getElementById(dongletxt').value = $dongle;";
echo "document.getElementById('shipdatetxt').value = $shipdate;";
echo "document.getElementById('chasistypetxt').value = $chasistype;";
echo "document.getElementById('chasisnumbertxt').value = $chasisnumber;";
echo "document.getElementById('opsystxt').value = $opsys;";
echo "document.getElementById('dvd1txt').value = $dvd1;";
echo "document.getElementById('dvd2txt').value = $dvd2;";
echo "document.getElementById('storagetxt').value = $storage;";
echo "document.getElementById('nodrivetxt').value = $nodrive;";
echo "document.getElementById('drivesizetxt').value = $drivesize;";
echo "document.getElementById('interface1txt').value = $interface1;";
echo "document.getElementById('interface2txt').value = $interface2;";
echo "document.getElementById('interface3txt').value = $interface3;";
echo "document.getElementById('interface4txt').value = $interface4;";
echo "document.getElementById('interface5txt').value = $interface5;";
echo "document.getElementById('interface6txt').value = $interface6;";
echo "document.getElementById('commentstxt').value = $comments;";
echo "document.getElementById('warrantyexptxt').value = $warrantyexp;";
echo "document.getElementById('extendedwarrantytxt').value = $extwarexp;";
echo "document.getElementById('onsitetxt').value = $onsite;";
echo "document.getElementById('sqlversiontxt').value = $sqlversion;";
echo "<\script>";
You can create dynamics JS using the below
Define Content-Type on the top of your .js.php file:
<?
header('Content-Type: application/javascript');
// Write your php code
?>
and call the js file like this ..
<script type="application/javascript" src="JS_PATH/name-of-file.js.php"></script>
and if you want to use inline php values, you can write like this
<script type="application/javascript">
document.getElementById('modeltxt').value = "<?php echo $dummy_value ?>";
</script>
No need to do this. You can simply use javascript inside php as below :-
As you mention in your question
Basically I want to pass variables plucked from a ODBC_connection by PHP into textboxes.
<?php
// Php block of code
?>
<script type="text/javscript">
document.getElementById('modeltxt').value = "TEST3";
document.getElementById('customertxt').value = "<?php echo $customer;?>";
......
....
...
</script>
<?php
// Another block of code
?>

Why this Javascrript Error Is coming at line 2 character 1

I have The Following Javascript Error
"Line 2 Character 1" coming in the entire app
I have researched Follwing thing in SOF i have found this link .
Javascript Line 1 Syntax error in IE
So my JS script is something like this
<script type ="text/javascript">
$(document).ready(function(){
var sv = "<?php echo $var1 ;>";
var sv1 = "<?php echo $var2 ;>";
var sv2 = "<?php echo $var3 ;>";
var sv3 = "<?php echo $var4 ;>";
var sv4 = "<?php echo $var5 ;>";
});
</script>
But when i put the same code under JSLINT it is saying "Problem at line 1 character 2: Expected 'html' and instead saw 'script'."
.How can I make by script corrected .Can any one please explain what iam doing wrong
You need to declare html tags first, and THEN you can start typing your script.
Wrap it in tags.
You're also having some weird PHP tags there, they need to be finished with ?>, not ;>
You aren't properly closing your PHP tags
<script type ="text/javascript">
$(document).ready(function(){
var sv = "<?php echo $var1 ?>";
var sv1 = "<?php echo $var2 ?>";
var sv2 = "<?php echo $var3 ?>";
var sv3 = "<?php echo $var4 ?>";
var sv4 = "<?php echo $var5 ?>";
});
</script>
Are you sending the PHP directly to the browser? If not, we'll need to see the code as it appears to the browser. If there was a double quote being printed, for example, it would cause syntax errors.
Also, there shouldn't be whitespace between your type attribute and the = character.

Categories