Page 1 of 2

Populate field from database

Posted: Wed Sep 19, 2012 12:10 am
by steve
I am using the following code on admin/new_ticket.php

Code: Select all

  <td style="text-align:right">
		<?php
        
			?>
			User:
</td>
<td>

            <select name="user">
			<option value="" selected="selected"><?php echo $hesklang['select']; ?></option>
			<?php
            if ($ticket['owner'])
            {
            	echo '<option value="-1"> > '.$hesklang['unas'].' < </option>';
            }

			foreach ($admins as $k=>$v)
			{
				if ($k != $ticket['owner'])
				{
					echo '<option value="'.$k.'">'.$v.'</option>';
				}
			}
			?>
			</select>
    </td>
	</tr>
	<tr>
	<td style="text-align:right">Email:</td>
	<td width="80%"><input type="text" name="user_email" size="40" maxlength="50" value=""/></td>
	</tr>
	</table>
The idea is to have a drop down box that has all users in the users table listed. So far this part works.

Once I select a user, I want their email address (as its set in the users table) to populate the "user_email" field.

Is this something you would be willing to help me with?

Re: Populate field from database

Posted: Wed Sep 19, 2012 5:37 pm
by Klemen
I'm a bit busy to be more helpful, but you would need to come up with some Javascript code. Basically the idea is:

- create a Javascript array and populate it with Admin info (ID and Email)
- add an onchange to the select box, you can get the index like this:
form1.user.options[form1.user.options.selectedIndex].value
- find the email with this ID value from the array and insert it into the email field

Re: Populate field from database

Posted: Wed Sep 19, 2012 6:42 pm
by inka
I might be able to help here...
I have something similar in a page. A list populated from the database and then another info requested from the db depending on the selection.
I`m not really a programmer so the code is probably messy, but it works.

The javascript in the head part of the html:

Code: Select all

<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","getemail.php?q="+str,true);
xmlhttp.send();
}
</script>
The php code for the selection list:

Code: Select all

<?php 

DEFINE ('DB_USER', 'user');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'users');

$con = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

if (!$con) {
	echo 'Could not connect to MySQL: ' . mysqli_connect_error();
}
$sql="SELECT name, id FROM users ORDER BY name";
$result=mysqli_query($con, $sql);
?>
<select name="users" onchange="showUser(this.value)">
<option>Select your name</option>
<?php
while($row_list=mysqli_fetch_assoc($result)){
?>
<option value="<?php echo $row_list['id']; ?>"><?php echo $row_list['name']; ?></option>
<?php
}
?>
</select>
As you can see in the javascript I used another page (getemail.php) to inquire the db. Here the code for that:

Code: Select all

<?php
$a=$_GET["q"];
function email($a){$dbc = @mysqli_connect('localhost', 'user', 'password', 'database');
$sql2="SELECT email FROM users WHERE id=$a";
$list2=mysqli_query($dbc,$sql2);
$row_list2=mysqli_fetch_assoc($list2); 
$nume=$row_list2['nume_fil']; 
return $last;}
echo email($a);
?> 
Put this where you want the email to appear:

Code: Select all

<div id="txtHint"></div>
As I said I compiled the page a long time ago and the code probably could be really improved, but you could start from here.

Re: Populate field from database

Posted: Thu Sep 20, 2012 2:53 pm
by kabuc
but why not suitable in the same field name e-mail? example name (email)

Re: Populate field from database

Posted: Mon Sep 24, 2012 4:48 pm
by steve
Thank you for your help.

I have the drop down box showing all users in the database, however I an unable to see the email output of the selected user. Any ideas?

Re: Populate field from database

Posted: Mon Sep 24, 2012 5:20 pm
by kabuc
steve wrote:Thank you for your help.

I have the drop down box showing all users in the database, however I an unable to see the email output of the selected user. Any ideas?
but there are simple way like this http://i46.tinypic.com/radxc6.png

Re: Populate field from database

Posted: Mon Sep 24, 2012 6:10 pm
by steve
I don't think you understand what I am trying to accomplish here.

I want to populate a field with the email address of the user selected from the drop down box.

Re: Populate field from database

Posted: Mon Sep 24, 2012 6:27 pm
by kabuc
steve wrote:I don't think you understand what I am trying to accomplish here.

I want to populate a field with the email address of the user selected from the drop down box.
without example I don't understand :)

Re: Populate field from database

Posted: Mon Sep 24, 2012 6:31 pm
by steve
I know :lol:

Thats ok, inka's hack is what i am looking for, the only issue is the javascript output is not doing what I want it to do.

Re: Populate field from database

Posted: Mon Sep 24, 2012 7:09 pm
by kabuc
I think this is you want, you need just little apply with your wishes. there are source code and demo
http://remysharp.com/2007/09/18/auto-po ... ect-boxes/

Re: Populate field from database

Posted: Tue Sep 25, 2012 12:28 am
by steve
Thank you this is useful, first I will see if inka has an ideas why his script is not working for me.

Re: Populate field from database

Posted: Mon Oct 01, 2012 7:11 am
by inka
Unfortunately without seeing your code, I can`t really help.
Make sure you put everything in the right place and don`t forget to put the last div where you want the email to appear.
Any errors?

ps.
I was in a hurry, and modified the getemail.php wrong, I don`t know if you caught the mistake or not.

Code: Select all

<?php
$a=$_GET["q"];
function email($a){$dbc = @mysqli_connect('localhost', 'user', 'password', 'database');
$sql2="SELECT email FROM users WHERE id=$a";
$list2=mysqli_query($dbc,$sql2);
$row_list2=mysqli_fetch_assoc($list2);
 
Here you should of course write at the end:

Code: Select all

$email=$row_list2['email'];
return $email;}
echo email($a);
?>
It was wrong to ask for a $row_list2['nume_fil'] when we selected email and giving $last as a result was another big mistake, since I don`t had this variable in the copied code. Sorry for them.

Re: Populate field from database

Posted: Mon Oct 01, 2012 4:07 pm
by steve
I guess the issue I was having was in the getemail.php, i changed the php to what you provided in your last response and now it works fine.

Thanks so much for your help.

Re: Populate field from database

Posted: Mon Oct 01, 2012 5:18 pm
by kabuc
What is the point of this idea?

Re: Populate field from database

Posted: Mon Oct 01, 2012 9:29 pm
by steve
I use this on the /admin/new_ticket.php page, so I can send notification of a ticket being created to specified users in the system.