Hi

I'm hoping someone here can help me with a php/mysql question :-)

I have a dropdown menu populated from a table "tblsyslur". The fields in "tblsyslur" are:
sysla
syslaskt
syslaid = primary key

I have taken the values from the field "sysla" and used that to populate a dopdown menu. What I want to do is to is, when a value is selected in the dropdown menu, the "syslaid" is registred and used to lookup values in another table (tblhreppar) (that also has syslaid in it) and then a list of values from the tblhreppar is displayed below the dropdown menu.

Here is the code I've used to generate the dropdown menu.

<?php

require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database, $db_server)
	or die("Unable to select database: " . mysql_error());


$results=mysql_query("SELECT * FROM tblsyslur") or die(mysql_error()); 
echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($results)) { 


echo "<option value=$row[syslaid]>$row[sysla]</a></option>"; 
} 
echo "</select>"; 

?>
Member Avatar for diafol

try this for the options:

echo "<option value=\"{$row['syslaid']}\">{$row['sysla']}</a></option>";

with regard to the other question:

is this supposed to work with a normal form submit or is an ajax (javascript onselect trigger)?

Hi, thanks for taking the time to look at this :-)

Is the syntax suggestion you posted, a better solution than the one I posted?

Regarding the latter question, I really don't have a preference. I don't have any experience with ajax so I'm not sure what the difference is ;-)

Member Avatar for diafol

Answer 1

echo "<option value=$row[syslaid]>$row[sysla]</a></option>";

shouldn't work.
You should enclose array keys within quotes (double or single can be used).
Array items should be enclosed within braces when used inside quotes, e.g. for echo.

Answer 2
OK, so you don't seem to have any form tags. Read up on creating forms and related syntax - you need to know about method and action attributes. Also there does not seem to be a submit button - again look this up.

Apologies if you know all about this, it's just you didn't include them in your code.

Answer 1
The dropdown menu works fine as it is, you can see it here:
http://www.hafdal.dk/testing/dropdown.php

Answer 2
So I would need a submit button to get an action that generates a new sql query? I was thinking maybe something like "upon selection, load value into variable". Perhaps a mysql_fetch_object()?

Member Avatar for diafol

No - you must send the form data to the server in order to run server side scripts. Ajax is a 'cheat' which allows you to do this without refreshing the page. You simply send the form data via javascript via XMLHttpRequest to a php page and wait for a response.

So, you can send ajax-like without form tags - just the dropdown if you like. I would suggest using something like jQuery for this though. It can all get horrible and complicated otherwise.

Hi again. I started looking into Ajax after "talking" to you, and I found an example on w3schools that actually fits my needs almost to a T.

You can see it here: http://www.w3schools.com/php/php_ajax_database.asp

I got the example there up and running on my server:
http://www.hafdal.dk/testing/getuser.htm

However, I wanted to change the way the dropdown list is populated, i.e. I wanted it to be generated from the database BUT I can't figure out how to get a space between the variables firstname and lastname in this line:

echo "<option value=\"{$row['id']}\">{$row['firstname']}{$row['lastname']}</a></option>";

As far as I can see the ajax script wont work if I only get the firstname (I would love to know why though! I cant seem to figure out how the $q is collected). See the way its displaying here: http://www.hafdal.dk/testing/users.php

Here is all the code:

<html>
<head>

<?php

require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database, $db_server)
	or die("Unable to select database: " . mysql_error());


$results=mysql_query("SELECT * FROM user") or die(mysql_error()); 
echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($results)) { 

echo "<option value=\"{$row['id']}\">{$row['firstname']}{$row['lastname']}</a></option>";
} 
echo "</select>"; 

?>

<script type="text/javascript">
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>
</head>
<body>


<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>
Member Avatar for diafol

EEK - full js alert! :) You went the complicated way after all!

To get the space:

echo "<option value=\"{$row['id']}\">{$row['firstname']} {$row['lastname']}</a></option>";

Just put a space between the variables.

For the select trigger - well you don't have one. Something like this maybe:

echo '<select name="myselect" onselect="showUser();return false;">';

This runs the js function when the dropdown item is selected.

Now for the showUser code.

You want to get the $row value passed to the querystring fo the php file. You can get this from the following (been a while since I did any vanilla js - so bear with me):

var element = document.getElementById("myselect");
var evalue = element.options[element.selectedIndex].value;

...

xmlhttp.open("GET","getuser.php?q="+evalue,true);

Not tested.

The getuser.php should pick up the q value thus:

if(isset($_GET['q'])){

 //clean the variable
 //use it to retrieve data from the DB
 //echo the data in the format you want it displayed

}

EEK - full js alert! :) You went the complicated way after all!

LOL yes looks like I did! :-)

So I managed to get the space between the variables, but that's about the only thing I got working :-/

Why does the trigger not work after I pull the dropdown values from the database? I tried to add the trigger as suggested but without any luck and that's probably because I haven't looked that much into Ajax.

But here's what I have now, and unfortunately it does not work :-/

<html>
<head>
<script type="text/javascript">
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>
</head>
<body>

<?php

require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database, $db_server)
	or die("Unable to select database: " . mysql_error());


$results=mysql_query("SELECT * FROM user") or die(mysql_error()); 
echo "<select name=myselect>"; 
while($row=mysql_fetch_assoc($results)) { 


echo "<option value=$row[id]>$row[firstname]</a></option>"; 
} 
echo "</select>"; 

?>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>
Member Avatar for diafol

OK, this is javascript. You'll probably find somebody here to help you, but the js forum is probably the best place.

Also - as I suggested - jQuery - using the .post or .ajax methods is a breeze. Have a look.

Thanks for trying to help me!! :-))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.