Jump to content

Drop boxes


Schlo_50

Recommended Posts

Hey guys,

 

I have been working on a lengthy form and have come to a point where i am now stuck for ideas.

I have two drop boxes within the form. The first contains usernames stored in my database, and the second is

supposed to contain information which is unique to a username. So there is a one to many relationship.

 

I want the second generated drop box to automatically display the unique values when a username is selected, but at the moment

it displays all values associated with every username stored in my database. (so the list is rather long!)

 

Can this be done? I have attempted below:

 

<?php 

print "<select name=\"client\">";
$filea = file("../users.txt");

foreach($filea as $Keya => $Vala){

$Dataa[$Keya] = explode("|", $Vala);

$client = $Dataa[$Keya][0];

print "<option value=\"$client\">$client</option>";

    }

print "</select>"; 

 ?>

  </td>
  </tr>
  <tr>
  <td class="text"><img src="images/point.png" width="11" height="8" border="0" />Category</td>
  <td class="text">

<?php


print "<select name=\"category\">";

	$file = file("../usercats.txt");

	foreach($file as $Key => $Val){

   	$Data[$Key] = explode("|", $Val);
	  
     	$username = $Data[$Key][0];
     	$catname = $Data[$Key][1];
     	$catid = $Data[$Key][2];

if ($client = $username){

 		   
     	print "<option value=\"$catid\">$catname</option>";
	   
    }
    }

	print "</select>"; 


?>

 

Thanks for any responses. If you need any questions answered please do ask!

 

Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/91233-drop-boxes/
Share on other sites

you're going to need to use javascript and have an onChange() event trigger when a user name is selected.

 

the javascript should either:

1) [easist] reload the page with the username as a $_GET value in the url so you can use php to pull up the unique info for that user when the page first loads and populate it into the field.

 

or

 

2) [dumbest] populate the second field with the corresponding user's information that you've pre-loaded into a javascript array when the page first loaded.  (although this means all users information will be visible in the client-side source code)

 

or

 

3) [best method but harder] use an "ajax" function to submit the username to a server-side php script that returns the unique information in the background and then populates the the field with the result.

Link to comment
https://forums.phpfreaks.com/topic/91233-drop-boxes/#findComment-467598
Share on other sites

unfortunately, it will not.

 

you may need to find a creative work around (like ask for the username first before the user enters other field information) or have the javascript submit the partially submitted form and when the page reloads, php can use the $_POST values to populate the form fields

Link to comment
https://forums.phpfreaks.com/topic/91233-drop-boxes/#findComment-467609
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.