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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.