Question: I have a php file with some javascript code in it, and i'm calling the file using the script tags in my HTML. My problem is when I have a get method in the php, the java function won't echo..here's my code:

```
Header("content-type: application/x-javascript");

        $monthS = $_GET['location'];

     // Connect to MySQL
         if ( !( $database = mysql_connect( "localhost",
            "root", "" ) ) )                      
            die( "Could not connect to database </body></html>" );

    // open Events database
         if ( !mysql_select_db( "Events", $database ) )
            die( "Could not open Events database </body></html>" );

            $result = mysql_query("SELECT * FROM posted_events WHERE Month_ = 'July' ") 
                    or die ('Error updating database because: '.mysql_error());;

            $daysArray = Array();
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $daysArray[] =  $row['DayNum']; }
            $length = count($daysArray);

            echo "function test() { ";
                echo "alert('monthS');";
                for($i=0; $i < $length; $i++) { 
            echo "var active = document.getElementById('day'+".$daysArray[$i].");
             active.setAttribute('class', active.getAttribute('class') + ' event');  ";
}       
            echo "}";

```

But if i remove `$monthS = $_GET['location'];`then the function echos perfectly, but i need that variable. And I got that variable from JS using JQuery.

Answer: 
Be **VERY** careful with this type of code. You're directly generating Javascript. **ANY** warnings or stray output from PHP will probably introduce syntax errors into the code, causing the JS interpreter to kill the entire script block.

You'd be better off generating some data that goes to pre-written Javascript, which can then work on the data, e.g.

```
$data = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $data[] = $row;
}
?>
<script type="text/javascript">
var data = <?php echo json_encode($data); ?>;

for (i in data) {
   ... do stuff with javascript data array
}

```

as it stands now, if you don't have a `location` parameter in the url for this script, you'll probably get a PHP warning about an undefined array key. That warning will become part of the JS code, and be treated as a syntax error.
You need to echo the PHP Get variable into javascript to assign the js variable.

```
echo "<script>var varmonthS = " . $_GET['location'] . ";";
echo "alert(varmonthS);</script>";

```