I have a dropdown and I want the input selected after the page refresh or post. So every post has the same $hoogte_array.
<table><form action="index.php" method="post">
<tr><th>Hoogte: <select name="hoogte">
<?
$hoogte_array[1]= 63;
$hoogte_array[2]= 103;
$hoogte_array[3]= 123;
$hoogte_array[4]= 153;
$hoogte_array[5]= 173;
$hoogte_array[6]= 203;
$kleur_array[1] = "groen";
$kleur_array[2] = "blauw";
foreach ($hoogte_array as $key => $valuehoogte)
{
echo "<option value='".$key."''>".$valuehoogte."</option>";
}
?>
</select></th>
<th>Kleur: <select name="kleur">
<?
foreach ($kleur_array as $key => $valuekleur)
{
echo "<option value='".$key."''>".$valuekleur." </option>";
}
?>
foreach ($hoogte_array as $key => $valuehoogte)
{
$hoogte = (isset($_POST['hoogte']))?$_POST['hoogte']:"1";
$sel = ($hoogte == $key)? 'selected="selected"' : '';
echo "<option value='".$key."'' ".$sel.">".$valuehoogte."</option>";
}
Related
I have made many select box in PHP and I want to keep selected item as selected after refreshing the page. (when selecting same select box or another) here is my code.
$selectbox='<select class="form-control" name="estate_id" onchange="this.form.submit()" style="width: 200px" >';
$est_name = $client ->call('get_estate'); // call method from web services
$_SESSION['estname'] = array();
$_SESSION['estname'] = $est_name;
$count = count($_SESSION['estname']);
$i = 0;
foreach ($_SESSION['estname'] as $row)
{
$id = $_SESSION['estname'][$i]['est_id'];
$name = $_SESSION['estname'][$i]['est_name'];
if($id == isset($_POST['estate_id']))
{
$isSelected = ' selected="selected"';
}
else {
$isSelected = '';
}
$selectbox.= "<option value=".$id.$isSelected.">".$name."</option>";
$i++;
}
$selectbox.='</select>';
echo $selectbox;
<select name="name">
<option <?php if ($_GET['name'] == 'a') { ?>selected="true" <?php }; ?>value="a">a</option>
<option <?php if ($_GET['name'] == 'b') { ?>selected="true" <?php }; ?>value="b">b</option>
</select>
im new to PHP and im trying this stuff for hours, I wanted to show the Array items to the Select Option. Here's the code:
<select name="state">
<?php
$arrstate=array
(
array("AK","Alaska"),
array("AL","Alabama"),
array("AR","Arkansas"),
array("AZ","Arizona"),
array("CA","California"),
array("CO","Colorado"),
array("CT","Connecticut"),
array("DC","District of Columbia"),
array("DE","Delaware"),
array("FL","Florida"),
array("GA","Georgia"),
array("HI","Hawaii"),
array("IA","Iowa"),
array("ID","Idaho"),
array("IL","Illinois"),
array("IN","Indiana"),
array("KS","Kansas"),
array("KY","Kentucky"),
);
for($lop=0;$lop<=49;$lop++)
{
if (strtoupper($lead_info['state'])==$arrstate[$lop][0])
{
echo "<option selected=\"selected\" value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";
}else{
echo "<option value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";
}
}
?>
</select>
But it seems it only shows ".$arrstate[$lop][1]."
What seems to be the problem?
try it
<?php
$arrstate = array
(
'AK' => 'Alaska',
'AL' => 'Alabama',
'AR' => 'Arkansas',
'AZ' => 'Arizona'
); ?>
<select>
<?php foreach($arrstate as $key => $value) { ?>
<option value="<?php echo $key; ?>" <?=($value == $current_zip_entered ? "selected" : "" )?>><?php echo $value; ?></option>
<?php } ?></select>
<?php
$arrstate=array
(
"AK","Alaska",
"AL","Alabama",
"AR","Arkansas",
"AZ","Arizona"
);
?>
this is the easiest way...
<select>
<?php
foreach($arrstate as $key => $value) {
?>
<option value="<?php echo $key; ?>" <?=($value == $current_zip_entered ? "selected" : "" )?>><?php echo $value; ?></option>
<?php
}
?>
</select>
You can retrieve from database like this example:
<select>
<?php $result = mysqli_query($conn, "SELECT * FROM categories");
while ($row = mysqli_fetch_array($result)) {
$categories = array($row["category"]);
$arrlength = count($categories);
for($x = 0; $x < $arrlength; $x++) {
echo "<option>";
echo $categories[$x];
echo "</option>";
}}?>
</select>
I am trying to make the select box sticky that is located in the first foreach loop:
I believe there should be an if statement located inside the <option> tag like below:
if($selectedMake==$key){
echo selected='selected';
}
echo "<option im not sure how to properly enter it in here?>$key</option>"
// Start of code below:
$selectedMake = $_POST['make'];
$cars = array(
'Toyota'=>array(
'Corolla'=>array(
'image'=>'corolla.png',
'colour'=>'blue',
'transmission'=>'manual',
'doors'=>'2'
),
'Highlander'=>array(
'image'=>'highlander.png',
'colour'=>'silver',
'transmission'=>'auto',
'doors'=>'4'
),
),
'Mazda'=>array(
'RX7'=>array(
'colour'=>'blue',
'transmission'=>'manual',
'doors'=>'2'
),
'MX-5'=>array(
'colour'=>'red',
'transmission'=>'manual',
'doors'=>'2'
)
)
);
echo '<form method="post" action="cars.php">';
echo '<select name="make">';
foreach ($cars as $key => $value) {
echo "<option>$key</option>"; // This option tag needs to be made sticky
}
echo '</select>
<input type="submit" name="submit">
</form>
';
if (isset($_POST['submit'])) {
$selectedMake = $_POST['make'];
echo "<h1>$selectedMake</h1>";
foreach ($cars as $key => $value) {
if ($selectedMake == $key) {
foreach ($value as $key => $value) {
echo "<b>$key</b> <br>";
foreach ($value as $key => $value) {
if ($key == 'image') {
echo '<img src="imgs/'.$value.'" width="150px">';
} else {
echo "<li>$key: $value</li>";
}
}
echo "<br>";
}
}
}
} else {
echo "Not clicked";
}
echo '<pre>',print_r($cars),'</pre>';
?>
If by sticky, you mean selected if it matches the value you have then:
echo "<option".($selectedMake==$key ? " selected" : "").">$key</option>"
Ah, I think I know what you mean. You want something like this...
<select name="make">
<?php
foreach (array_keys($cars) as $key) :
$selected = $key === $selectedMake ? ' selected' : '';
?>
<option<?= $selected ?>><?= htmlspecialchars($key) ?></option>
<?php endforeach ?>
</select>
I cannot seem to find my error, my dropdown list is empty but there are values in the array when i do a print_r($data).
My code:
<?php $sql = "SELECT p.firstname, p.lastname from person p where p.personid >= :personid";
$array = array('personid' => 75101);
$sth = $dbh->prepare($sql);
if($sth->execute($array)) {
$data = $sth->fetchAll();
} ?>
<tr><td style="width:120px;">Contact Person</td>
<td>
<select>
<?php foreach ($data as $key => $value) { ?>
<option value=" <?php $key; ?>" > <?php $value ?> </option>
<?php } ?>
</select>
</td>
can some one help please?
thanks
try this...
<?php $sql = "SELECT p.firstname, p.lastname from person p where p.personid >= :personid";
$array = array('personid' => 75101);
$sth = $dbh->prepare($sql);
if($sth->execute($array)) {
$data = $sth->fetchAll();
} ?>
<tr><td style="width:120px;">Contact Person</td>
<td>
<select>
<?php foreach ($data as $key => $value) { ?>
<option value=" <?php echo $key; ?>" > <?php echo $value; ?> </option>
<?php } ?>
</select>
</td>
Try this
<?php foreach ($data as $key => $value) { ?>
<option value=" <?php echo $key; ?>" > <?php echo $value; ?> </option>
<?php } ?>
You cant print a variable without using echo , you forgot to print the $key and $value ,
change
<option value=" <?php $key; ?>" > <?php $value ?> </option>
with
<option value=" <?php echo $key; ?>" > <?php echo $value; ?> </option>
You are not print variables. Only use in your code.
If You need short tags, try this code:
<?php foreach ($data as $key => $value) { ?>
<option value="<?=$key;?>"><?=$value["firstname"]." ".$value["lastname"];?></option>
<?php } ?>
But if on your server short tags is disabled then try code from previous replay, only $value variables replace with $value["firstname"].
i would like to extract all the values that correspond to the organisations to come under the drop down menu as options for organisation,but now only the last value of the oraganisation is showing up in the drop down as an option
here is my code
<?php
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
echo $each->location[0]->building[0];
echo $each->location[0]->name;
echo $each->name;
}
$org=$each->name;
$arr=array($org);
reset($arr);
//print_r($org);
//$result = count($org);
//echo $result;
while(list(,$value)=each($arr)){
//echo "value:$value<br/>\n";
//$_SESSION['organisation']=$value;
//echo $_SESSION['organisation'];
}?>
<select name="category_id">
<option value=""></option>
<?php
$keys = array_keys($arr);
$count1=count($keys);
echo $count1;
for($i=0; $i<count($arr); $i++)
{?>
<option value="<?php echo $keys[$i]; ?>"><?php echo $arr[$i]; ?></option>
<?php
}
?>
</select>
Make use of that first foreach rather than starting an entire new loop. Tested and this works:
<?php
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
echo '<select name="category_id"><option value=""></option>';
$stepper = 0;
foreach($items as $each) {
$building = $each->location[0]->building[0];
$name = $each->location[0]->name;
$final_name = $each->name;
echo '<option value="'.$stepper.'">'.$final_name.'</option>';
$stepper++;
}
echo '</select>';
?>
Use this code
<?php
$org = array();
$items = json_decode('[{"location":[{"building": ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
$org[]=$each->name;
}
?>
<select name="category_id">
<option value=""></option>
<?php
foreach($org as $key=>$val)
{?>
<option value="<?php echo $key; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>