傳遞id元素數據庫我有以下的代碼,該代碼的工作原理:用ajax
<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>
現在我想重複同樣的過程,使用另一個單選按鈕,但這個時候,我希望新電臺的價值按鈕是用戶從<div id="txtHint"></div>
中選擇的。
這可能嗎?
這裏是我的後端getuser.php
文件:
<?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);
?>
任何意見是極大的讚賞。提前致謝。
感謝您的回覆,您能否告訴我您哪裏不清楚,我可以嘗試並更好地解釋它? – Annie