Solved

jQuery UI Selectable form values

Posted on 2013-02-07
1
409 Views
Last Modified: 2013-02-11
I am using the jQuery UI selection box to create a multiple choice form.
The question and its options are created dynamically from mysql.
Therefore there could be 2 options to choose from or 22.

I want each selection to be entered into the database separately.

As you can see, currently I am inserting the values in 1 textbox, seperating each number via a comma.

What I want is to have a hidden textbox for each option. Then if the selection is selected, the value is inserted into its corresponding textbox.

Code i am currently using below...



jQuery
    $(function() {
        $("#selectable").bind("mousedown", function(e) {
            e.metaKey = true;
        }).selectable({
            stop: function() {
                var result = "";
                $( ".ui-selected", this ).each(function() {
                    var index = $( "#selectable li" ).index( this );
					
                    result += (( index + 1 ) + "," );
                });

				$("input#selectResult").val(result);
            }
        });
    });

Open in new window




HTML

<ul id="selectable">
<?php while($optionRow = mysql_fetch_array($optionResult))
{?>
      <input type="text" id="selectResult" name="<?php echo "qno".$questionID; ?>" value="" />
      <li id="<?php echo $optionRow['questionOptionID'] ?>" class="ui-state-default"><?php echo $optionRow['questionOption'] ?></li>
php } ?>
</ul>

Open in new window

0
Comment
Question by:sjtinsley83
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
 $(function() {
        $("#selectable").bind("mousedown", function(e) {
            e.metaKey = true;
        }).selectable({
            stop: function() {
                $( ".ui-selected", this ).each(function(i, val) {
                    var index = $( "#selectable li" ).index( this );
  		    $("input:hidden.selectResult:eq(" + i + ")").val(index + 1);
                });
            }
        });
    });

Open in new window

With :
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />
<input type="hidden" class="selectResult" name="selectResult[]" />

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now