I have read numerous tutorials on how to populate a dropdown box with values from a database but what I want to do is the other way around - update a database with values from a dropdown box.

Say I have Table1 with fields Name and Married, with the default value for Married "No". I echo the contents of Table1 into a html table, with the column Married showing dropdown boxes instead of just text. I must then be able to select Yes or No from the boxes and my code should update the database accordingly.

So far I am able to display the data in a table but I having trouble with displaying the dropdowns and making them update the database. Please help, thanks.

    yourpage.php

    <?
    if(isset($_POST['married'])){
    
    
    $result = mysql_query("update Table1 set Married='$_POST[married]' where Name='$_POST[name]'");
    
    
    
    }
    
    
    ?>
    
    
    <form action=post>
    Name: <input name=name>
    
    Married: <select name="married"  onchange="this.form.submit();">
    
    <option value=yes>yes</option>
    <option value=no>no</option>
    
    
    </form>

      let's see some of your code (use the forum's php code please)

        [code=php]

      [/code]

      I'd like to see how you are going about displaying the records, plus, it's easier to add to code then generate new code, no matter how simple the script. You'll usually get more help on programming forums this way.

      There are different ways of going about your drop down menu issue. An easy way is to loop through the database and within your loop that displays the menu, after checking what the value of the column is (and setting that value to, say, $selected and setting the row's id to $id), add something like:

      echo "<select name=\"selection\">";
      echo "<option value=\"yes\" ";
      if ($selected == "yes"){echo "selected";}
      echo ">";
      echo "<option value=\"no\" ";
      if ($selected == "no"){echo "selected";}
      echo "></select>";
      
       

        Thanks both of you for the replies. I am really new to all of this. I will try your suggestions right after posting this. As for the code for displaying data, I use:

        echo "<table>";
        
        while($row = mysql_fetch_assoc($result)) {
        $pqtn=$row["question"];
        $pans=$row["answer"];
        $psby=$row["submitted_by"];
        
        echo "<tr>";
        echo "<td> $pqtn </td>";
        echo "<td> $pans </td>";
        echo "<td> $psby </td>";
        echo "</tr>";
        
        }
        
        echo "</table>";
        

        This is from a different project but I used the same approach in this one I'm working on now. Just haven't gone around to displaying a rows in column as dropdowns (having trouble making them work). I was reading up on it while I made my first post. Anyways, thank you very much, I'll go back once I solve my problem with your suggestions. 🙂

          this is a little late, but, can i put php code inside the onchange property of the dropdown?

            flushfire wrote:

            this is a little late, but, can i put php code inside the onchange property of the dropdown?

            It depends on what you intend to happen. PHP is completely server side, meaning the browser sends the server information, PHP processes it, then generates a new html page. If you are looking to do something like....if a drop down menu changes to a category, use php to pull up that category and populate a new menu with subcategories....you'll need to use AJAX, which combines MYSQL, PHP, and JAVASCRIPT.

              i just want to change/asign variables after the onchange is triggered... would that be possible?

                why not reload the whole thing, php page,
                process information from dropdown/page and get your variables
                and display again

                sure it is possible

                <form method="post" action="">
                <select name="dropdown" onchange="this.form.submit()">

                  I want to be able to change variables without my submit button activating. if that's possible. im fairly new to php so if this is stupid please bear with me. =P

                  These are my two problems right now: I want to change variables on the onchange of the dropdown, and I'm having problems relating the dropdown to the specific record it should be asigned to.

                  I have tried the first code example, It shows an input box, I dont want the user to input names. Ive also tried assigning an array in place of that input box, but Im having trouble making it work when posting. The second code example I prefer, but then I'm having trouble relating the data from the dropdowns to the records in the database. anyways, after trying to make this work using checkboxes (and failing) this is the code I am stuck with:

                  <?php
                  if (isset($_POST['submitted'])){
                  
                  $nm=$_POST['name'];
                  $st=$_POST['status'];
                  
                  require_once ('mysql_connect.php');
                  $query="SELECT * FROM practice WHERE name LIKE '%$nm%' AND married LIKE '%$st%'";
                  $result=mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: ". mysql_error());
                  if(@mysql_num_rows($result) > 0) {
                  
                  echo "<h2>Search Results</h2>";
                  
                  echo "<table border=\"1\" cellspacing=\"0\" width=\"320\">";
                  echo "<tr bgcolor=\"#AAAAAA\"><td align=\"center\"><b><font color=\"#FFFFFF\">Name</font></b></td><td align=\"center\"><b><font color=\"#FFFFFF\">Status</b></font></td></tr>";
                  
                  while($row = mysql_fetch_assoc($result)) {
                  
                  $name=$row["name"];
                  $status=$row["married"];
                  
                  echo "<tr>";
                  echo "<td> $name </td>";
                  echo "<td align=\"center\"><select name=\"status\">";
                  echo "<option value=\"Yes\" ";
                  echo "if ($status == \"Yes\"){echo \"selected\";}";
                  echo ">Yes</option>";
                  echo "<option value=\"No\" ";
                  echo "if ($status == \"No\"){echo \"selected\";}";
                  echo ">No</option></select></td>";
                  echo "</tr>";
                  
                  }
                  
                  echo "</table>";
                  echo "<br>Do another <a href=\"search.php\">SEARCH</a>?";
                  
                  }else{
                  echo "<h2>Search Results</h2>";
                  echo "No results found. Do another <a href=\"search.php\">SEARCH</a>?";
                  }
                  
                  }else{
                  ?>
                  <h2>Search</h2>
                  <form method="post" action="search.php">
                  	<table border="0">
                    		<tr>
                  			<td>Name</td>
                  			<td> 
                  		        <input type="text" name="name">
                  			</td>
                  		</tr>
                  		<tr>
                  			<td>Status</td>
                  			<td>
                  			<input type="text" name="status">
                  			</td>
                  		</tr>
                  		<tr>
                  			<td colspan="2" align="center"><input type="hidden" name="submitted" value="TRUE">
                  			<input type="submit" name="submit" value="Search">
                  			</td>
                  		</tr>
                  	</table>
                  </form>
                  <?php } ?>
                  

                  What should I add to get the dropdowns to relate to the specific columns in the database?

                    help anyone? I have tried using an array as the name for dropdowns but when posting it only contains 1 value. I don't know whats wrong.

                    this is what i have for the table:

                    echo '<td align="center"><select name="dropdown[]">';
                    echo '<option value="Yes" ';
                    echo 'if ($status == "Yes"){echo "selected";}';
                    echo '>Yes</option>';
                    echo '<option value="No" ';
                    echo 'if ($status == "No"){echo "selected";}';
                    echo '>No</option></select></td>';
                    

                    and what i use for showing the contents of the dropdowns:

                    if (isset($_POST['save'])){
                    $y = $_POST['dropdown'];
                    	foreach($y as $value){	
                    	echo "$value<br/>";
                    	}
                    

                    edit: nevermind, I figured what was wrong with my code. Thanks to everyone who replied!

                      Write a Reply...