Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  • Thread Tools
  • Rate This Thread
  1. #1
    New to the CF scene
    Join Date
    Apr 2016
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    putting selected into hidden textbox

    Like my other post my form allows the user

    In my form I have 2 dropdownlists that allow multiple selections, i need to put the users selections into a hidden textbox so i can pass the values to the next page via post. I cannot use $_POST['languages'] to get the values as it returns all the users selections form the all the dropdowns as a single array and i cannot tell what values came from what dropdown. So my thinking is to use jquery to populate a hidden textbox, however my code doesnt work once i add another dropdown. I tried using class="languages" and .languages but this doesnt work.

    Here is the POST result for the dropdown.
    Code:
    ["languages"]=> array(7) {
    => string(9) "Bulgarian" [1]=> string(7) "English" [2]=> string(7) "Finnish" [3]=> string(6) "French" [4]=> string(7) "English" [5]=> string(7) "Finnish" [6]=> string(6) "French" }
    Here is my code for the dropdown.
    Code:
    <script>
    function addTableRow(tableID) {
    	var table = document.getElementById(tableID);
    	var rowCount = table.rows.length;
    	if(rowCount < 10){
    		var row = table.insertRow(rowCount);
    		var colCount = table.rows[0].cells.length;
    		for(var i=0; i<colCount; i++) {
    			var newcell = row.insertCell(i);
    			newcell.innerHTML = table.rows[0].cells[i].innerHTML;
    		}}else{ alert("Maximum number is 10");	}}
    
     $(function(){
    	   $( "#txtdate" ).datepicker({ dateFormat: 'dd/mm/yy', yearRange: "2016:2020", showOtherMonths: true, selectOtherMonths: true});  });
    </script>
    
    <input type="button" value="add row" onClick="addTableRow('tbl')">
    <table id="tbl">
    <tr><td>
    <input type="hidden" name="txtlang[]" id="txtlang">
    <label for="languages" class="languages">Select Required Languages</label>
    <?php $languages = getLanguageList();?>
    <select name="languages[]" id="languages" multiple>
     <?php 
    foreach ($languages as $key => $value){
    echo "<option value=\"".$key."\">".$value."</option>";
    }
    ?>
    </select>
    <script type="text/javascript">
    function getLanguageSelection() {
     var langSelection = $( "#languages" ).val() || [];
     $( "#txtlang" ).val( langSelection.join( ", " ));
    }
    $( "#languages" ).change( getLanguageSelection );
    </script>

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,303
    Thanks
    30
    Thanked 864 Times in 862 Posts
    I don't see how your sending this to a PHP script, but maybe this is the problem:
    <select name="languages[]" id="languages" multiple>
    If this [the selects] are in a form and this is sent to the PHP script with a SUBMIT button. Then the value selected is contained in $_POST["languages"] if you change name="languages[]" to name="languages" so it's not an array but a single value.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,131
    Thanks
    3
    Thanked 814 Times in 803 Posts
    Just use a different name for each select - a JavaScript solution will not work for all visitors so you always need a non-JavaScript fallback with forms.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •