Iam trying a solution in PHP/MYSQL where in there is a some country list with a checkbox. Certain countries have something called entities which is a drop down which shows Micro, Small, Large as a dropdown. This entity dropdown appears for certain countries only. I am able to pass the checkbox checked values to next page. But i need to pass the value of the entity selected in the drop down also. Iam not able to acheieve that. My code ie posted below. Here i need to pass the value of the drop down (select - entity_selected) also to next page:
<?php
echo '<table border="1">';
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1["country"];
$entity1 = $row1["entity"];
echo '<tr>';
?>
<td><input name="check_list[]" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?></td>
<td>
<?php
if($entity1 == 'Yes'){ ?>
<select class="form-control selectpicker" name="entity_selected">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php }
?>
</td>
<?php echo '</tr>'; } ?>
</table>
The Next page code is below to get the check box selected countries.
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check;
}
}
How can i get the drop down values here? iam getting the checkbox (countries values correctly). Can anyone pls help me on this pls?
<form method="POST" action="nextpage.php">
<table border="1">
<?php
$i = 0;
while($row1 = mysqli_fetch_array($result_country)) {
$country1 = $row1['country'];
$entity1 = $row1['entity'];
echo "<tr>";
?>
<td>
<input name="check_list<?=$i ?>" type="checkbox" value="<?php echo $country1;?>"> <?php echo $country1;?>
</td>
<td>
<?php
if($entity1=="Yes") {
?>
<select class="form-control selectpicker" name="entity_selected<?=$i ?>">
<option value="Micro">Micro</option>
<option value="Small">Small</option>
<option value="Large">Large</option>
</select>
<?php
};
?>
</td>
<?php
$i++;
echo "</tr>";
};
?>
</table>
</form>
nextpage.php:
<?php
$entities = $checklist = [];
foreach($_POST as $k => $v) {
if(preg_match("/^checklist(\d+)$/", $k, $matches))
$checklist[intval($matches[0])] = $v;
unset($matches);
if(preg_match("/^entity_selected(\d+)$/", $k, $matches))
$entities[intval($matches[0])] = $v;
};
?>
Related
I have this loop coded to create a dropdown with the numbers 0-30.
<td style="text-align:left;">
<select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i=0; $i<=30; $i++) {
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
</td>
I cannot seem to figure out how to get the value of I to work in my calculations on an order processing form.
I assign the variable using
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
but if I try to output $iPhone12 * 15 for example the answer is always zero.
Make sure you select is wrapped in a form element with method="POST". You should also use isset to make sure it exists.
<form action="" method="post">
<td style="text-align:left;"><select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i = 0; $i <= 30; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select></td>
<button>submit</button>
</form>
<?php
if (isset($_POST['iPhone12Case'])) {
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
printf($iPhone12 * 15);
}
I have some options to select and would like to set some textinputs to visible or hidden depending on what user selects. I have the functionality already but in this case I need to take the second value of the echo and check if its equal to something, I can currently only check the first value of the echo:
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
So, I need to get ONLY the value of $row['serial_or_imei'] and compare onchange="if (this.value=='') , how can I do that?
EDIT: further explanation
I need to read the value of $row['serial_or_imei'] which could be empty, 'serial' OR 'imei', so, one of these three.
P.S: I must keep the $row['id'] in the value attribute because I am passing that value later to server so dont suggest removing that.
You can add one attribute to option element and test with it. something like
<select id="type_id" name="device_type_id" onchange="if (this.getAttribute('data')==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option data="<?php echo $row['serial_or_imei'] ?>" value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You can compare the value of an array in the onchange by assign the value to a variable which will be a quite neat format as
<?php $value = echo $row['serial_or_imei']; ?>
<select id="type_id" name="device_type_id" onchange="if (this.value=='<?php empty($value)?>'){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
You add a new custom attribute serial and supply the value of serial_or_imei to it and then compare in javascript with onchange="if (this.value=='')
<select id="type_id" name="device_type_id" onchange="if (this.value==''){this.form['device_imei_textinput'].style.visibility='hidden'}else{this.form['device_imei_textinput'].style.visibility='visible'};">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-serial="<?php echo $row['serial_or_imei'] ?>"> <?php echo $row['name']; ?> </option>;
<?php } ?>
</select>
EDIT: Attribute “serial” is not allowed on element “option”. Changed this to data-seial.
You say you need the values back to server because they change dynamically. A clean way would be to have a dedicated hidden field:
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
Here's a full self-contained script to illustrate it:
<?php
$random_serial_or_imei = range(1000, 9999);
shuffle($random_serial_or_imei);
$result = [];
for ($i = 0; $i < 5; $i++) {
$result[] = [
'id' => $i,
'serial_or_imei' => $random_serial_or_imei[$i],
'name' => "Name #$i",
];
}
?>
<select name="device_type_id">
<option value="">Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id']?>"><?php echo $row['name']; ?></option>
<?php } ?>
</select>
<?php foreach ($result as $row) { ?>
<input type="hidden" name="map[<?php echo $row['id']; ?>]" value="<?php echo $row['serial_or_imei']; ?>">
<? } ?>
This prints something like:
<select name="device_type_id">
<option value="">Select</option>
<option value="0">Name #0</option>
<option value="1">Name #1</option>
<option value="2">Name #2</option>
<option value="3">Name #3</option>
<option value="4">Name #4</option>
</select>
<input type="hidden" name="map[0]" value="9513">
<input type="hidden" name="map[1]" value="3931">
<input type="hidden" name="map[2]" value="3971">
<input type="hidden" name="map[3]" value="4476">
<input type="hidden" name="map[4]" value="6429">
Selected type can be found at $_POST['device_type_id'] and the corresponding IMEI at $_POST['map'][ $_POST['device_type_id'] ].
Based on the information from the comments, I think this solution is what you are looking for. Note I move the onchange to a function for clarity.
You can see the "three" option hides the text input as it doesn't have a imei.
Also notice how I use the data- attribute inside your option so you can retrieve that information as well, independent of the option id. Also note the this.form['device_imei_textinput'] didn't worked. The elements reference is required.
function check_imei(obj, form) {
var imei = obj.options[obj.selectedIndex].getAttribute('data-imei');
if (imei == '') {
form.elements.device_imei_textinput.style.visibility='hidden';
} else {
form.elements.device_imei_textinput.style.visibility='visible';
}
}
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<option value="1" data-imei="foo">One</option>
<option value="2" data-imei="bar">Two</option>
<option value="3" data-imei="">Three</option>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</form>
Mirror of example : https://jsfiddle.net/asp1hewx/
EDIT : To address the PHP side of things, the form above could be generated with :
<form>
<select id="type_id" name="device_type_id" onchange="check_imei(this, this.form);">
<option value='Select'>Select</option>
<?php foreach ($result as $row) { ?>
<option value="<?php echo $row['id'] . $row['serial_or_imei'] ?>" data-imei="<?php $row['serial_or_imei'] ?>"><?php echo $row['name'] ?></option>
<?php } ?>
</select>
<input type="text" id="device_imei_textinput" name="device_imei_textinput" />
</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 } ?>
I am trying to populate a drop-down list of the database. In my view file I have the following code
Here is my controller
$query = $this->interprete_model->interpreteID($this->session->userdata('user_id'));
print_r($query);
$data['interprete'] = $query;
Aqui esta mi vista, usa set_select.
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($result as $row):?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, TRUE); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
Result:
enter image description here
Many selected, I need one selected to modify (update) the data.
You can try this :
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($users as $row):
$selected = FALSE;
// 1 is the id u want to be selected u can change it according to you
if ($row->id == 1){
$selected = TRUE;
}
?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, $selected); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
You can also use form_dropdown as
// FOR ids
$ids = array(1,2,3,4); // array of user ids
echo form_dropdown('regionI',$ids,1,array('class'=>'form-control'));
// FOR name
$names= array('name1','name2','name4','name3'); // array of user names
echo form_dropdown('regionI',$names,'name1',array('class'=>'form-control'));
For More :
https://www.codeigniter.com/user_guide/helpers/form_helper.html
i write this way for edit time selection
<?php foreach ($select_single as $select_single_show):?>
<select class="form-control" name="regionI">
<?php foreach ($users as $row):?>
<option <?php if($row->id==$select_single_show->regionI)echo "selected";?> value="<?php echo $all_branch_show->id?>"><?php echo $row->name?>
</option>
<?php endforeach;?>
</select>
<?php endforeach;?>
Hi I have a form with selector name "top" in PHP framework Codeigniter. When I submit the form, the input post does not show the value posted. Only when I submit the second time, it post the value. The code is below, please advise?
<div id="selector_form">
<?php
echo form_open(base_url().'home/choose_car/'); ?>
<select name="select_car">
<option>Please Select Car</option>
<?php
foreach ($list as $key => $value) {
if($value->model == $car[0]->model){
$selected = 'selected';
}else{
$selected = '';
}
//change make to carid
echo "<option ".$selected." value='".$value->carID."''>".$value->make." - ".$value->model."</option>";
}
?>
</select>
<textarea id="description" rows="4" cols="50" ><?php if(isset($car)&&$car){print_r($car[0]->desc);}else{echo "Please Select Car";} ?></textarea>
<input type="submit" />
</div><!-- end selector_form -->
<table id="controls_table">
<th>Section</th><th>Material</th><th>Color</th><th>Show</th>
<tr>
<td><label for="top">Top: </label></td>
<td><select name="top">
<?php
if($material){
foreach ($material as $key => $value) {
if($value->materialID == $this->input->post('top')){
$selected = 'selected';
}else{
$selected = '';
}
echo "<option ".$selected." value='".$value->materialID."'>Carbon/".$value->materialName."</option>";
}
}else{
echo "<option ".$selected." >Please Select Car</option>";
}
?>
</select></td>