我正在尝试使用我通过 PHP 在 JavaScript 中从 MySQL 获得的变量。我猜问题出在这一行:$speicher = $row['Vorname']
如何使用常规 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>