Multiple selection input checkbox materialize does not work

Asked

Viewed 392 times

0

I’m using the Materialize library, taking the database data and listing in front-end, but the multiple selection does not work:

<div class="row">
	<form action="#">
		<span>
			<?
			$sql = "SELECT * from $ufEstab where cidade = '{$cidadeEstab}' group by bairro";
			$query = mysqli_query($conexao,$sql);
			while ( $ln = mysqli_fetch_array($query)) {?>
				<div class="col s4">
					<input value="<?= utf8_encode($ln['bairro']);?>" type="checkbox" name="bairro" id="test5" />
					<label for="test5"><?= utf8_encode($ln['bairro']);?></label>
				</div>
			<? }//fim do while bairros ?>
		</span>
		<button class="btn">Cadastrar bairros</button>
	</form>
</div><!--row-->

  • I think you should use a select with the Multiple attribute. <select Multiple> <option value="" disabled Selected>Choose your option</option> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select>

  • Might explain better what’s not working ?

  • It does, except that you are ordering PHP to define all checkboxes with the same value in name (neighborhood), so when sending the form, one element will overwrite the other so that it only looks like a single element.

1 answer

1

Change of:

<input value="<?= utf8_encode($ln['bairro']);?>" type="checkbox" name="bairro" id="test5" />

for:

<input value="<?= utf8_encode($ln['bairro']);?>" type="checkbox" name="bairro[]" id="test5" />

Adding to name of the input the [], when submitting the form it will be sent as a multi choice array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.