2012-08-15 82 views
-1

我使用Ajax从数据库中填充下拉菜单,我的问题是如何将onclick重定向到页面以及从数据库中取回的CARD_ID?PHP和AJAX onclick重定向

现在,当我点击它时,它显示了搜索栏中的卡名,这是我想要的一部分,但现在我需要另一半的重定向。我怎样才能做到这一点?

<?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'; 
} 

的HTML

<div class="ajax-div"> 

    <div class="searchbar"> 

    <input type="text" onKeyUp="getScriptPage('box','text_content')" id="text_content"> 

    </div> 

    <div id="box"></div> 
</div> 
+0

哪里可以找到它? – Ibu 2012-08-15 00:35:59

+0

“搜索栏”是?关于这个问题可能有些奇怪的事情之一 – 2012-08-15 00:37:05

+0

猜猜我也会添加HTML。 – Undermine2k 2012-08-15 00:39:16

回答

2

我不知道你正在尝试做的,但似乎你想是这样的:

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参数。

+0

谢谢,我会试试这个! – Undermine2k 2012-08-15 02:19:14