0

I'm trying to display data from my database based on a select form. I'm using js and php to achieve this.

In my header I have the js:

<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser.php?q="+str,true);
  xmlhttp.send();
}
</script>

The code for the select page:

 <?php 
 include 'header.php'; 
 mysql_connect("#####", "#####", "########") or die(mysql_error());
 mysql_select_db("######") or die(mysql_error());
 $result = mysql_query("SELECT * FROM medlemmer");

 ?>

<div class="row">
    <div class="col-md-4">
        <form>
            <select name="users" onchange="showUser(this.value)">
            <?php
            $i = 0;
            while($row = mysql_fetch_array($result))
              { if (!$i++) echo "<option selected='selected'>Vælg medlem</option>" ?>

                <option value="<?php echo $row[medlemmer_id] ?>"><?php echo $row[medlemmer_navn] ?></option>

                <?php } $i++; ?>
            </select>
        </form>
    </div>
    <div class="col-md-8">
        <div id="txtHint">
            <b>Person info will be listed here.</b>
        </div>
    </div>
</div>  

And finally the php code which should return the query results from the selection:

<?php
$q = intval($_GET['q']);
mysql_connect("######", "#######", "######") or die(mysql_error());
mysql_select_db("######") or die(mysql_error());
$result4 = mysql_query("SELECT boder_boder_id, medlemmer_medlemmer_id, medlemmer_navn, medlemmer_id, boder_navn, boder_id, boder_pris FROM boder_has_medlemmer, medlemmer, boder WHERE boder_boder_id = boder_id AND medlemmer_medlemmer_id = medlemmer_id AND medlemmer_id ='$q' ");


if (mysql_num_rows($result4) == 0)
echo "Query returned 0 rows";

while($row = mysqli_fetch_array($result4)) {
    echo $row[medlemmer_navn].'<br>';
    echo $row[boder_pris].' kr.';
}
?> 

I run a check to make sure that the query is returning something. I get no errors.

I'm relatively new to this. Any suggestions to what is going wrong? I can see that something happens if i click on the select form. But nothing is displayed.

TietjeDK
  • 1,167
  • 2
  • 15
  • 43

2 Answers2

2

You are combining mysql_* with mysqli_*.`

So working solution will be

while($row = mysql_fetch_array($result4)) {
    echo $row[medlemmer_navn].'<br>';
    echo $row[boder_pris].' kr.';
}

But mysql_* is deprecated and will be removed in the future, you should use mysqli_* or instead PDO with prepared statements.

Community
  • 1
  • 1
Lkopo
  • 4,798
  • 8
  • 35
  • 60
0

On this line

<option value="<?php echo $row[medlemmer_id] ?>"><?php echo $row[medlemmer_navn] ?></option>

you are missing colons it should be

<option value="<?php echo $row[medlemmer_id]; ?>"><?php echo $row[medlemmer_navn]; ?></option>
Khan Shahrukh
  • 6,109
  • 4
  • 33
  • 43