Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2000071

RE: [PHP] Newbie Alert - populating form fields From: Chad Day (cday <email protected>)
Date: 07/14/00

Here's an old post of mine, modifying a script I found on the net to use
mysql_querys to populate instead of hardcoding the array elements in.

Message: 3794873
FROM: Chad DayDATE: 05/25/2000 12:59:51SUBJECT: RE: [PHP3] FW: new twist on
multiple select box problem I -just- finished modifying some code I found in
a codebase out there to get
multiple select boxes while pulling from mysql into arrays. Some of the
variables (store*) are still unchanged from the original code, but this will
work. As you can see, the original coders comments and link to his page are
still there, so I -just- finished this up. :)

<HTML>
 <HEAD>
 </HEAD>
 <BODY>
 <?

require("globals.php");
require("common.php");
// Connect to the Database
if (!($link=mysql_pconnect($hostname, $username, $password))) {
   DisplayErrMsg(sprintf("error connecting to host %s, by user %s",
                             $hostname, $username)) ;
   exit() ;
}

// Select the Database
if (!mysql_select_db($databasename, $link)) {
   DisplayErrMsg(sprintf("Error in selecting %s database", $databasename)) ;
   DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link),
mysql_error($link))) ;
   exit() ;
}

         /***************************************************************/
         /*Code: PHP3 */
         /*Author: Leon Atkinson <leon <email protected>> */
         /*Creates two form fields based on data from a database */
         /*Writes javascript that update field 2 based on field 1 */
         /***************************************************************/

         /* To simulate database connectivity, I`ll fill some arrays
*/
// $store_id = array( "1", "3", "4", "5", "6");
// $store_state = array( "California", "California", "California",
"Nevada", "Utah");
// $store_city = array( "Los Angeles", "San Francisco",
"Walnut Creek", "Las Vegas", "Salt Lake City");
// $numStores = 5;
         // $distinct_states = array( "California", "Nevada", "Utah");

        $fillleague = mysql_query("select LEAGUENAME, TEAMNAME, TEAMID from
leagues, teams where leagues.LEAGUEID = teams.LEAG
UEID order by LEAGUENAME");
        $fillteam = mysql_query("SELECT TEAMNAME, LEAGUEID from teams order
by LEAGUEID");

        $store_state = array();
        $store_id = array();
        $store_city = array();
        while ($row = mysql_fetch_array($fillleague)) {
                array_push($store_state,$row["LEAGUENAME"]);
                array_push($store_id,$row["TEAMNAME"]);
        }
       while ($row = mysql_fetch_array($fillteam)) {
                array_push($store_city,$row["TEAMNAME"]);
        }

        $numStores = mysql_num_rows($fillteam);
        $last_value = ``;

        while ( list( $index, $value) = each ( $store_state)) {
                if ($last_value == $value) {
                        continue;
                }
                else {
                        $distinct_states[] = $value;
                }

        $last_value = $value;
        }
         /* create the javascript function the updates the city
list */
         echo "<SCRIPT Language="JavaScript">";

         echo "function UpdateCity()";
         echo "{";

         /* clear the city selector */
         echo " var City =
document.choose_store.TEAMNAME.options.length;";
         echo " while(City > 0)";
         echo " {";
         echo " City--;";
         echo "
document.choose_store.TEAMNAME.options[City].text =
``;";
         echo "
document.choose_store.TEAMNAME.options[City].value =
``;";
         echo " }";
         echo " document.choose_store.TEAMNAME.options[0].selected
=
true;";

         /* figure out which state is selected*/
         echo " var StateSelected = 0;";
         echo
"
while(document.choose_store.LEAGUENAME.options[StateSelected].selected==f
alse) StateSelected++;";

         $StoreState = "";
         $CityCount = 0;

         /* If this weren`t a demo, I`d run a query something
like: */
         /* select * from store order by State
*/
         /* But since this is a demo, I`ll just read from an
array */
         $RowCount = 0;
         while($RowCount < $numStores)
         {
                 $store_ID = $store_id[$RowCount];
                 $store_State = $store_state[$RowCount];
                 $store_City = $store_city[$RowCount];

                 if($StoreState != $store_State)
                 {
                         if($StoreState != "")
                         {
                                 echo " }";
                         }

                         echo
" if(document.choose_store.LEAGUENAME.options[StateSelected].value ==
`$store_State`)";
                         echo " {";

                         echo
" document.choose_store.TEAMNAME.options[0].value = 0;";
                         echo
" document.choose_store.TEAMNAME.options[0].text = `Choose
a
Team`;";

                         $StoreState = $store_State;
                         $CityCount=1;
                 }

                 echo
" document.choose_store.TEAMNAME.options[$CityCount].value =
`$store_ID`;";
                 echo
" document.choose_store.TEAMNAME.options[$CityCount].text =
`$store_City`;";
                 $CityCount++;
                 $RowCount++;
         }

         echo " }";

         echo "}";

         echo "</SCRIPT>";

         echo "<CENTER><FORM NAME=choose_store>";

         echo "<SELECT NAME=LEAGUENAME onChange="UpdateCity();">";
         /* This is another place I`d put a query:
*/
         /* select distinct State from store order by State
*/
         echo "<OPTION VALUE=0>Choose a League";
         $RowCount = 0;
         while($RowCount < count($distinct_states))
         {
                 echo "<OPTION VALUE="";
                 echo $distinct_states[$RowCount];
                 echo "">";
                 echo $distinct_states[$RowCount];
                echo "";
                 $RowCount++;
         }

         echo "</SELECT>";

         /* Now we need to know how many cities are listed for
the state with the most stores */
         /* select count(*) as items from store group by State
order by items desc */
         $MaxItems = 5;
         echo "<SELECT NAME=TEAMNAME>";
         $RowCount = 0;
         while($RowCount <= $MaxItems)
         {
                 echo "<OPTION VALUE="";
                 echo $store_city[$RowCount];
                 echo "">";
                 echo "Choose a Team";
                 $RowCount++;
         }
         echo "</SELECT><BR><BR>";
         echo "<input type="submit">";
         echo "</FORM></CENTER>";

         echo "<SCRIPT Language="JavaScript">UpdateCity();</SCRIPT>";
 ?>

 <BR>
 <BR>
 <CENTER>
 Check out
 <A
HREF="http://www.restorationhardware.com/secondary.htmy?id=location">www.Res
torationHardware.com</A>
 to see this code used to take to a real database.
 </CENTER>
 </BODY>
 </HTML>

-----Original Message-----
From: Reed, Dana [mailto:Dana.Reed <email protected>]
Sent: Thursday, May 25, 2000 2:41 PM
To: `php3 <email protected>`
Subject: [PHP3] FW: new twist on multiple select box problem

Anybody?

It seems like someone must have run into this problem before. I`d really
appreciate any thoughts on it.

med vnlig hlsning
/Dana

> -----Original Message-----
> From: Reed, Dana
> Sent: Thursday, May 25, 2000 4:53 PM
> To: `php3 <email protected>`
> Subject: new twist on multiple select box problem
>
> All
>
> I had no problem setting up a multiple select box, BUT, I don`t know how
> to call this with JavaScript.
>
> Is there some way of escaping the [] in "this.form.list1[]" or,
> alternatively, some way for PHP to receive multiple values without the []
> in the select name?
>
> Any other ideas?
>
>
> Here`s some example code (part of multiple select box swapper):
>
> <SELECT MULTIPLE SIZE="4" NAME="list1[]">
> <OPTION VALUE="x">item x</OPTION>
> <OPTION VALUE="y">item y</OPTION>
> <OPTION VALUE="z">item z</OPTION></SELECT>
>
> <INPUT TYPE="button" VALUE=" >> "
> onClick="move(this.form.list1,this.form.list2)" NAME="b1">
> <BR>
> <INPUT TYPE="button" VALUE=" << "
> onClick="move(this.form.list2,this.form.list1)" NAME="b2">
>
>
> Thanx for any suggestions.
>
> med vnlig hlsning
> /Dana

Hope this helps you, good luck!

Chad

-----Original Message-----
From: Beth Clark [mailto:elisaclark <email protected>]
Sent: Friday, July 14, 2000 11:19 AM
To: php-general <email protected>
Subject: [PHP] Newbie Alert - populating form fields

Not only new to PHP, but new to programming too, so please bare with me:)

I have a form that I want to populate with data from one table and insert
into another table. I've managed so far to populate a drop down menu from
the first table, but how do I populate the other fields once one of those
items is selected?

What I have so far is the drop down menu:

<?

$query = "select * from Courses";

$result = mysql_query($query);

$num = mysql_numrows($result);

echo "<font face=\"Arial\">Please choose the course you would like to
schedule:</font><br><br>";
echo "<font face=\"Arial\" color=\"blue\">Course:</font><br>";
echo "<select name=\"Course\"Course_ID=Course_ID>";
echo "<option value='' selected>***Select a Course***</option>";

/*Dynamically generate drop-down list*/
$i=0;
while ($i<$num):

$hello = mysql_result($result,$i, "CourseTitle");
echo "<option value='$hello'>$hello</option>";

$i++;

endwhile;
echo "</select><p>";
?>

and a field;

Audience:<br></font><textarea name="Audience" rows="5" cols="25" wrap><?
echo "$Aud"; ?></textarea></td>

Will someone please point me in the right direction for tutorials or scripts
or whatever on how to code "if Course A is selected, than Audience=
$Audience from table Courses where Course A = Course A". I know I'm missing
something fundamental here and I'm running out of time before deadline! TIA

Beth

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe <email protected>
For additional commands, e-mail: php-general-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe <email protected> For additional commands, e-mail: php-general-help <email protected> To contact the list administrators, e-mail: php-list-admin <email protected>