-4

I am trying to load a file which is being uploaded using PHP script. I have assigned a variable for the location of the uploaded file $tra = basename( $_FILES['userfile']['name']); and trying to give it as a input for the java script

<script type="text/javascript">
var a = <?php echo $tra; ?>;   
</script>
<script type="text/javascript" src="C:/xampp/htdocs/new/jmol/Jmol.js"></script>

But its showing an error Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\example.php on line 30 and line number 30 is

<script type="text/javascript">
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Pari.
  • 27
  • 1
  • 7
  • Could you show us more of the surrounding JS code? There is probably an extra angle bracket in there somewhere. – Matt Gibson Jul 10 '12 at 11:52
  • When you will start making things perfect, these type of error will automatically solved. Start with adding correct tags in question. – TeaCupApp Jul 10 '12 at 11:53
  • 2
    A parse error like this is usually an indication that there's something wrong _before_ the identified line. It gave up parsing when it got to something it didn't recognize, but it didn't recognize it because of what came before. – David Jul 10 '12 at 11:53
  • Exact duplicate of: http://stackoverflow.com/questions/3306314/how-to-pass-a-php-variable-to-javascript – Nikola K. Jul 10 '12 at 11:54
  • 1
    @NikolaK.: It's not _quite_ exact. This person is having problems with PHP syntax, entirely outside of the intended goal of emitting a value to JavaScript (which should work fine here if his other PHP code were made syntactically correct). – David Jul 10 '12 at 11:56
  • 1
    Hey this is the current weekly update of that ever-recurring question… – feeela Jul 10 '12 at 12:02
  • 1
    @NikolaK. = close but as David pointed out this is a syntax issue rather than "I have no clue at all". – Kev Jul 10 '12 at 22:52

1 Answers1

1

try this:

<script type="text/javascript">
var a = '<?php echo $tra; ?>';   
</script>

You need to enclose with '' to asign a string to a javascript variable. If you are trying to assign a number it should work without the quotes, unless there's an error in your PHP.

turbogp20
  • 51
  • 3