Waqas Khan Waqas Khan - 3 months ago 17
HTML Question

Javascript not working in php

Hello guys i am trying to embed javascript to the php file navigator user agent but i am facing this error :

Parse error: syntax error, unexpected '?' in /home/public_html/domain.com/modules/mod_news_pro_gk4/tmpl/layout.parts.php on line 874

here is the code

echo '<script type="text/javascript">
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){

document.write('<?php return ($news_price != '') ? '<div class="nspVmStore">'.$news_price.'<span class="extxt">.00 / Only</span></div> ' : ''; ?>') ;

}

else
document.write("<?php return ($news_price != '') ? '<div class="nspVmStore">'.$news_price.'<span class="extxt">.00 / Only</span></div> ' : ''; ?>");
</script>';

Answer
<?php
$val_1 = ($news_price != '') ? '<div class="nspVmStore">'.$news_price.'<span class="extxt">.00 / Only</span></div> ' : '';
$val_2 = "value 2 here";
?>
<script  type="text/javascript">
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent))
    document.write("<?php echo $val_1;  ?>");
else
    document.write("<?php echo $val_2;  ?>");
</script>
Comments