ristenk1
ristenk1

Reputation: 433

Javascript and PHP radio buttons to change visibility

I'm trying to work my way through a problem. It's two parts:

  1. I want to create a loop that reads out each of my page's menu_name from the database and on the same line that it reads it out, put a radio button to change the visibility. So far It looks like my radio buttons are all connected because (instead of being checked "no" for each of them it is only checked no on the last <li> item:

     //K's function:
     function get_all_pages() {
         global $connection;
         $query = "SELECT * 
             FROM pages ";
         $query .= "WHERE visible = 1 ";
         $query .= "ORDER BY position ASC";
         $page_set = mysql_query($query, $connection);
         confirm_query($page_set);
         return $page_set;
     }
    
     //K's function:
     function list_all_pages(){
         $output = "<ul>";
         $page_set = get_all_pages();
         while ($page = mysql_fetch_array($page_set)) {
             $output .= "<li><a href=\"add_feature.php?page=" . urlencode($page["id"]) . "\">{$page["menu_name"]}</a></li>";
             $output .= "&nbsp;&nbsp;<input type=\"radio\" name=\"visible\" value=\"0\" checked=\"checked\" /> No <input type=\"radio\" name=\"visible\" value=\"1\" /> Yes";
         }
         $output .= "</ul>";
         return $output;
     }
    

picture of problem #1

  1. I'm trying to do some sort of ajax thing with Javascript.. so I want the visible radio buttons to be next to my dynamically generated menu list. I want them to all be checked to no... when they are checked to yes I want a dropdown list to appear where they can pick the numbers 1-8 (the numbers will mean what position they will appear in).

I am new and trying to figure this out is a little out of my reach without some assistance from Javascript and php/mysql experts.

Upvotes: 1

Views: 142

Answers (2)

EthanB
EthanB

Reputation: 4289

Your radio buttons all have the same "name" attribute, so you can only select one at a time. You could append some unique identifier to the attribute value. For example, visible_{$page_id}:

function list_all_pages(){
    $output = "<ul>";
    $page_set = get_all_pages();
    while ($page = mysql_fetch_array($page_set)) {
        $page_id = urlencode($page["id"]);
        $output .= "<li><a href=\"add_feature.php?page={$page_id}\">{$page["menu_name"]}</a></li>";
        $output .= "&nbsp;&nbsp;<input type=\"radio\" name=\"visible_{$page_id}\" value=\"0\" checked=\"checked\" /> ";
        $output .= "No <input type=\"radio\" name=\"visible_{$page_id}\" value=\"1\" /> Yes";
    }
    $output .= "</ul>";
    return $output;
}

Advanced tip: With PHP you can simplify the processing of "page" data by using specially-formatted input names -- like so:

  • Let the radio buttons use names like pages[$page_id][visible].
  • Let the lists (aka select) use a name like pages[$page_id][order].

Once the user submits this data, PHP converts it into a "pages" array (with indices being page_id's). You can then do something like:

function process_pages($pages) {
  foreach ($pages as $page_id => $data) {
     $is_visible = $data['visible'] === '1';
     $page_order = (int) $data['order'];
     //... update page's visibility/page order
  }
}
$pages = $_REQUEST['pages'];
process_pages($pages);

Upvotes: 2

Ibrahim
Ibrahim

Reputation: 1261

1. Change the name attribute of each radio button to disconnect them e.g. use visible1, visible2, ...

2. put the <input> tag within <li> after your <a> tag. That will show your radio buttons next to your dynamically generated menu.

3. You can create select list for number by creating and calling a javascript function that will be executed by onclick of Yes options, which will create and show the select fields.

Please comment on the answer if you need more clarification.

Upvotes: 0

Related Questions