I would like to be able to dynamically change how many option values are available, so that there are always "$i" values, like in the following.
<form action="scheduled.php" method="post" id="fields">
<p>Teams Playing</p>
<SELECT NAME="Teams[]" MULTIPLE SIZE=<?php echo htmlspecialchars($i); ?>>
<OPTION value="<?php echo htmlspecialchars($team[0]); ?>"><?php echo htmlspecialchars($team[0]); ?>
<OPTION value="<?php echo htmlspecialchars($team[1]); ?>"><?php echo htmlspecialchars($team[1]); ?>
<OPTION value="<?php echo htmlspecialchars($team[2]); ?>"><?php echo htmlspecialchars($team[2]); ?>
<OPTION value="<?php echo htmlspecialchars($team[3]); ?>"><?php echo htmlspecialchars($team[3]); ?>
...
<OPTION value="<?php echo htmlspecialchars($team[$i]); ?>"><?php echo htmlspecialchars($team[$i]); ?>
</SELECT>
<input type="submit">
</form>
I already suspect my code is sloppy for using all of the "?php echo"s. Is there any way I can make a for loop so that there are always "$i" options displayed in this format?
(Posted on behalf of the OP).
Here was my solution:
<form action="scheduled.php" method="post" id="fields">
<p>Teams Playing</p>
<SELECT NAME="Teams[]" MULTIPLE SIZE=<?php echo htmlspecialchars($i); ?>>
<?php
for ($x=0; $x<$i; $x++){
echo "<OPTION value=".htmlspecialchars($team[$x]).";>";
echo $team[$x];
}
?>
</SELECT>
<input type="submit">
</form>
Related
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>
This i my View code:
<form role="form" action="<?php echo base_url() ?>add_customer" method="post">
<select class="form-control" id="customer_id" name="customer_id">
<?php foreach ( $customer as $cust ){?>
<option value="<?php echo $datas[0]->customer_id; ?>"<?php if($cust->customer_id==$datas[0]->customer_id) echo 'selected="selected"'; ?>> <?php echo $cust->customer_id; ?></option>
<?php }?>
</select>
</form>
For Eg: Dropdown Value contains 1,2,3,4 actually selected value is 2 display in that field correctly. Now, I want to update the dropdown value to 4
How can I do this? Please help..
Hope this will help you :
Change values from value="<?php echo $datas[0]->customer_id; ?>" to this value="<?=$cust->customer_id;?>"
<select class="form-control" id="customer_id" name="customer_id">
<?php foreach ( $customer as $cust ){?>
<option value="<?=$cust->customer_id;?>" <?php if($cust->customer_id == $datas[0]->customer_id) echo 'selected="selected"'; ?> > <?php echo $cust->customer_id; ?></option>
<?php }?>
</select>
I have datalist like this :
<input type="text" list="colours" id="txt">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
The option that datalist has was populated from a foreach loop using php.
How do I disable the datalist input, if the data that foreach loop's result is null/none using jquery ?
like for example
Have Option
<input type="text" list="colours">
No Option
<input type="text" list="colours" disabled="disabled">
Here is an example of keeping your PHP and HTML separate:
<?php
$option="";
foreach ($kondisi as $kondisi) {
if($kondisi != 'null' || $kondisi != 'none'){
$option .= '<option data-value="'.$kondisi'.->nama_kondisi" value="'.$kondisi.'->id_kondisi">'.$kondisi.'->nama_kondisi</option>';
}else{
$option .= 'your disabled option code here...';
}
}
?>
<input type="text" list="colours">
<datalist id="colours">
<?php echo $option; ?>
</datalist>
If you want to disable it then do like below:-
<?php if(count($kondisi)>0){?>
<input type="text" list="colours">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
<?php }else{?>
<input type="text" list="colours" disabled="disabled"/><!-- or use autocomplete="off"-->
<?php } ?>
i´m kind of a noob in php things and i got the following problem:
i got a plugin which helps me making a website with 2 languages. So i tell the plugin which languages i want to have in my contao-backend and then i can use them in the frontend. I can then chose wether i want to be able to switch the language by clicking on a flag or by clicking the language in a select box. But there is no option to just have the languages next to each other without a flag. Just a text like "German Spanish". its always a select box or flags.
I then opened the template for the select box and it tells me this:
<?php
/**
Menu for switching between languages of a page.
*/
?><form name="<?php echo $this->type;?>" method="post" style="display:inline"
><select name="language" onchange="this.form.submit();">
<?php foreach ($this->items as $item): ?>
<option value="<?php echo $item['language'];?>" <?php
if ($item['isActive']) {
echo ' class="active" selected="selected"';
} ?>><?php
echo $this->languages[$item['language']];
?></option>
<?php endforeach; ?></select><input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}" /></form>
(This is the one where i can switch by a select box)
and:
<form name="<?php echo $this->type;?>" method="post" style="display:inline"
><?php foreach ($this->items as $item): ?><input
class="language" type="radio" name="language"
id="language_<?php echo $item['language'];?>"
onchange="this.form.submit();"
value="<?php echo $item['language'];?>" <?php
if ($item['isActive']) {echo ' class="active" checked="checked"';} ?> />
<label for="language_<?php echo $item['language'];?>" <?php
if ($item['isActive']) {echo ' class="active"';} ?>><img src="<?php
echo 'system/modules/i18nl10n/html/flag_icons/png/'.$item['language'].'.png';?>"
title="<?php echo $this->languages[$item['language']];?>"
alt="<?php echo $this->languages[$item['language']];?>"
/></label><?php endforeach; ?><input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}"></form>
(this is the one which lets me change the language by clocking on the flag)
....
my question now is if someone knows how i can edit this code in a way that it simply shows me the languages i can chose in a text like "german spanish".
Sorry for my english :P
greetings
You can just remove the <img> tag and replace it with the language name:
(I have also improved the code formatting for better readability)
<form name="<?php echo $this->type;?>" method="post" style="display:inline">
<?php foreach ($this->items as $item): ?>
<input class="language" type="radio" name="language"
id="language_<?php echo $item['language'];?>"
onchange="this.form.submit();"
value="<?php echo $item['language'];?>"
<?php
if ($item['isActive']) {
echo ' class="active" checked="checked"';
}
?>
/>
<label for="language_<?php echo $item['language'];?>"
<?php if ($item['isActive']) { echo ' class="active"'; } ?>
>
<?php echo $this->languages[$item['language']];?>
</label>
<?php endforeach; ?>
<input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}">
</form>
I have a select box like this. This is nothing but looping from 1 to 10.
<select name="author_box[]" id="author_box[]">
<?php for($j=1;$j<=10;$j++){ ?>
<option id="<?php echo $j; ?>" name="<?php echo $j; ?>" value="<?php echo $$j; ?>"><?php echo $j; ?></option>
<?php } ?>
</select>
For example: I have a variable $tmp_rating="5". So what I want to display is showing the value 5 in the select box along with the other values (i.e. from 1 to 10).
How can I achieve that. In short, the value which I fetch from the database should be displayed first in the select box.
Thank you gentleman, for all the time and instant reply. This portal helps me a lot in fixing my issues in less time.
You have to use the selected attribute for the option:
<select name="author_box[]" id="author_box[]">
<?php for($j=1;$j<=10;$j++){ ?>
<option value="<?php echo $j; ?>" <?=($tmp_rating==$j)?'selected="selected"':null;?>><?php echo $j; ?></option>
<?php } ?>
</select>
This way, if $tmp_rating is the same as $j in your loop, the attribute selected is added and the correct value displays in your selectbox.
By adding this condition
<?php if($j == $tmp_rating) echo 'selected="selected"'; ?>
Full code
<option <?php if($j == $tmp_rating) echo 'selected="selected"'; ?> id="<?php echo $j; ?>" name="<?php echo $j; ?>" value="<?php echo $$j; ?>"><?php echo $j; ?></option>