SyntaxError: unterminated string literal var address = " - javascript

I've this problem at the var address line, i think to have write all correctly or not?
<?php for ($i = 1; $i <= count($data); $i++) { ?>
var address = "<?php echo $address[$i].','.$city[$i].','.$region[$i] ?>";
alert(address);
<?php } ?>

You're generating javascript with php and the error you get comes from the javascript part, not php. I guess that one of your variables like $address contains something that isn't valid in js strings, like a newline. The best practice is to use json_encode to encode values for use in javascript:
var address = <?php echo json_encode($address[$i].','.$city[$i].','.$region[$i]) ?>;

Related

How to preserv php string to javascript

I am trying to copy a string in a php array to localStorage and the string is a date in this format: 2016-06-15
But the string becomes calculated before stored.
How do I preserv it as a string?
Example:
<?php
$test = array('2015-10-05','20151005');
echo $test[0]."<br>";
echo $test[1];
echo '<script>localStorage["test1a"] = '.$test[0].';</script>';
echo '<script>localStorage["test2a"] = '.$test[1].';</script>';
?>
<script>localStorage["test1b"] = "2015-10-05";</script>
<script>localStorage["test2b"] = "20151005";</script>
This will output:
2015-10-05
20151005
And in localStorage:
test1a 2000
test1b 2015-10-05
test2a 20151005
test2b 20151005
<?php
$test = array('2015-10-05','20151005');
echo $test[0]."<br>";
echo $test[1];
echo '<script>localStorage["test1a"] = "'.$test[0].'";</script>';
echo '<script>localStorage["test2a"] = "'.$test[1].'";</script>';
?>
<script>localStorage["test1b"] = "2015-10-05";</script>
<script>localStorage["test2b"] = "20151005";</script>
Note the Additional Quotes added '"' wrapping the array variable.
To explain why it does not store the value but calculate it is because you are throwing a string at browser and it interpretes as HTML or Javascript in this case.

Receiving and sending back json data in PHP

I am sending data from services.js coming in as a json encoded string in the following format to myPhp.php:
{"Name":"alpha"}
I want to gather this data and send back to the services.js as it came back in json form adding beta to the string as in:
{"Name":"alpha beta"}
myPhp.php
<?php
$a = $_POST[data];
$b = json_decode($a);
$c = $b.Name + "beta";
echo ($c);
?>
The "." notation is not used in PHP to access / set properties - more like Javascript and you need to have quotes around the $_POST variable ~ unless data is defined as a constant. I think you could try something like this though.
<?php
$b = json_decode( $_POST['data'],true );
$b['name'].='beta';
echo json_encode($b);
?>
<?php
$a = $_POST[data];
$b = json_decode($a);
$c = $b['Name'] . " beta";
header('Content-Type: application/json');
echo json_encode($c);
?>

Array of Javascript in PHP

I was trying to get datas from the database and put them into the array in Javascript but Javascript is not working in PHP command area.
Here is the whole PHP codes;
<?php
mysql_connect("mysql.metropolia.fi","localhost","") or die("ERROR!!");
mysql_select_db("localhost") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB");
$sql = mysql_query("SELECT * FROM METEKSAN_HABER_CUBUGU");
$haber = 'haber';
$list = array();
$i=0;
while($rows = mysql_fetch_assoc($sql)){
$list[] = $rows[$haber];
$i++;
}
echo $i;
echo '<script type="text/javascript">
var yazi=new Array();';
echo $i;
for ($k = 0 ; $k < $i ; $k++){
echo 'yazi['.$k.']="'.$list[$k].'';
}
echo '</script>';
?>
But when it comes to;
echo '<script type="text/javascript">
var yazi=new Array();';
this command line, the problem begins. Though I write 'echo $i;' after that command, I get nothing on the screen but I get the result if I write before that command. So, it means that everything works well before that command. What you think about the problem ? Why can't I starting the Javascript command ? Am I writing something wrong ?
Please give me a hand.
Thanks.
UPDATE;
I opened the web source and yeah it exactly seems there is a problem. So, I think it's better to ask that how can I write
<script type="text/javascript">
/*Example message arrays for the two demo scrollers*/
var yazi=new Array()
yazi[0]='METEKSAN Savunma, Yeni Dönemin Örnek Oyuncusu Olmaya Hazır'
yazi[1]='METEKSAN Savunma Bloomberg TVde'
</script>
this Javascript code in PHP ??
You can see my output at http://users.metropolia.fi/~buraku/Meteksan/index.php
try something like this
while($rows = mysql_fetch_assoc($sql)){
$list[] = ''.$rows[$haber].'';
}
$js_array = json_encode($list);
echo "<script>var yazi = ". $js_array . ";</script>";
It seems you are executing it currently in your browser? Then you should find your second output when opening page source, because your browser tries to executes the output as JS code. If you execute it on cli, everything should work as expected.
EDIT based on your comment:
Bullshit i wrote before, obviously. Viewing line 122 of your current html shows me a problem with your quotation marks. try the following:
for ($k = 0 ; $k < $i ; $k++){
echo 'yazi['.$k.']=\''.$list[$k].'\';';
}
In the end you should try to avoid using this kind of js rendering at all. The json_encode proposal of jeremy is the correct way to go.
You may have much more compact code:
....
$list = array()
while($rows = mysql_fetch_assoc($sql)) {
$list[] = $rows[$haber];
}
echo '<script type="text/javascript">' . "\n";
echo 'var yazi=';
echo json_encode($list,JSON_HEX_APOS | JSON_HEX_QUOT);
echo ";\n";
echo '</script>' . "\n";
What is this doing:
There's no need to count the added elements in $i, count($array) will give you the cutrrent number.. But it's not needed anyway.
Put some newlines behind the echo, better readable source
json_encode will format an JSON array from your php array, which can be directly used as source code.

using php _GET within javascript function

I have some javascript embedded into an html file that I am running in a browswer.
document.getElementById('home-search-text-inp').value = <?php echo htmlspecialchars($_GET['search_for']); ?>;
Why does this not fill the textbox?
Note that:
document.getElementById('home-search-text-inp').value = "hi";
puts "hi" into the textbox and:
<?php echo htmlspecialchars($_GET['search_for']); ?>
writes text just fine.
Thanks in advance
You're missing quotes around your string value:
document.getElementById('home-search-text-inp').value = <?php echo htmlspecialchars($_GET['search_for']); ?>;
^^^^ ^^^^
HERE HERE
should be:
document.getElementById('home-search-text-inp').value = "<?php echo htmlspecialchars($_GET['search_for']); ?>";

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.

Categories