0

I have the following code, that works:

<script>
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) {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
 }
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>

<body>

<?php include 'db_connector.php';

$result = mysqli_query($con,"SELECT * FROM scope");

while($row = mysqli_fetch_array($result)) {

$row['name'];

echo  "<div class='toggle-btn-grp cssonly'>
<div><input type='radio' name='os' value=".$row['name']." id='myRadio' onchange='showUser(this.value)'>      <label  class='toggle-btn'>".$row['name']."</label></div>

</div>";

            }


 ?>

 <p>Click the "Try it" button to display the value of the radio button.</p>

 <button onclick="myFunction()">Try it</button>

 <div id="txtHint"></div>

 </body>

Now I want to repeat the same process, using another radio button but this time I want the value of the new radio button to be what ever the user selects from <div id="txtHint"></div>.

Is this possible at all??

Here is my backend getuser.php file:

<?php
$q = strval($_GET['q']);

echo "<center><b>Scope ".$q."</b></center><br><br>";

include 'db_connector.php';

mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM os WHERE scope = '".$q."'";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {  

echo "<a href=''>ID: ".$row['id']."</a><br>";
echo "<a href=''>Name: ".$row['name']."</a><br><br>";

}

mysqli_close($con);


?>

Any advice is greatly appreciated. Thanks in advance.

eightShirt
  • 1,457
  • 2
  • 15
  • 29
Annie
  • 33
  • 6

1 Answers1

0

You should look at this answer.

StackOverflow answer: Getting selected text in a browser, cross-platform

From what you wrote, I'm guessing that you want to make a new radio button based on the text that the user selects from the contents of the txtHint div.

The problem is that whatever you employ with normal javascript might be difficult to do in a cross browser way.

And maybe you're learning javascript, try using jQuery, specially for the AJAX...it's awesome!

The following answer also suggests the use of jQuery plugin and the wrapSelection plugin that does it in a cross browser supported way!

Community
  • 1
  • 1
ShivamMax
  • 75
  • 9
  • Thank you so much, I'll have a look. – Annie Nov 14 '14 at 10:02
  • @Maxouhell, @Liarez What am looking for is creating a variable lets say php using `
    `, so something like `$str =
    ;` this is what am trying to achieve. Is this possible, if so how?
    – Annie Nov 14 '14 at 10:34