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>
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'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];?>"
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>
This may seem like a basic question but I hope somebody could help me out.
I have a created a widget for a custom post type so that I could show the post types in a dynamic sidebar. In the widget, I have also created a dropdown that gets the categories to show.
My problem is that I also want to have the option to show all the categories and not just 1 category for the dropdown. Here's the code I have so far:
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>
Should I just use a multi-select for this and how do i go about it?
this will echo out all your categories but you'll have to update the selected part to match your instance.
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
with your full code
<select id="<?php echo $this->get_field_id('articleCategory'); ?>" name="<?php echo $this->get_field_name('articleCategory'); ?>">
<?php $arr = get_categories(); ?>
<option value="<?php foreach($arr as $option) {echo $option->term_id.','; };?>"><?php echo $arr; ?>All Categories</option>
<?php foreach($arr as $option) { ?>
<option <?php echo $instance['articleCategory'] == $option->term_id ? 'selected="selected"' : '';?> value="<?php echo $option->term_id; ?>"><?php echo $option->name; ?></option>
<?php } ?>
</select>
I have an html dropdown box. Then I use an array to fill the items in it.
The keys in this for each loop is just a number from 0 - 9. My problem now is how can I control what shows up as the default choice:
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>" selected="<?php if($k==1){ echo "selected"; } ?>"><?php echo $k + 1; ?></option>
<?php } ?>
In this code, you can see that I'm attempting to make the 2nd item as the default choice.
But it seems like I'm always brought to the last array item whatever number I type as the condition.
Try this:
<?php foreach($cat_r as $k=>$c){ ?>
<option value="<?php echo $k + 1; ?>" <?php if($k==1){ echo 'selected="selected"'; } ?>><?php echo $k + 1; ?></option>
<?php } ?>
Or, this format works too
<option value="foo" selected />
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>"
<?php if($k==1){ echo "selected=\"selected\""; } ?>>
<?php echo $k + 1; ?>
</option>
<?php } ?>
I came here for finding solution to set options selected by user before as selected , above question was in different context and I ended up in scratching my head to this thing...
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}
This code was functional for me, thanks..
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}