Jump to content

Help putting a list selection into a variable


Darkmatter5

Recommended Posts

Using the following code examples, how do I get the "Surveyor" selected from the list to be populated to the $q variable in the first PHP tag?  I don't know how to use the $_GET obviously so what I have in the code is an experiment from code from a tutorial site.

 

<?php

        $q=$_GET["q"];

        if(isset($_POST['submit']))

        {

        include 'library/dbconfig.php';

        include 'library/opendb.php';

 

        $query="SELECT * FROM byrnjobdb.surveyors WHERE SurveyorID=$q";

        $result="mysql_query($query)";

        echo "$result";

 

        include 'library/closedb.php';

        }

?>

<?php

       include 'library/dbconfig.php';

include 'library/opendb.php';

 

$query="SELECT SurveyorID, Surveyor

                    FROM byrnjobdb.surveyors

    WHERE Active='1'

    ORDER BY Surveyor ASC";

$result=mysql_query($query);

echo "<select name='Surveyor'>";

echo "<option>---Select---</option>";

while ($row=mysql_fetch_array($result))

{

$r1=$row['surveyorID'];

$r2=$row['Surveyor'];

echo "<option value='$r1'>$r2</option>";

}

echo "</select>";

 

include 'library/closedb.php';

?>

<input type="Submit" name="submit" id="submit" value="Load Record" />

 

Thanks!

 

Add if statement inside the loop to check if the current surveyorID is the requested $q and in that case you would put "selected" in <option> tag

 

   while ($row=mysql_fetch_array($result))
   {
       $r1=$row['surveyorID'];
       $r2=$row['Surveyor'];
       if ($r1 == $q) echo "<option value='$r1' selected>$r2</option>";
       else echo "<option value='$r1'>$r2</option>";
   }

 

 

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.