Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Update textfield from list

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Update textfield from list

Postby Jimbojames30 » Mon Mar 11, 2013 2:10 pm

Hi,

please you someone help me out, i have a dropdown list from a recordset called item, i have another field called description, how can i get it to update automatically from the item selection

many thanks, sorry new to php and mysql

table

item, description
1123, spirderman
1124, heman

form
Item: 1123 --(dropdown box)
Description: spirderman --(THIS TO AUTO POPULATE)
Jimbojames30
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Mar 09, 2013 1:30 pm

Re: Update textfield from list

Postby johnj » Tue Mar 12, 2013 8:51 pm

This is basically javascript. Please read about onChange() function and use that to populate your second drop down.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Update textfield from list

Postby seandisanti » Wed Mar 13, 2013 8:29 am

I recently did something like this... the markup for the selects was:
Code: Select all
        <div id="townDiv">
            <form action="trystuff.php" method="post">
                <select id="sourceDisorders" class="sel" size="1" style="position: absolute;z-index:2;left:0px;" >
                    <option>Select a Disorder for Axis I</option>
                    <?php echo createDisorderOptions(1); //this populates the first box with options from a database?>
                </select><br />
                <select name="dsm_axis_1" id="targetDisorders" class="sel" size="10" style="padding-top:5px;" >
                </select>
                <input type="hidden" name="axis_selected" id="axis_selected" />
            </form>
        </div>

and then the jquery and styling i used to move items back and forth on change is:
Code: Select all

        <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
        <style type="text/css">
            body
            {padding:0; margin:0; font-weight:normal;font-size:13px;}


            select.sel{
                margin:30px;
                width:420px;
            }
        </style>
        <script>
            $(function() {
                $("#sourceDisorders").change(function() {
                    $("#sourceDisorders option:selected").appendTo('#targetDisorders');
                    $("#targetDisorders option").prop("selected",false);
                    $("#axis_selected").val( $("#axis_selected").val()+'|' +$("#targetDisorders option:last").text());
                });
                $('#targetDisorders').dblclick(function() {
                    $('#targetDisorders option:selected').appendTo('#sourceDisorders');
                    var old='|' + $("#sourceDisorders option:last").text();
                    var targetList=$('#targetDisorders option:selected');
                    $("#axis_selected").val($("#axis_selected").val().replace(old,""));
                    targetList.remove();
                    var foption = $('#sourceDisorders option:first');
                    var soptions = $.makeArray($('#sourceDisorders option:not(:first)')).sort(function(a, b) {
                        return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
                    });
                    $('#sourceDisorders').html(soptions).prepend(foption);
                    foption.attr("selected", true).siblings("option").removeAttr("selected");
                });
            });
        </script>
the first select is a single line tall, and as its value changes, whatever is selected is moved to the second select. Also, the input axis_selected is kept up to date with a pipe separated list of selections in the second select box, so that the form handler can just explode the list from $_POST['axis_selected']
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron