PHP How output string to html - javascript

After print php string in js code I get error
<script type = "text/javascript">
var editorHtml = "<?php echo $editorHtml; ?>";
</script>
When the string contains " double-quotes, I get Unexpected token ILLEGAL, because the double-quotes close the JS string in the middle.
<script type = "text/javascript">
var editorHtml = '<?php echo $editorHtml; ?>';
</script>
I try this, but i get similar problem, if the string contains ' single-quotes.
I need a unuiversal solution for this, when the string contains ' AND " quotes.

<script type = "text/javascript">
var editorHtml = <?php echo str_replace(array(']]>', '<!'), array(']]\x3E', '\x3C!'), json_encode($editorHtml)); ?>;
</script>
Sanitizes in all cases properly.

Look an example:
<?php
$someVar = 1;
?>
<script type="text/javascript">
var javaScriptVar = "<?php echo $someVar; ?>";
</script>

Related

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 to assign a PHP variable to a JavaScript variable inside JavaScript code? The whole file is PHP though

-------------php code ends here-------------
<script type="text/javascript">
var e = <?php echo $tweetText; ?>;
var f = twemoji.parse(e);
document.write(f);
</script>
-------------php code starts here-------------
If I put single quotes or double quotes before and after then it matches any ' or " inside the tweetText and doesn't let the string print to screen.
This solution works fine, just add ,,addslashes" to escape quotes
<script type="text/javascript">
var e = '<?php echo addslashes($tweetText); ?>';
var f = twemoji.parse(e);
document.write(f);
</script>
<script type="text/javascript">
var e = '<?php echo $tweetText; ?>';
var f = twemoji.parse(e);
document.write(f);
</script>
That's it
The best way to do this would be with json_encode as #vivek said in a comment.
<script type="text/javascript">
var e = <?php echo json_encode($tweetText); ?>;
var f = twemoji.parse(e);
document.write(f);
</script>
Try to put the whole string inside doublequotes, then escape the same - double quotes - inside the javascript string, with a simple str function:
<script type="text/javascript">
var e = "<?php echo str_replace('"','\"',$tweetText); ?>";
var f = twemoji.parse(e);
document.write(f);
</script>

How to use a variable from php file to javascript file

How to use a variable from php file to javascript file
in php file I assigned the $phpVariable to jsVariable with this code:
<script>
var jsVariable = <?php echo $phpVariable; ?>;
</script>
in Javascript file: I test the jsVariable with this code:
<script>
alert( jsVariable );
</script>
but the output is ":undefined"
Thanks
Moi
If such variable is a string, you should wrap it in quotes:
<script>
var jsVariable = "<?php echo $phpVariable; ?>";
</script>
EDIT but you should never do that. You'd better inject it as a data-* attribute of a DOM element, or either fetch it from a JSON config file / string you'll be parsing, like:
<script type="application/json" id="json-config">
<?php echo json_encode($someObject); ?>
</script>
And then:
var config = $.parseJSON( $('#json-config').html() )
<?php
print "<script>";
print "var jsVariable = $phpVariable";
print "</script>";
?>

What is this Javascript Syntax Error

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.

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