0

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!

4

2 に答える 2

0

使用を検討してくださいjson_encode()

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

文字列内のすべての特殊文字を適切に引用するように注意してください。

于 2012-06-07T09:10:27.133 に答える
0

$_POST変数に がある場合chr(10)、それも問題になる可能性があります。で使用trim()$_POST['PostcodeSelect']ます。

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

うまくいくかもしれません!最良の推測!

于 2012-06-07T09:13:09.917 に答える