简体   繁体   中英

unterminated string literal issue

First timer here, and total noob when it comes to PHP and JavaScript.

I have 2 lines of code which are identical, however one works and one kicks out a unterminated string literal error.

One that works:

<script type="text/javascript">
  document.getElementById('vehiclemake').value
                                         = "<?php echo $_POST['vehiclemake'];?>";
</script>

One that doesn't:

<script>
  document.getElementById('PostcodeSelect').value =
                                        "<?php echo $_POST['PostcodeSelect'];?>";
</script>

The only thing I can think of is that the postcode select POST variable has an underscore in it eg: AB1_1BA does this need escaping or something?

Any help greatly appreciated, cheers!

Consider using json_encode() :

document.getElementById('vehiclemake').value = <?php echo json_encode($_POST['vehiclemake']);?>;

It will take care to properly quote all special characters in your string.

For your stuff, If $_POST variable has chr(10) , it might be an issue too. So use trim() on the $_POST['PostcodeSelect'] .

<script type="text/javascript">
    document.getElementById('PostcodeSelect').value="<?php echo trim($_POST['PostcodeSelect']); ?>";
</script>

Might work! Best guess!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM