How to Echo Selected Value of Dropdown in PHP

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



Leave a reply



Submit