i am newbie with php....i am trying to create dropdown list populated by radio button.
1. if month radio button is clicked the dropdown will be populated with months
2. if week radio button is clicked the dropdown will be populated with 4 weeks
3. save and print the selection on the page (not in the url)

I tried to search but couldn't find anything that worked like it.....i can either create dropdown w/o radio button and vise versa...

I tried using java script to populate dropdown dynamically but then when i try to get the selection it doesn't print out anything (it just says "on").

any pointers will be helpful..
thanks in advance...

    if you want the drop down to populate once the radio button is selected with out submitting ther page then you need java script. otherwise submit page, check get\post parameters output appropriate dropdown.

    post what you have written so far if you want more detailed help

      Take a look at using "jQuery" http://jquery.com/ I've just started using this with PHP and it works fantasticly.

      I've not got time to go into it here and now, but i may write an article for the site about it in the future.

      Cheers

      Shawty

        i have tired mulitple ways:

        Basic idea (functions i found from another php source) but html/php after that I am trying to use on my own:

        <?php
         function createMonths($id='month_select', $selected=null)
             {
                    $months = array(
                         1=>'January',
                         2=>'February',
                         3=>'March',
                        4=>'April',
                         5=>'May',
                         6=>'June',
                         7=>'July',
                         8=>'August',
                         9=>'September',
                         10=>'October',
                         11=>'November',
                         12=>'December');
        
        
             $selected = is_null($selected) ? date('m') : $selected;
        
             $select = '<select name="'.$id.'" id="'.$id.'">'."\n";
             foreach($months as $key=>$mon)
             {
                 $select .= "<option value=\"$key\"";
                 $select .= ($key==$selected) ? ' selected="selected"' : '';
                 $select .= ">$mon</option>\n";
             }
             $select .= '</select>';
             return $select;
         }
        
         function createWeek($id='week_select', $selected=null)
             {
                    $quarter = array(
                         1=>'1st Week',
                         2=>'2nd Week',
                         3=>'3rd Week',
                         4=>'4th Week');
        
        
             $selected = is_null($selected) ? date('m') : $selected;
        
             $select = '<select name="'.$id.'" id="'.$id.'">'."\n";
             foreach($week as $key=>$quar)
             {
                 $select .= "<option value=\"$key\"";
                 $select .= ($key==$selected) ? ' selected="selected"' : '';
                 $select .= ">$quar</option>\n";
             }
             $select .= '</select>';
             return $select;
         }
        
         ?>
          [B]
         <label><input type="radio" name="dw_sel" value = "month" id="month_select" onclick = 'createMonths("month_select",1)' /> month</label>
         <label><input type="radio" name="dw_sel" value = "week" id="week_select" onclick = 'createWeek("week_select",1)' /> week</label>
         <select size="1" id="dw_sel"></select>[/B]
        
        
         onclick: doesn't work
        
        i can take that out and do:
        if(_GET['dw_sel']=="month")
        {
        echo createMonths("month_select",1);
        }else if(_GET['dw_sel']=="week")
        {
        createWeek("week_select",1)
        } now this code works when i select week and click submit button.  but that is not what i need it to do.  i need to select week radio button and populate dropdown, then select whatever week or select month radio button and populate dropdown. then click submit.upon which i can use the variable to do below.
        
        
        I do not know how i can select 2nd week, save it in variable and do something like below.
        
        
        here i need to put logic for example (i will appreciate any input or help in regards to this as well):
        
        if month is selected and it is janurary then set and print red color else if february then set and print pink and so on.
         as well if it is week and not month  then if it is 1st week then set and print black....

        i hope i made sense...
        Thanks for the response...

          you cant mix java script with php like that, php is server side and java script is client side, as i said before its going to be done all one way or the other.

            Without a doubt mp, certainly did make sense :-)

            Your trying to call PHP functions from JavaScript calls which is not possible.

            PHP is a server side language. That means that all your PHP function calls have already been executed and the output turned into HTML by the time it's displayed in your browser.

            At the other end of the scale is JavaScript, this is a client side language and runs in your browser, what ever you put in an onClick entry in an HTML tag is a JavaScript call and will expect to find a function written using the javascript language inside some script block in the resulting HTML code produced by your HTML.

            It does take some getting used to, and it's defo not an easy thing to get your head around.

            The top and bottom of this all is that JavaScript cannot talk directly to PHP and vice versa.

            There are clever methods you can use such as getting PHP to print out JavaScript code to your page, and have JavaScript submit hidden input tags back to the loaded document to pass data back to PHP, but this is all very very complicated stuff.

            As i mentioned previously, what your trying to achive will need to be done using ajax based technologies, and will be made much easier by using a dedicated JavaScript library such as Jquery or Prototype.

            If i get chance i'll add a post to this topic with a very basic example in, but in order to go further you really do need to look at the client side model before you even touch PHP.

            Cheers

            Shawty

              Actually the reason i need this working in php is because I had only one dropdown to choose from before and whatever value was picked i used it in my select statements inside php script. all of the coding is done. I do not want to re-write the code in another language as it is working per expected.

              now i have months added instead of only week. so i need to add little bit more logic in the start where user is given two options to select from "month" or "week" (only one at a time). If month January is selected then do something or if week 2 is selected then do something. I am having issues with setting that up. Any tips will be helpful otherthan changing the language 🙂. in accessing the variables.

              before i had only the following given one dropdown. now i need the same but for two dropdowns with only one selection possible at a time

              <FORM method=get action=>
                <p>make selection</p>
                <p>
                <select name=week size=1 va>
                  <option value=1>1st week </option>
                  <option value=2>2nd week </option>
                  <option value=3>3rd week </option>
                  <option value=4>4th week </option>
                </select>
              <input type=submit value=submit>
              if ($_GET['week'] == "") {
                 $storeWeek = 1;
              } 
              else { 
              	$storeWeek = $_GET['week'];
              }
              if ($storeWeek== 1) {echo 'red';}
              if ($storeWeek== 2) {echo 'blue';} and so on...
              

              thanks for being patient with me...

                Hi mp,

                I understand exactly what you are trying to do, and it's not a case of i'm advising you to change language, it's a case of you must change language if you want to do what your trying to do.

                Purely and simply you cannot make a button or other control on a rendered web page fire a PHP procedure and update the already drawn control on the web page.

                You can make a form, and a submit button that then calls a script which then re-draws the entire page with a new form, but you cannot re-draw the form in place on the page without using Javascript.

                As an outline to how you would achive this....

                1. Code the main page in PHP.
                2. In place of where the select list would go, put an HTML div element
                3. Use an include or whatever to place the default select, or whatever you need to in the div
                4. Place 2 HTML buttons on the form.
                5. Set up some <link> tags in the header of the document to include which ever javascript libs you wish to use for the javascript call (i recommemd Jquery)
                6. Attach handlers written in Javascript to the onclick even in your buttons
                7. Have the Javascript onclick event handlers make an AJAX request to call a seperate PHP script on your server that returns the appropriate select list (This could simply be the same default you used for include erlier)
                8. When the Javascript call returns, take the HTML it obtained from your call to a seperate PHP script, and replace the contents of your HTML div with the contents recived.

                Your still going to be able to use your exsisting PHP code to make the select lists you already have, but you are going to have to mix in a little Javascript, there's no escaping that.

                Cheers

                Peter

                  Ok mp,

                  Here's a solution that works:

                  First create index.html as follows:

                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html xmlns="http://www.w3.org/1999/xhtml">
                  
                  <head>
                    <title>jQuery Test</title>
                    <script type="text/javascript" src="jquery.js"></script>
                    <script type="text/javascript">
                  
                  /* Setup the java handlers using Jquery Javascript lib */
                  $(document).ready(function()
                  { 
                    $('#select_div').load("get_select.php?type=month");
                  
                    $("#opt_months").click( function(){ $('#select_div').load("get_select.php?type=month"); } );
                    $("#opt_weeks").click( function(){ $('#select_div').load("get_select.php?type=week"); } );
                  });
                  
                    </script>
                  </head>
                  
                  <body>
                  	<h1>jQuery Dynamic Select Switch Test</h1>
                  
                    <!-- the following div is the target for loading the select list into -->
                  	<div style="width: 100%; height: 100px; border: 1px solid black; padding: 4px" id="select_div">
                  	  <p>This is the div where your select list will live, the $('#select_div')... line above auto loads the default select in here using java script.</p>
                  	  <p>When viewing this page in the browser, you will not see these 2 paragaraphs as they will be replaced..</p>
                    </div>
                  
                  
                    <p>Please select drop down type:</p>
                    <input type="radio" name="opt_group" id="opt_months" value="0" checked="checked" />Months<br/>
                    <input type="radio" name="opt_group" id="opt_weeks" value="1" />Weeks
                  
                  </body>
                  </html>
                  

                  Then create get_select.php as follows:

                  <?php
                  function createMonths($id='month_select', $selected=null)
                       {
                              $months = array(
                                   1=>'January',
                                   2=>'February',
                                   3=>'March',
                                  4=>'April',
                                   5=>'May',
                                   6=>'June',
                                   7=>'July',
                                   8=>'August',
                                   9=>'September',
                                   10=>'October',
                                   11=>'November',
                                   12=>'December');
                  
                  
                       $selected = is_null($selected) ? date('m') : $selected;
                  
                       $select = '<select name="'.$id.'" id="'.$id.'">'."\n";
                       foreach($months as $key=>$mon)
                       {
                           $select .= "<option value=\"$key\"";
                           $select .= ($key==$selected) ? ' selected="selected"' : '';
                           $select .= ">$mon</option>\n";
                       }
                       $select .= '</select>';
                       return $select;
                   }
                  
                  function createWeek($id='week_select', $selected=null)
                       {
                              $quarter = array(
                                   1=>'1st Week',
                                   2=>'2nd Week',
                                   3=>'3rd Week',
                                   4=>'4th Week');
                  
                  
                       $selected = is_null($selected) ? date('m') : $selected;
                  
                       $select = '<select name="'.$id.'" id="'.$id.'">'."\n";
                       foreach($quarter as $key=>$quar)
                       {
                           $select .= "<option value=\"$key\"";
                           $select .= ($key==$selected) ? ' selected="selected"' : '';
                           $select .= ">$quar</option>\n";
                       }
                       $select .= '</select>';
                       return $select;
                   }
                  
                  @$type = $_GET['type'];
                  if(isset($type))
                  {
                    if($type == "month")
                    {
                      print "<p>Please select month:</p>";
                      print createmonths();
                    }
                    else
                    {
                      print "<p>Please select week:</p>";
                      print createweek();
                    }
                  }
                  else
                  {
                    print "<p>ERROR : No Select type was specified.</p>";
                  }
                  
                  ?> 
                  

                  Put both of these files into the same folder on the web server.

                  finaly download "jquery.js" from http://jquery.com/ , this is the javascript library i recomend for working with PHP.

                  Once you have Jquery then copy the jquery.js file into the same folder as the html & php files, and hey presto you can change the select on the fly.

                  How does it work?

                  Jquery makes it simple to add click handlers to your html elements in your HTML form. How you create these elements really doesnt matter.

                  When you click on a radio button, a click handler (as defined in the top of the html) is fired for the element clicked.

                  This handler then makes an ajax call to the file get_select.php in the background.

                  get_select takes one parameter, this is a GET parameter and it's used to determine which select list to produce, this output is then returned back to your html page and the exsisting html inside the DIV with the ID of 'select_div' is replaced.

                  Cheers

                  Shawty

                    Write a Reply...