I have this JavaScript function which is getting a value from a select option in HTML:

```
<script type="text/javascript">

function showUser(str) {
if (str=="") {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
    var str=xmlhttp.responseText;
    var splitstr=str.split('||');

    document.getElementById("txtHint").innerHTML=splitstr[0];
    document.getElementById("txtval").innerHTML=splitstr[1];
    }
  }
xmlhttp.open("GET","getdetails.php?q="+str,true);
xmlhttp.send();
}

</script>

```

Now, `str` is the JavaScript variable I want to take its value and put it into a PHP variable.

I am using this, but it is not working:

```
$grade = "<script language=javascript>document.write(str);</script>";
echo $grade;

```

What is the correct way to do this?