简体   繁体   中英

How to save mysql search result in variable

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>

只需将其作为javascript var的值

var yourjavascriptvar = <?php echo $row['Vorname']; ?>;

你忘了分号

  $speicher = $row['Vorname'];

You need to understand how php works. PHP pre-process page executing functions. When you use function "echo" it adds content to your webpage, which is send to browser and then executes javascript code.

In order to use values in javascript you need to echo them in php, like there where always there.

var jsValueName = '<?php echo $row["columnName"]; ?>';

Remember about enclosing with: ' '

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>";


}

There are somethings that might be wrong in this code...

First : A line in your code is missing a semicolon.

 $speicher = $row['Vorname']

Second : You are fetching your result ( s ) to one variable, $speicher.

Third : Your javascript is out of the HTML tags. It might don't work, but if it does is not a proper way to do it.

Fourth : The way you are using the PHP variable to use it in javascript seems good to me.

 var Ergebnis = '<?php echo $speicher;?>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM