how to add check bo0x in selection box with multiple selection box - php

I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.

My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);

Related

How to echo selected values on multiselect from database on a multiselect form

i have a registration form using multiselect which insert options in the value 1,2,3,4,5,6,7 based on selection
However, after inserting i want to provide an option for editing where users edit
and i want the already selected values from database to be preselected while looping other options as unselected
I am experiencing unidentified offset 3, unidentified offset 3........
<div class="form-group">
<label for="touch-spin-1">Course Days</label>
<select multiple="multiple" name="coursedays[]" style="width: 100%;" data-toggle="select2" data-placeholder="<?php //echo $course_days; ?>" data-allow-clear="true" >
<?php
$entities = "1,2,3,4,5,6,7"; $entity = explode(",",$entities); $servs = explode("," , $course_days); $arr_length = count($entity); for($i=0;$i<=$arr_length;$i++){
?>
<option <?php if (in_array($servs, $entity)) { echo "selected";} ?> value="<?php echo $servs[$i]; ?>"> <?php echo $servs[$i]; ?></option>
<?php } ?></select></div>
try in this way
<?php
$course_days = "2,3,4";
$entities = "1,2,3,4,5,6,7";
$entity = explode(",", $entities);
$servs = explode(",", $course_days);
foreach($entity as $en) {
?>
<option value="<?php echo $en; ?>" <?php echo (in_array($en, $servs)) ? 'selected' : ''; ?>>
<?php echo $en; ?>
</option>
<?php } ?>

PHP getting value from a dynamic select list

I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks
You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"

Multiselect Checkbox Validation in PHP

I am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.
My Form
<?php
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title;
}
} ?>
try this code
<?php
$k==0;
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title;
$k++;
}
} ?>

How do I make values stay in chekbox?

How do I make the values stay in checkboxes?
my problem is when I submit the form the values do not stay (in the form).
Below is my code :
Mca<input type="checkbox" name="qual[]" id="Mca" value="Mca"
<?php if($qual == "Mca") { echo ' checked="checked"' ; } ?>>
Mtech<input type="checkbox" name="qual[]" id="Mtech" value="Mtech"
<?php if($qual == "Mtech") { echo "checked"; } ?>>
Btech<input type="checkbox" name="qual[]" id="Btech" value="Btech"
<?php if($qual == "Btech") { echo "checked"; } ?>>
Try this because $qual is an array.
/* Your $qual should be */ <?php $qual = $GET['qual']; ?>
<?php if($qual == "Mca")
must be changed to :
<?php if(in_array("Mca",$qual)
Try using better code for your form:
<?php
$checkboxes = array('Mca', 'Mtech', 'Btech');
foreach($checkboxes as $k => $v){
echo '<input '.($v==$qual[$k]? 'checked="checked" ': '').'type="checkbox" name="qual[]" id="'.$v.'" value="'.$v.'">';
}
?>

Trying to create an array condition

I have a text field which the user can enter comma separated list and then php converts it to a drop select list however I want a condition that will show a text input field if only a single value is entered. I tried the code below but it is only returning the select box even with a single entry. How can I achieve this condition?
<?php $listval = explode(",",$vals);
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
explode will always return an array. So therefor is_array will always be true.
Change your if statement to this
if(sizeof($listval) > 1)
try This
<?php $listval = strpos(',',$vals)?explode(",",$vals):$vals;
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
Please try to use the follwing code:
<?php $listval = explode(",",$vals);
if(count($listval) > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php } elseif(count($listval) == 1) { ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
As #Kris indicated, you can check with,
count($array)
or
sizeof($array)
<?php $listval = explode(",",$vals);
$array_count = count($listval);
if($array_count > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>

Categories