我想通過JavaScript在JavaScript中使用從MySQL獲取的變量。我猜測問題出在這一行:$speicher = $row['Vorname']
如何將mysql搜索結果保存在變量中
如何使用常規PHP變量來存儲我的搜索結果並在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>
該代碼看起來不錯..你有錯誤或什麼? – 2013-05-06 20:15:52
請注意,您的JavaScript不在HTML標記中。 – 2013-05-06 20:21:27
@explosion丸:是啊其實我得到一個錯誤:解析錯誤:語法錯誤,意外'回聲'(T_ECHO)在C:\ xampp \ htdocs \ Seite2.php在線47 – user2352375 2013-05-06 22:01:34