我正在使用 Ajax 从数据库中填充下拉菜单,我的问题是我如何在单击上将其重定向到页面以及我从数据库中返回CARD_ID?
现在,当我单击它时,它会在搜索栏中显示卡名称,这是我想要的一部分,但现在我需要另一半重定向。我怎样才能做到这一点?
<?php
$mysqli = mysqli_connect('localhost', 'root', '', 'draftdb');
if(mysqli_connect_errno())
{
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$str = $_GET['content'];
if(strlen($str))
{
$sel = mysqli_query($mysqli, "select CARD_NAME, CARD_ID,CARD_TYPE from cards where CARD_NAME like '".trim($str)."%'");
if(mysqli_num_rows($sel))
{
echo "<table border ='"0'" width='"100%'">'n";
if(mysqli_num_rows($sel))
{
echo "<script language='"javascript'">box('1');</script>";
while($row = mysqli_fetch_array($sel))
{
$card_info = str_ireplace($str,"<b>".$str."</b>",($row['CARD_NAME']));
$card_type = str_ireplace($str,"<b>".$str."</b>",($row['CARD_TYPE']));
echo "<tr id='"word".$row['CARD_ID']."'" onmouseover='"highlight(1,'".$row['CARD_ID']."');'" onmouseout='"highlight(0,'".$row['CARD_ID']."');'" onClick='"display('".$row['CARD_NAME']."');'" >'n<td>".$card_info." ".$card_type."</td>'n</tr>'n";
}
}
echo "</table>";
}
}
else
{
echo "<script language='"javascript'">box('0');</script>";
}
?>
JavaScript。
subject_id = '';
function handleHttpResponse() {
if (http.readyState == 4) {
if (subject_id != '') {
document.getElementById(subject_id).innerHTML = http.responseText;
}
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
function getScriptPage(div_id,content_id)
{
subject_id = div_id;
content = document.getElementById(content_id).value;
http.open("GET", "script_page.php?content=" + escape(content), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
if(content.length>0)
box('1');
else
box('0');
}
function highlight(action,id)
{
if(action)
document.getElementById('word'+id).bgColor = "#C2B8F5";
else
document.getElementById('word'+id).bgColor = "#F8F8F8";
}
function display(word)
{
document.getElementById('text_content').value = word;
document.getElementById('box').style.display = 'none';
document.getElementById('text_content').focus();
}
function box(act)
{
if(act=='0')
{
document.getElementById('box').style.display = 'none';
}
else
document.getElementById('box').style.display = 'block';
}
目录
<div class="ajax-div">
<div class="searchbar">
<input type="text" onKeyUp="getScriptPage('box','text_content')" id="text_content">
</div>
<div id="box"></div>
</div>
我不确定你想做什么,但似乎你想要这样的东西:
1-更改显示行的方式,以便将ID也发送到函数:
echo "<tr id='"word".$row['CARD_ID']."'" onmouseover='"highlight(1,'".$row['CARD_ID']."');'" onmouseout='"highlight(0,'".$row['CARD_ID']."');'"
onClick='"display('".$row['CARD_NAME']."', " . $row['CARD_ID'] . ");'" >'n<td>".$card_info." ".$card_type."</td>'n</tr>'n";
^^^^^^^^^^^^^^^^^^^^^^^^^
2-更改display
功能以重定向
function display(word, id)
{
// document.getElementById('text_content').value = word;
// document.getElementById('box').style.display = 'none';
// document.getElementById('text_content').focus();
window.location.href = "some_page.php?card_id=" + id;
}
我已经注释掉了原始行,因为在您要离开的页面上做一些事情似乎没有多大意义。如果这是您正在寻找的解决方案,您也可以完全删除 word
参数。