Avatar of peter-cooper
peter-cooper

asked on 

How do I Enable submit button only if listbox has items

initially i have the submit disabled so users cannot submit an empty list. Is there way with my existing code to detect if a list has items and enable the submit button. However if a user removes items then make it false .

I am using jquery to populate a listbox which a contributor helped me with and wodering if that could be modified to check for items in a list and enable the submit button.

Thanks

php

<?php
	$conn = mysql_connect("localhost","root","");
	mysql_select_db("sample",$conn); 
	$result = mysql_query("SELECT * FROM boxes where department = '{$_GET['dept']}' ORDER BY custref ASC");
?>
								
	<select name="boxdest[]" id="boxdest" size="7" multiple="multiple">

<?php
	$i=0;
	while($row = mysql_fetch_array($result)) {
?>
	<option value="<?php echo $row["custref"];?>"><?php echo $row["custref"];?></option>
<?php
	$i++;
	}
?>
	</select>
	<span style="display: inline-block; width: 70px; height: 82px; padding-left: 10px; padding-right: 10px; vertical-align: top;margin-top:35px;">
          <button class="btn btn-primary switch-item" data-src="boxdest" data-dst="boxdest2">&gt;</button>
    	  <button class="btn btn-primary switch-item" data-dst="boxdest" data-src="boxdest2">&lt;</button>
	  </span>
	  <select name="boxdest2[]" id="boxdest2" size="7" multiple="multiple"></select>

Open in new window


jquery

<script>
		$(function () {
			$('.switch-item').click(function (e) {
				e.preventDefault();
				var src = $(this).data('src');
				var dst = $(this).data('dst');
				var sel = $('#' + src + ' option:selected').detach();
				$('#' + dst).append(sel);
				
			});
			
			$('form').submit(function () {
				$('#boxdest2 option').prop({
					selected: true
				});
			});
			
		});
	</script>

Open in new window

PHPjQueryHTMLJavaScriptMySQL Server

Avatar of undefined
Last Comment
Nicholas

8/22/2022 - Mon