Trying to create a type of dynamic checkbox. I would like it to represent the current state.

This check box is to check the values within a database. If the value is 1 (active) the box will be checked.
If the value is 0 (inactive) the box will not be checked.

Also,
Need the values to be submitted to the database as checked.

I cannot recall the code I used in the past which was successful, so I am currently at a loss

Code is as follows:

<input type="hidden" name="Active" value="0" />
	<input type="Checkbox" name="Active" value="1" <?php if($Active == 1){echo "CHECKED";} ?>>

BTW - the first line sets the variable Active to 0 if not checked.

The next step is to do the same (default to the current value) of a drop down box

    Ok, you told us what you're trying to do... but I don't believe you told us what's wrong. What's happening? Are the checkboxes not getting selected according to their values in the database?

    If so, how do you retrieve their values from the database? Where did this $Active variable come from?

      don't worry - I just went though your same train of thought and problem solved (box not being checked if active)

      <input type="hidden" name="Active" value="0" />
      	<input type="Checkbox" name="Active" value="1" <?php if(($row_rsUser['Active']) == 1){echo "CHECKED";} ?>>

        is there a simple way with a drop down box to default to the current value?

          In the setup you're using? If you hardcode values, sure:

          <select name="foobar">
          <option value="test1"<?PHP if($row_rsUser['foobar'] == 'test1') echo 'selected';?>>Testing!</option>
          <option value="test2"<?PHP if($row_rsUser['foobar'] == 'test2') echo 'selected';?>>Testing!</option>

          and so on.

            not hardcoded - it is a dynamic list from mysql

              Well, in that case, it's the same basic idea. Check to see if the current option is equal to the user's choice, and if so, add the SELECTED keyword to the HTML.

                Little puzzled. Here is my html code. It currently defaults to the lowest access level as defined in the query.

                <option value="<?php echo $row_rsUserType['AccessLevel']?>"<?php if (!(strcmp($row_rsUserType['AccessLevel'], $row_rsUserType['AccessLevel']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsUserType['UserType']?></option>

                my php for the selection box is:

                <?php
                //connect to the database and define the recordsets for the dynamic drop down box and its limits
                	//usertype recordset
                		//allow only the system administrator to add users with same access level, all other AccessLevels add user below
                	if ($_SESSION['loginAccessLevel'] == 1) {
                	$query_rsUserType = "SELECT UserType, AccessLevel FROM usertype WHERE AccessLevel >= ('{$_SESSION['loginAccessLevel']}') ORDER BY AccessLevel ASC";
                		} else {
                		$query_rsUserType = "SELECT UserType, AccessLevel FROM usertype WHERE AccessLevel >= ('{$_SESSION['loginAccessLevel']}'+1) ORDER BY AccessLevel ASC";
                		}
                
                $rsUserType = mysql_query($query_rsUserType, $connMI) or die ('<p>Could not select the database because <b>' .mysql_error().'</b></p>');
                $row_rsUserType = mysql_fetch_assoc($rsUserType);
                $totalRows_rsUserType = mysql_num_rows($rsUserType);
                ?>

                Unfortunately, I am currently at a loss here - newbie stuff, but I assume the change in the code will be in the bolded selection here:

                <option value="<?php echo $row_rsUserType['AccessLevel']?>"<?php if (!(strcmp($row_rsUserType['AccessLevel'], $row_rsUserType['AccessLevel']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsUserType['UserType']?></option>

                  Try just echo'ing selected, instead of selected="selected".

                    as in

                    <option value="<?php echo $row_rsUserType['AccessLevel']?>"<?php if (!(strcmp($row_rsUserType['AccessLevel'], $row_rsUserType['AccessLevel']))) {echo "selected";} ?>><?php echo $row_rsUserType['UserType']?></option>
                      11 days later
                      gamber wrote:

                      not hardcoded - it is a dynamic list from mysql

                      Gamber,

                      I am trying to retrieve data from mySQL DB and use the entries in the DB to generate a custom/dynamic form which is basically a list of Primary Key items (Entry ID number) and a check box to allow the user to select the entries they want to see.

                      The DB will constantly be changing, so i obviously can't hardcode any of the checkboxes. Could you help me with this? Is this what you've done?

                      Thanks in advance.

                      Ps - I am an experienced programmer, but very new to web programming. So far i have mySQL DB running, some php pages SELECTing and INSERTing via form submission, and some Curl form submission stuff working.

                        MasterCJ,
                        Give me a little more info.
                        What is to be included within your form? I should be able to offer a more relevant explanation if you give me a little more info.

                        Been through it a few times, and I am sure I can help you out.
                        Gamber

                          gamber wrote:

                          MasterCJ,
                          Give me a little more info.
                          What is to be included within your form?

                          The form will read data from a mySQL DB
                          and dynamically create a form which will have multiple rows
                          and two columns.

                          The first column will list Job ID and the second column will list
                          websites.

                          The user will 'check' which Job's and which websites the user wants
                          to submit these jobs to.

                          Once a user has selected the desired stuff, they then click on a
                          submit button and then a php page will CURL to each selected
                          website and dump in the Job data.

                          The dynamic part of my checkbox website is/should be, everytime the user goes onto it, a php script will run to generate a page will all
                          the latest jobs and websites.

                          I hope this is enough information, please advise me if you need anything
                          else. THANKS!

                          to the website

                            25 days later

                            MasterCJ,
                            Sorry for the long response time. Still in need of a solution/help? Wrapped up a couple of projects so my time is again free - just ask!

                            gamber

                              Write a Reply...