I have a page that my Warcraft guild is going to use to update our open/closed classes status. On this page (rec_main.php) is a table with 2 (or maybe 3) columns. The first column has the various classes (druid, hunter, etc.), the second column "had" sql queries which pulled the open/close status out of the database. The 3rd column has a combo box for each class with "open" and "close" options.

My first thought was to have 3 columns as described above, and a "submit" button at the bottom which when pressed will submit all of the changes in each of the combo boxes to the database. However, users may not change more than one of the classes' open/closed status', and it could potentially screw things up to always update all of the rows in the table.

So, my next thought was to eliminate the 2nd column mentioned above, and have the first column be the class names, and the 2nd column be the combo boxes. The data displayed in each of the combo boxes would be the data pulled from the database, and the use would have the option of changing that data between open and closed. When the user changes the option, it will post to that record in the database only automatically.

Here is a rough sketch of my mysql table (called oth_recruitment):

Fields: Class Value ID

      Druid     Open     1
      Hunter   Closed  2
      Mage     Open     3

etc...

the php page that displays the data is rec_main.php, and it's layed out like this:

     Class        Status

    Druid         (will display "open" in combo box, user can also select "closed")
    Hunter       (will display "closed" in combo box, user can also select "open")
    Mage         (will display "open" in combo box,...) etc....

When a combox box value is changed, it saves that value to the database.

I'm guessing some Java will be required here, I don't know.

