Jump to content

Radio Buttons From MySQL


SalientAnimal
Go to solution Solved by Psycho,

Recommended Posts

Hi All, 
 
I closed an earlier thread I had as I have made a number of changes, and did some research to try and achieve what I am looking for. I have managed to get a few bit and pieces of code together, that I would like to try and combine to get the desired result.
 
What I want to achieve on my form is the following:
 
A user selects a "shift" from a drop-down that is created from my MySQL database.
 
This list is created using the following:
 
function createOptions($optionList, $selectedValue)
{
    $options = '';
    foreach ($optionList as $option)
    {
        $selected = ($option['value']==$selectedValue) ? ' selected="selected"' : '';
        $options .= "<option value='{$option['value']}'{$selected}>{$option['label']}</option>\n";
    }
    return $options;
}


//DETERMINE SELECTED OPTIONS PASSED ON QUERY STRING
$shift   = isset($_GET['shift'])   ? intval($_GET['shift'])   : false;
//$agent = isset($_GET['agent']) ? intval($_GET['agent']) : false;
//$tertiary_category  = isset($_GET['tertiary_category'])  ? intval($_GET['tertiary_category'])  : false;








//GENERATE OPTIONS FOR THE SHIFT OPTIONS
$query = "SELECT DISTINCT id AS value, shift AS label
          FROM shift_structure
 WHERE active_status = 1
          ORDER BY id"; 
$optionList = $dbo->query($query);
$shift_options = createOptions($optionList, $shift);
This script reloads my form:   
<script language="javascript">


        function getSelectValue(selectID)
        {
            var optionObj = document.getElementById(selectID);
            return optionObj.options[optionObj.selectedIndex].value;
        }


        function reload(form)
        {
            //Adding the unselected options should work fine
            var locationURL = 'shift_form_schedule.php';
                locationURL += '?shift='   + getSelectValue('shift');
         //       locationURL += '&secondary_category=' + getSelectValue('secondary_category');
        //        locationURL += '&tertiary_category='  + getSelectValue('tertiary_category');
            //Perform the reload
            self.location = locationURL;
        }
 </script>

On selecting the shift, I need to create:

1. A list of agents
2. A series of 3 radio buttons for each agent
 
The values of the radio buttons will need to be the agents name.
 
Then on submitting the form each agent's selected radio button needs to be populated into the relevant column in a table.
 
The table has 5 columns
Date - This is the date of the shift
Shift - The actual shift
Present - Radio Button 1
Late - Radio Button 2
Absent - Radio Button 3
 
The radio button columns need to be populated with the agents name, depending on which button was selected in the form.
 
This is a bit of code that I found that will create my radio buttons, however, I do not know how to:
1. Include it into my form.
2. Exclude the database connection section of the code as I already have this in my includes at the top of the form.
3. Adjsut the code to create 3 radio buttons per agent, currently is only creates 1 radio button for each agent. And I can only select one button at a time. I need to be allowed to select 1 button per agent.
 
<?php
require_once 'includes/myradio.php';


define('HOST', 'localhost');
define('USER', 'username');
define('PASS', 'password');
define('DBNAME', 'database');


$db = new mysqli(HOST, USER, PASS, DBNAME);


if ($db->connect_errno) {
echo "Failed to connect to MySQL: (" . $db->connect_errno . ") "
. $db->connect_error;
} else {
$sql = "SELECT shift, title as name FROM schedule";
$result_db = $db->query($sql);
if (!$result_db) {
echo $db->error . ' Error perform query!';
} else {
$aa_student = array();


while ($row = $result_db->fetch_object()) {
$aa_student[$row->shift] = $row->name;
}
myradio($aa_student, 10, 'STUDENT_RADIO', 0, 2);
}
}
$db->close();

Here is the functions for creating the radio buttons:

<?php
function myradio($array, $checked, $name, $return=0, $option=1) {
if (count($array) <= 0) {
return;
}
$str_radio = "";
if ($option == 1) {
for ($i = 0; $i < count($array); $i++) {
if ($array[$i] == $checked) {
$str_radio .=
 "<input type=\"radio\" 
name=\"{$name}\" 
value=\"{$array[$i]}\" 
id=\"id{$array[$i]}\" 
checked=\"checked\"/>";
$str_radio .=
 "<label 
for=\"id{$array[$i]}\">$array[$i]</label>";
} else {
$str_radio .=
 "<input type=\"radio\" 
name=\"{$name}\" 
value=\"{$array[$i]}\" 
id=\"id{$array[$i]}\"/>";
$str_radio .=
"<label for=\"id{$array[$i]}\">$array[$i]</label>";
}
}
}


if ($option == 2) {
foreach ($array as $value => $label) {
if ($value == $checked) {
$str_radio .=
"<input type=\"radio\" 
 name=\"{$name}\" 
 value=\"{$value}\" 
 id=\"id{$value}\" 
 checked=\"checked\"/>";
$str_radio .=
"<label for=\"id{$value}\">{$label}</label>";
} else {
$str_radio .=
"<input type=\"radio\" 
name=\"{$name}\" 
value=\"{$value}\" id=\"id{$value}\"/>";
$str_radio .=
"<label for=\"id{$value}\">{$label}</label>";
}
}
}


if ($return) {
return $str_radio;
} else {
echo $str_radio;
}
}

?>
Please let me know if you require any additional information.
Link to comment
Share on other sites

i moved your thread from the 'third party' forum section to the php coding help section. the 'third party' forum section is for complete (functional) scripts that you are trying to use as is or to modify, such as a chat script, login script... snippets of code and coding concepts that you may have found, while being written by a third party, become your responsibly when you incorporate them into your own scripts.

Link to comment
Share on other sites

  • Solution

SalientAnimal,

 

I think you need to slow down just a tad. Get your scripts working without all the JavaScript. If you are going to go to the trouble of making things react immediately, then you should rally use AJAX rather than just dynamically submitting the page. But, don't do that until you have everything working without JavaScript. So, create your page with a simple Submit button for testing. Plus, you should use POST data as opposed to GET since you are sending so much data.

 

This is really a simple process. On every page load you check for the selected value for any of the fields on the form. Then, query the database for the possible values and use any POSTed values to determine which options are selected when building the form fields.

 

So, just like I provided a function to create the select list options which takes parameters for the possible values and the optional selected value, you can create similar functions for other types of form fields - such as a radio button group, checkboxes, whatever.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.