dsbx40787736 2013-05-06 20:13
浏览 60
已采纳

如何在变量中保存mysql搜索结果

I am trying to use the variable i got from MySQL via PHP in JavaScript. I am guessing the problem lies in this line: $speicher = $row['Vorname']

How can I use a regular PHP variable in order to store my search result and reuse it in javaScript?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org

/TR/html4/loose.dtd">
<html>
<head>
<title>Seite1</title>
<meta name="author" content="admin1212">
<meta name="editor" content="html-editor phase 5">
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#FF0000" alink="#FF0000" vlink="#FF0000">
<img src="http://lodsb.org/socialcomp/artemis/1405.jpg" onload= "tim()">
<input  type="button" value=" TAG1" id=" TAG1" name="select" style="height: 25px; width: 100px" size="100" onclick= "sel2(this)">
<input  type="button" value=" TAG2" id=" TAG2" name="select" style="height: 25px; width: 100px" size="100" onclick= "sel2(this)">
<input  type="button" value=" TAG3" id=" TAG3" name="select" style="height: 25px; width: 100px" size="100" onclick= "sel2(this)">
<input  type="button" value=" TAG4" id=" TAG4" name="select" style="height: 25px; width: 100px" size="100" onclick= "sel2(this)">
<input  type="button" value=" TAG5" id=" TAG5" name="select" style="height: 25px; width: 100px" size="100" onclick= "sel2(this)">
<input  type="button" value=" TAG6" id=" TAG6" name="select" style="height: 25px; width: 100px" size="100" onclick= "sel2(this)">


<input type = "text" id = "count" readonly="readonly" value="0" >
<input type = "text" id = "sel1" readonly="readonly" value="" >
<input type = "text" id = "sel2" readonly="readonly" value="" >

<?php   

    $speicher = "Hallo";



    // mit dem Datenbankserver verbinden
    mysql_connect("localhost", "root", "") or die (mysql_error());

    // Datenbank auswählen
    mysql_select_db("Menschen") or die(mysql_error());

    // SQL-Query
    $strSQL = "SELECT * FROM Personen";

    // Query ausführen (die Datensatzgruppe $rs enthält das Ergebnis)
    $rs = mysql_query($strSQL);

    // Schleifendurchlauf durch $rs
    // Jede Zeile wird zu einem Array ($row), mit mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {
      $speicher = $row['Vorname']
       // Schreibe den Wert der Spalte Vorname (der jetzt im Array $row ist)
      echo $row['Vorname'] . "<br />";



      }




    // Schließt die Datenbankverbindung
    mysql_close();


    echo "Success: ";
    echo date("r");





    ?>

<input  type="button" value="FUNKTION" id="button7" name="noselect" style="height: 25px; width: 100px" size="100" onclick= "a()">


</body>
</html>
<script type="text/javascript">
function Mauskontrolle (Element) {
  Element.value="777777777"
}

function sel2 (Button){
var counter = document.getElementById("count").value
var sel1 = document.getElementById("sel1")
var sel2 = document.getElementById("sel2")


if (counter==0){
sel1.value = Button.id
}
if (counter==1){
if (sel1.value == Button.id){ return }
sel2.value = Button.id
}
counter++
if (counter >=2){
counter=0
alert(sel1.value + " " + sel2.value)

}

document.getElementById("count").value  = counter

}
function emp(){
var arr = document.getElementsByName("select")
//alert(arr.length)
for(var i = 0; i < arr.length; i++){
arr[i].value=""
}
}
function tim(){
window.setTimeout(emp, 5000)
}
function a(){
var Ergebnis = '<?php echo $speicher;?>';
alert(Ergebnis);
}



</script>
  • 写回答

5条回答 默认 最新

  • dongxieting9623 2013-05-06 20:20
    关注

    This is a more complex question for a beginner, but normally you could use PHP to echo javascript, so you could possibly take your results array, loop through and echo out javascript ... here is a brief example to get you started.

    echo "<script language='javascript' type='text/javascript'>var resultsArr = new Array();</script>";
    
    
    while($row = mysql_fetch_array($rs)) {
    
          $speicher = $row['Vorname'];
    
          echo $row['Vorname'] . "<br />";
    
          echo "<script type='text/javascript'> resultsArr['Vorname'] = \"".$row['Vorname']."\" </script>";
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题