If anyone can be of some assistance, it would be appreciated. I'm having a tough time finding tutorials on combo boxes for php.

    They are quite simple all that is needed is a name to have [] on the end and a loop such as..

    <?
    if(isset($_POST['checkbox'])){
    foreach($_POST['checkbox'] as $x){
    echo $x;
    echo "<br>";
    }}else{
    echo "Please Select At Least One";
    }
    ?>

      Maybe I need to be a little more specific. I have a lot of experience with ASP. With PHP, not so much. The query language is similar, but my trouble is coding between HTML and PHP - where to use HTML and where to use PHP, in other words.

      The code that you left for me doesn't make a lot of sense to me, because the "checkbox" thing - well....i'm not using checkboxes. All I want is this:

      1) a list of "classes" (meaning Warcraft character classes)
      2) a listbox for each class with the options "Open" and "Closed" (and maybe a couple others)
      3) The values in the database will show in the listboxes when the page is loaded.
      4) The user can select another value in a listbox (one for each character class) and when he does, it will update the database AND the page.

        So if i`m getting your question right , you want to list all the classes and next to that class you will have a combo box with the values "open" and close".
        ex:

        Class name

        druid - combo [Open | Close]
        hunter - combo [Open | Close]

        [Submit button]

        All you have to do is use a "form" element and list all the classnames and combobox inside the form element. ex:

        <form id="form1" name="form1" method="post" action="index.php">
        <?php
        $arr = array('1','2','3');
        
        while(list($key,$val) = each($arr))
        {
        	print $key.'&nbsp;';
        ?>
          <select name="select_<?=$val;?>">
            <option value="1">Open</option>
            <option value="0">Close</option>
          </select>
        <?php
        }
        ?>  
        <input type="submit" name="Submit" value="Submit" /> </form>

        Remember in the above example I have taken an array. You have to replace that with your record set. Remember to have the $val as the ID of your class name. Finally on submit do as follow,

        if(isset($_POST['Submit']))
        {
        	while(list($key,$val) = each($HTTP_POST_VARS))
        	{
        		if(substr($key,0,6)=='select')
        		{
        			$classNameErr = explode('_',$key);
                                $classNameId =  $classNameErr[1];
                                $classNameStatus =  $val;
        
                            <!-- update the table -->
        
        	}
        }
        }
        

        Here the 'select' will be the first 6 characters of the combo box. You can use any name for that.

        Hope this helps,

        Regards,
        Niroshan

          That WILL work, and I'm going to start with it. Is there a way, however to do away with the "submit" button, and instead have the database field update when the user changes any one of the combo box options? I've seen it done - I've used similar combo boxes. Any ideas?

            yes you can. only thing you need to do is instead of having the submit button submit the form when ever user select the combo box.

            <select name="select_<?=$val;?>" onchange="document.form1.submit();"> 
            
            

            but there is a drawback. if you try to do it when ever user select the combo box then the user can only choose one option at a time due to the form submission on every option select. thats why i have put a submit button so user can at a glance select all the options and then submit all the data at once. This will be more user friendly and it will save lots of server calls and speed up the performance.

              Alright, I'm looking at the code you submitted.

              I've edited the first block to look like this:

              <form id="druid" name="druid" method="post" action="rec_main.php">
              // rec_main.php is the page this information is displayed on - shouldn't this be something else?
              <?php
              $status = array('Open','Closed');
              
              while(list($key,$val) = each($status))
              {
                  print $key.'&nbsp;';
              ?>
                <select name="select_<?=$val;?>">
                  <option value="Open">Open</option>
                  <option value="Closed">Closed</option>
                </select>
              <?php
              }
              ?>  
              <input type="submit" name="Submit" value="Submit" /> </form>

              I've not changed the code to exclude the submit button here. Now, would I insert this code AFTER the database query? For example:

              
              <?php
              
              include 'config.php';
              include 'opendb.php';
              
               $query  = "SELECT druid FROM oth_recruitment";
               $result = mysql_query($query);
              
                 blah, blah blah
              ?>

              In other words, I want the status pulled from the database, and put into the list box as one of the options...That's the way I REALLY want to do it. The alternate way is to have 2 tables side-by-side. The first table will show the current stati (I think that's a word) of the classes, and the second will have the form(s) to change the stati via a submit button or when the value is changed.

              The submit code (second block of code you posted) would go on it's own php page right? If it's like asp at all, I know that the actual transaction is done by passing data from the form to the handler. Is there anything I need to know regarding php to add an auto redirect from that form handler page back to rec_main.php, or does it not work like I think it does?

                Hi sorry about delayed reply. I was kind a busy with my office work.

                I think you misunderstood.let me explain. first the top potion of the php code where you list all the classes.If i undestood your question correctly you should have a table stucture as below,

                classes

                Id ------------> PK
                className --> class names (ex:druid, hunter)
                status -------> status of the class 0 - close, 1 - open

                now PHP code:

                <form id="form1" name="form1" method="post" action="index.php">
                <?php
                $sql = 'SELECT * FROM classes';
                $row = mysql_query($sql);
                
                while($result = mysql_fetch_array($row))
                {
                    print $result['className'].'&nbsp;';
                ?>
                  <select name="select_<?=$result['id'];?>">
                    <option value="1" <?=$result['status ']==1 ? 'selected' : '';?>>Open</option>
                    <option value="0" <?=$result['status ']==0 ? 'selected' : '';?>>Close</option>
                  </select>
                <?php
                }
                ?>  
                <input type="submit" name="Submit" value="Submit" /> </form>

                now the answer for your question:
                rec_main.php is the page this information is displayed on - shouldn't this be something else?

                No you dont have to give any other file here. you can use the same file name.

                P.s: if u want u can give another file. if your giving another file name as target then the second potion of the php code should be in that file.

                now the second part:

                
                if(isset($_POST['Submit']))
                {
                    while(list($key,$val) = each($HTTP_POST_VARS))
                    {
                        if(substr($key,0,6)=='select')
                        {
                            $classNameErr = explode('_',$key);
                            $classNameId =  $classNameErr[1];
                            $classNameStatus =  $val;
                
                        $sql = 'UPDATE classes SET status='.$classNameStatus.' WHERE id='.$classNameId;
                        mysql_query($sql);
                
                    }
                }
                
                 print 'update done';
                } 
                

                this way u can update all the classes status at once.

                Hope this would help.

                Thanks,

                Regards,
                Niroshan

                  I put your code in, and it's doing something, but it's not updating...here it is.

                  <?
                  session_start();
                  if(!session_is_registered(myusername)){
                  header("location:main_login.php");
                  }
                  ?>
                  <html>
                  <head>
                  <meta http-equiv="Content-Language" content="en-us">
                  <style type="text/css">
                  .style1 {
                  	border: 4px solid #304614;
                  	text-align: center;
                  		background-color: #000000;
                  		background-image: url('assets/light.jpg');
                  }
                  .style5 {
                  	background-repeat: repeat-x;
                  }
                  .style6 {
                  	font-size: large;
                  	color: #000000;
                  	font-family: "BLACK KNIGHT";
                  }
                  .style8 {
                  	text-align: center;
                  	background-color: #000000;
                  }
                  .style15 {
                  	border: 1px solid #000000;
                  	text-align: center;
                  		background-color: #C0C0C0;
                  		color: #000000;
                  }
                  .style16 {
                  	border-collapse: collapse;
                  	border: 1px solid #000000;
                  }
                  .style23 {
                  	font-size: x-small;
                  	color: #000000;
                  	font-family: Verdana;
                  }
                  .style24 {
                  	border: 1px solid #000000;
                  	text-align: left;
                  	font-size: small;
                  	background-color: #008080;
                  	font-family: Verdana;
                  }
                  </style>
                  </head>
                  
                  <body style="color: #FFFFFF; background-color: #000000; background-image: url('assets/tirisfal-bg.jpg');background-repeat:repeat-x;">
                  
                  <table border="0" cellpadding="0" cellspacing="0" style="width: 1395px; height: 518px" align="center">
                  	<!-- MSTableType="layout" -->
                  	<tr>
                  		<td valign="top" colspan="3" style="height: 71px">
                  		<!-- MSCellType="ContentHead" -->
                  		&nbsp;</td>
                  	</tr>
                  	<tr>
                  		<td>&nbsp;</td>
                  		<td valign="top" class="style1">
                  		<!-- MSCellType="ContentBody" -->
                  		<br>
                  		<span class="style6">Site Recruiting Setup<br>
                  		<br>
                  		</span>
                  		<span class="style23"><a href="http://drknights.com/admin/">&lt;&lt;click here
                  		to go back</a></span>
                  		<table align="center">
                  			<tr>
                  				<td>
                  				<table align="right" class="style16" style="width: 341px">
                  					<tr>
                  						<td class="style8">Item</td>
                  						<td class="style8" style="width: 135px">Current&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  						Change</td>
                  						<td class="style8" style="width: 120px">Update</td>
                  					</tr>
                  					<tr>
                  						<td class="style24"><strong>Recruiting Status</strong></td>
                  						<td class="style15" colspan="2">
                                                                      <form id="form1" name="form1" method="post" action="rec_main.php" style="width: 245px">
                                                                      <?php
                                                                      include 'config.php';
                                                                      include 'opendb.php';
                  
                                                                  $sql = "SELECT status FROM oth_recruitment";
                                                                  $row = mysql_query($sql);
                  
                                                                  while($result = mysql_fetch_array($row))
                                                                  {
                                                                                print $result['status'].'&nbsp;';
                                                                  ?>
                                                                  <select name="select_<?=$result['id'];?>" style="width: 70px">
                                                                  <option value="1" <?=$result['status ']==1 ? 'selected' : '';?>>Open</option>
                                                                  <option value="0" <?=$result['status ']==0 ? 'selected' : '';?>>Close</option>
                                                                  </select>
                                                                  <?php
                                                                  }
                                                                  ?>
                                                                  <input type="submit" name="Submit" value="Submit" >&nbsp;
                                                                  </form>
                                                                  </td>
                  				</tr>
                  				<tr>
                  					<td class="style24">Druid</td>
                  					<td class="style15" colspan="2">
                                                                  <form id="form2" name="form2" method="post" action="rec_main.php" style="width: 245px">
                                                                  <?php
                                                                  $sql = "SELECT druid FROM oth_recruitment";
                                                                  $row = mysql_query($sql);
                  
                                                                  while($result = mysql_fetch_array($row))
                                                                  {
                                                                      print $result['druid'].'&nbsp;';
                                                                  ?>
                                                                  <select name="select_<?=$result['id'];?>" style="width: 70px">
                                                                  <option value="1" <?=$result['druid ']==1 ? 'selected' : '';?>>Open</option>
                                                                  <option value="0" <?=$result['druid ']==0 ? 'selected' : '';?>>Close</option>
                                                                  </select>
                                                                  <?php
                                                                  }
                                                                  ?>
                                                                  <input type="submit" name="Submit" value="Submit" >&nbsp;
                                                                  </form>
                                                      </td>
                  				</tr>
                  				<tr>
                  					<td class="style24">Hunter</td>
                  					<td class="style15" colspan="2">
                                                                  <form id="form3" name="form3" method="post" action="rec_main.php" style="width: 244px">
                                                                  <?php
                                                                  include 'config.php';
                                                                  include 'opendb.php';
                  
                                                                  $sql = "SELECT hunter FROM oth_recruitment";
                                                                  $row = mysql_query($sql);
                  
                                                                  while($result = mysql_fetch_array($row))
                                                                  {
                                                                       print $result['hunter'].'&nbsp;';
                                                                  ?>
                                                                  <select name="select_<?=$result['id'];?>" style="width: 70px">
                                                                  <option value="1" <?=$result['hunter ']==1 ? 'selected' : '';?>>Open</option>
                                                                  <option value="0" <?=$result['hunter ']==0 ? 'selected' : '';?>>Close</option>
                                                                  </select>
                                                                  <?php
                                                                  }
                                                                  ?>
                                                                  <input type="submit" name="Submit" value="Submit" >&nbsp;
                  
                                                                  </form>
                                                      </td>
                  				</tr>
                  				<tr>
                                                      </td>
                  					<td class="style8" colspan="3">&nbsp;</td>
                  				</tr>
                  
                  			</table>
                  		 </td>
                                 </table>
                  	   </td>
                  	<td style="height: 400px">&nbsp;</td>
                  </tr>
                  <tr>
                  	<td style="width: 96px" class="style5">&nbsp;</td>
                  	<td style="width: 1203px" class="style5">&nbsp;</td>
                  	<td style="height: 47px; width: 96px" class="style5">&nbsp;</td>
                  </tr>
                  </table>
                  </html>
                  <?php
                  if(isset($_POST['Submit']))
                  {
                      while(list($key,$val) = each($HTTP_POST_VARS))
                      {
                          if(substr($key,0,6)=='select')
                          {
                              $classNameErr = explode('_',$key);
                              $classNameId =  $classNameErr[1];
                              $classNameStatus =  $val;
                  
                          $sql = "UPDATE oth_recruitment SET status='.$classNameStatus.' WHERE id='.$classNameId' ";
                          mysql_query($sql);
                  
                      }
                  }
                  
                   print 'update done';
                  }
                  include 'closedb.php';
                  ?>

                  It's probably something small, I don't know. When I press "submit" the "update done" text comes up at the bottom of the screen, but the fields aren't changing. I've only included 2 form examples, keeping in mind that there are 10. If you want to see the page in action, goto www.drknights.com, click the administrator link, then click the "change status" link on the bottom of the left table. use your username from these forums and 12345 as the password.

                    Hi Petrakid,

                    I have gone through your code and saw you have included config and database open file each time you calling a query. You don’t have to do that since your connecting to a single database why don’t you call includes on top of the page so that will include the config files on one place instead of including it on every query execution.

                    First I need to clarify something. Do you want to have a submit button and a combo box for each option or just combo boxes for each option and one submit button? Because according to your code your using a combo box and a submit button for each option but your initial requirement was to have each option a combo box and ONE submit button.

                    If each option have a combo box and a submit button and a form (according to your code) we don’t have to bother much and even you don’t have to loop through $HTTP_POST_VARS array even. What we can do is keep a hidden field to hold option ID and just do normal update as below,

                    if(isset($_POST['Submit']))
                    {
                        $sql = "UPDATE oth_recruitment SET status='. $_POST[‘combobox’]
                    .' WHERE id='$_POST[‘hiddenField’]' ";
                                mysql_query($sql);
                    
                        }
                    }
                    
                     print 'update done';
                    }
                    
                    
                    

                    But if you want to have one submit button the scenario is completely changing. Then you have to include all your combo boxes in a ONE FORM. This is what I have done in my code.

                    Hope you understood the both options. If you have any queries please ask.

                    Regards,
                    Niroshan

                      Yea, it's not working. The problem is that I'm looking at 10 different websites and tutorials - each gives different examples and different methods of updating the database, and I'm getting confused. With ASP it was simple because you didn't have to jump between php and html code all the time. That's probably what's getting me most confused.

                      Can you give me a very basic example. say the database is called "mydatabase", the table is called "mytable", there are three fields, "field1", "field2", and "field3". Each field's data is displayed in a table, and after each data is a combo box with 2 options, "Option1," and "Option2". At the bottom of the table is a "submit" button. Now, also keep in mind that the table I have setup now does NOT have an id field...just the field name and 1 value in the field. In other words, there is only 1 value that will be passed from the webpage to the database, and that's the value selected in the combo box.

                      You've been the most help so far out of all the tutorials and forums I've been reading....I wish there was a website that showed how to use combo boxes in an update set.

                        oh, if I need to make another field in the database, and ID field that would be the primary key, I can - I "borrowed" this table from a similar recruitment page, and this is how they had it setup.

                          Hi petrakid,

                          Ok here goes the example. I think I have understood your requirement and according to that I have made some changes to your requested table structure. I will try to explain you as much as I can and it’s up to you to do more research on it.

                          class table structure

                          ID – [PRIMARY KEY]:auto generated number :: its always a best practice to have a primary key and sometimes it’s a must (if it’s a joining table, in a situation like many to many and one to many relationship then you cannot have a primary key instead you have to defined the primary key in those main two tables.). more information research on table relationships.

                          className - Varchar :: This will hold all the class names. Ex: druid, hunter

                          status - Int :: This will hold the status value of the each class. Ex: 1 – open / 0 – close

                          This is the table structure we are going to have in our example. Its has an ID which is a auto generated number. It’s going to be a unique number so when ever we are updating a field we are going to use that ID field.

                          Here comes the PHP part.

                          <body>
                          <?php
                          mysql_connect('localhost','[user name]','[password]');
                          mysql_select_db('phpbuilder');
                          
                          
                          if(isset($_POST['Submit']))
                          {
                          	while(list($key,$val) = each($HTTP_POST_VARS))
                              {
                                  if(trim(substr($key,0,10))=='cmbStatus_')
                                  {
                                      $classNameErr = explode('_',$key);
                                      $classNameId =  trim($classNameErr[1]);
                                      $classNameStatus =  $val;
                          
                                  $sql = 'UPDATE mytable SET status='.$classNameStatus.' WHERE id='.$classNameId;
                          		mysql_query($sql);
                              }
                          }
                          
                           print 'update done'; 
                          }
                          ?>
                          
                          <table width="100%" border="0">
                            <tr>
                              <td><form id="form1" name="form1" method="post" action="10851604.php">
                                  <table width="100%" border="0">
                                    <tr>
                                      <td>Class Name</td>
                                      <td>Status</td>
                                    </tr>
                          <?php
                          $sql = 'SELECT * FROM mytable';
                          $row = mysql_query($sql);
                          
                          while($result = mysql_fetch_array($row))
                          {
                          ?>
                          	<tr>
                          		<td><?=$result['className'];?></td>
                          		<td><select name="cmbStatus_<?=$result['id'];?>" id="cmbStatus">
                          		  <option value="1" <?=$result['status']==1 ? 'selected' : '';?>>Open</option>
                          		  <option value="0" <?=$result['status']==0 ? 'selected' : '';?>>Close</option>
                          		</select>
                          		</td>
                          	  </tr>
                          	  <tr>
                          		<td>&nbsp;</td>
                          		<td>&nbsp;</td>
                          	  </tr>
                          <?php
                          }
                          ?>        
                          <tr> <td colspan="2"><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form></td> </tr> </table> </body>

                          Now the explanation:

                          First we look at the first part of the code;

                          <?php
                          $sql = 'SELECT * FROM mytable';
                          STEP 1:$row = mysql_query($sql);
                          
                          STEP 2:while($result = mysql_fetch_array($row))
                          {
                          ?>
                          	<tr>
                          		<td><?=$result['className'];?></td>
                          STEP 3:		<td><select name="cmbStatus_<?=$result['id'];?>" id="cmbStatus">
                          STEP 4:		  <option value="1" <?=$result['status']==1 ? 'selected' : '';?>>Open</option>
                          		  <option value="0" <?=$result['status']==0 ? 'selected' : '';?>>Close</option>
                          		</select>
                          		</td>
                          	  </tr>
                          	  <tr>
                          		<td>&nbsp;</td>
                          		<td>&nbsp;</td>
                          	  </tr>
                          <?php
                          }
                          ?>
                          

                          STEP 1 & STEP 2:
                          In these steps we are just fetching all the records from the mytable and loop through the records.

                          STEP 3:
                          Here what we do is we are setting the combo boxes name. the name will have two parts.
                          1 – A default value as ‘cmbStatus_’ to identify the combo boxes from other objects we used in our form.
                          2 – Record Id to identify individual records when we doing the update query.

                          STEP 4:
                          In this step we are checking the previous records status and adjust the combo value according to it.

                          Now we are done with setting up the form. Lets look at the second part.

                          STEP 1: if(isset($_POST['Submit']))
                          {
                          STEP 2:	while(list($key,$val) = each($HTTP_POST_VARS))
                              {
                          STEP 3:        if(trim(substr($key,0,10))=='cmbStatus_')
                                  {
                          STEP 4:             $classNameErr = explode('_',$key);
                                       $classNameId =  trim($classNameErr[1]);
                                       $classNameStatus =  $val;
                          
                          STEP 5:            $sql = 'UPDATE mytable SET status='.$classNameStatus.' WHERE id='.$classNameId;
                          STEP 6:			mysql_query($sql);
                                  }
                              }
                          
                           print 'update done'; 
                          }
                          

                          STEP 1:
                          Checking if the ‘submit’ button is clicked.

                          STEP 2:
                          Loop through the HTTP_POST_VARS array to get the all the combo values.

                          Step 3:
                          Get the first 10 characters of each html element and match with the combo default value.

                          Step 4:
                          If match found then get the ID part from the combo name.

                          Step 5:
                          Prepare the update statement.

                          Step 6:
                          Update the table.

                          Hope this explanation is good and if you need further helop please let me know.

                          Thanks,

                          Regards,
                          Niroshan

                            7 days later

                            That's not exactly it either - The things that I don't have are:

                            the need to pull class names out of the database - the FIELD's in the database ARE the class names. I have the class names coded into the HTML, so I can make them look better without a lot of code.

                            the use of 1/0 for status - I'm using OPEN and CLOSED. It's the way this table was designed by the other people that designed it, and I don't want to change a bunch of other code all over the rest of my website to accommodate for a new table.

                            What I DO have is:

                            3 columns on the webpage's table, one for class name (hard coded), one for "current status" (from database) and one with comboboxes for each class using Open and Closed options. The option that is automatically selected in the combo boxes is the current status - this way when the user clicks the submit button, which submits the whole form, nothing will change that doesn't need to change. I have 1 submit button, per your suggestion, at the bottom of the table, which is supposed to send the selected options to the form handler on a different page, and update the database. I decided to use a separate page to handle the updating, simply because that's how I did it with asp, and I'm more familiar with that approach.

                            Each value that is pulled from the database initially and displayed is assigned to a variable. If I have to ever change anything, I won't have to change a bunch of code this way.

                            Now, when the user selects an option, either open or closed, from one of the combo boxes, the script needs to know what option that is, and what to send to the form handler. So, if the user selects "open" for the class "hunter", when he presses "submit" that value needs to pass to the form handler and be updated in the database. This is where I'm missing something. Now, when I press "submit" it takes me to the form handler, and the page says that the data is updated - when the auto-redirect script I have takes me back to the initial page, however, the data doesn't change.

                            I got a text box on the same page (different form) and using nearly the same code, I was able to get it to update correctly. What do I have to do differently on combo boxes?

                              **update! I figured it out. Took me a little bit of trial and error, but it works perfectly now, and I understand php a bit better.

                                Write a Reply...