Trying to Echo a value from a select drop down in PHP
You need to give your dropdown a name:
HTML:
<li>
<select name="uDraft">
<option value="Draft Offense">Draft Offense</option>
<option value="Draft Defense">Draft Defense</option>
<option value="Trade them, we can't pick good anyways">Trade them, we can't pick good anyways</option>
</select>
</li>
PHP:
if(isset($_GET["uDraft"])){
$draft= $_GET["uDraft"];
echo $draft;
}
Set dropdown list value inside PHP Echo
Let's try this
<?php $temp_val = 3; ?>
<td>
<select>
<?php
For($i=0;$i<=5;$i++){
?>
<option value="<?= $i; ?>" <?php if($temp_val == $i) echo("selected")?>>
<?= $i ?>
</option>
<?php } ?>
</select>
</td>
Happy coding :)
How can I echo out the users dropdown selection?
I suggest reading PHP's Dealing with Forms tutorial and also researching the change event.
For your code example, I've added a form element along with an "onchange" attribute to the select element which will automatically submit the form when a number is chosen.
<html>
<head>
</head>
<body>
<form action="<?php echo basename(__FILE__); ?>" method="post">
<label for="numbers">Numbers:</label>
<select name="numbers" onchange="document.forms[0].submit();">
<option value="0">select number</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit">
</form>
<?php
if (array_key_exists('numbers', $_POST)) {
$numbers = $_POST['numbers'];
echo '<p>' . $numbers . '</p>';
}
?>
</body>
</html>
How do I set the selected item in a drop down box
You need to set the selected attribute of the correct option tag:
<option value="January" selected="selected">January</option>
Your PHP would look something like this:
<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>
I usually find it neater to create an array of values and loop through that to create a dropdown.
How to echo selected option value. Not select name
You're setting the value to "NEE_category" instead of the values you're pulling from your database.
echo "<option value=\"". $row['catDesc'] ."\">". $row['catDesc'] ."</option>";
How to show a selected value in a dynamic drop down in PHP
You can approach this as
<?php
$selectedOption = '';
if($_POST){
$selectedOption = $_POST['selClass'];
}
?>
<form action="" method="POST" name="form1" id="form1">
<select name="selClass" size="1" id="selClass" onchange="form1.submit()">
<option value="">Select a class</option>
<?php
echo "<option value='". "All records". "' . >" . "all records". "</option>";
while ($row1 = mysqli_fetch_array($rs5)) {
if($row1["class"] == $selectedOption)
echo "<option value='".$row1["class"] ."' selected='selected'>" . $row1["class"]. "</option>";
else
echo "<option value='".$row1["class"] ."'>" . $row1["class"]. "</option>";
}
?>
</select>
</form>
Related Topics
Sending Message in Telegram Bot With Images
How to Convert Text to Svg Paths
How to Get Next Id of Autogenerated Field in Laravel for Specific Table
Change Language of Site With a HTML Button
How to Replace Newline or \R\N With <Br/>
Remove Comma on Last Item of Foreach
Codeigniter Call to a Member Function Num Rows on Boolean
SQL Injection That Gets Around mysql_real_escape_string()
How to Get a Youtube Video Thumbnail from the Youtube API
How to Download an Mp3 File from Remote Url
How to Center the Text in Phpexcel Merged Cell
How to Update Field to Add Value to Existing Value
Multiple Column With Same Search String in Laravel
Laravel 5 - How to Access Image Uploaded in Storage Within View
Getting Data from a Textarea into a Database