tags:

views:

72

answers:

2

I am trying to send a variable from a Javascript to a php script but what gets sent is just the first string and the rest is discarded.Dont know what i would be doing wrong. Here is my code:

<script type="text/javascript">
document.write(<li><a href=../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical Category 2>Historical Category 2 </a></li>)
</script>

My $_GET['category'] at the server side only prints Historical? Dont know what i may be missing or if there is a better way of passin data from a Javascript to PHP,i will appreciate.

A: 

The problem is with your URL - you havent encoded the spaces so it only picks up the first variable

../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical Category 2

should be

../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical+Category+2

or

../../../../projects/sungrant/view/HistoricalCategory2.php?category=Historical%20Category%202

prodigitalson
The attribute value also needs to be quoted.
David Dorward
+3  A: 

Either wrap your href attribute value in quotes or change the spaces to %20.

href="../../../../projects/sungrant/view/HistoricalCategory2.php
                                                 ?category=Historical Category 2"

or

href=../../../../projects/sungrant/view/HistoricalCategory2.php
                                              ?category=Historical%20Category%202

The reason it doesn't work with spaces is that in valid HTML, attributes are separated by spaces. If you need to use spaces in a HTML attribute value, make sure you wrap the string with quotes. If it's a URL, the browser will do the necessary URL encoding for you.

Andy E
or use `+` instead of `%20` as [prodigitalson](http://stackoverflow.com/questions/2798926/javascript-to-php/#2798940) mentioned.
Andy E