Select option selected an not selected - php

I'm trying to Update a SQL Field so what i got is a Select and
$row = $stmt->fetch(PDO::FETCH_ASSOC);
....
$country_ausgabe = $row['country'];
So country_ausgabe as for example a value = de for german.
Also i have a array with all country codes in it.
So with this code i get all in a Select. $countries_de = the array with all country codes.
<select>
foreach ($countries_de as $key=>$country) {
echo "<option value='" . $key . "'>" . $country . "</option>";}
</select>
So but how can i use my $country_ausgabe to set this value in the select to selected?

use this code
<select>
foreach ($countries_de as $key=>$country) {
$selected = ($country_ausgabe == $key)?"SELECTED":"";
echo "<option value='" . $key . "' ".$selected." >" . $country . "</option>";}
</select>

try this
echo "<select>";
foreach ($countries_de as $key=>$country)
{
$selected="";
if($key==$country_ausgabe)
{
$selected = "selected";
}
echo "<option value='" . $key . "' " . $selected . " >" . $country . "</option>";
}
echo "</select>";

Related

Using Selected is not displaying anything in dropdownlist

I'm displayed a dropdown list of initials that come from a table. I insert "--" at the start before getting the initials from the table.
It goes:
AB
CD
EF
I want it to select the default. $init is the defaulkt
The code below works for '--' but not for the initials.
On the While, if I remove the if statement and just leave the 'else'
echo "<option value='" . $row['id'] . "'>" . $row['init'] . " ";
it is OK.
The first half of the if statement should be the same but with 'selected added.
I don't understand why it is not working.
All I get is "--", not the initials, or any other following input.
Thanks for your help.
<?php
echo $init;
// Drop down list initials
$conn=createconnection();
$sql = "SELECT * FROM employees WHERE lef='0' AND man='1' ORDER BY init";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Updated by and date<br>";
echo "<select name='select_initial' id='select_initial'>";
if ($init==="--"){
echo "<option value='" . '0' . "'selected>" .'--' . " </option>";
} else {
echo "<option value='" . '0' . "'>" . '--' . " </option>";
}
// output data of each row
while($row = $result->fetch_assoc()) {
if ($init===$row('init')) {
echo "<option value='" . $row['id'] . "'selected>" . $row['init'] . " </option>";
} else {
echo "<option value='" . $row['id'] . "'>" . $row['init'] . " </option>";
}
}
echo "</select>";
//echo "<br><br>";
}
$conn->close();
?>
I found if I used square brackets round the $row function it worked:
if ($init===$row['init']) {

Select default value from GET value

My website is using form editing to click on a record, which then opens up a form and allows for all of the values to be edited, and saved to append the new values. However the issue I am facing is the select, which should be using the default value from $_GET, but instead the default is coming up as the value which is first in alphabetical order, not what it should actually be. So the issue is if i edit and save, the form is automatically placing the wrong value within the select box.
The code I am using for my select is as follows:
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."</option>";}
echo "</select><br>"; //CLOSE DROP DOWN BOX
How can I edit the above snippet to allow my $cli = $_GET["ClientCode"] to be shown within the select box as the default value on page load
change your while loop like :
$cli = isset($_GET["ClientCode"])?$_GET["ClientCode"]:"";
while ($row = mysql_fetch_array($result)) {
$selected = ($cli == $row['ClientCode']) ? "selected" : "";
echo "<option value='" . $row['ClientCode'] ."' $selected >" . $row['ClientName'] ."</option>";}
Change your <select ...> ... <select> dropdown list code in the following way,
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
$output = "<option value='" . $row['ClientCode'] ."'";
if(isset($_GET["ClientCode"]) && $row['ClientCode'] == $_GET["ClientCode"]){
$output .= " selected='selected'";
}
$output .= ">" . $row['ClientName'] ."</option>";
echo $output;
}
echo "</select><br>";
You need check $_GET in your loop and do selected="selected" for needed option
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
if (!empty($_GET["ClientCode") && $_GET["CleintCode"] == $row["ClientCode"]))
{
echo "<option selected='selected' value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
</option>";
}
else
{
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
</option>";
}
}
echo "</select><br>"
Use this
if($row['ClientCode'] == $_GET["ClientCode"])
{
?>
<option selected="selected">
Value goes here...
</option>
<?php
}

I have a dropdown displaying user names. How can I use Ajax to display their info?

My dropdown list displays user names that I get from the database. I want to implement a displayInfo function so that when someone selects a user, it will automatically display his/her info below.
How can I display a user's info when someone selects their name?
This is my dropdown code:
<?php
//connect
$conn = mysqli_connect("localhost","user","123abc");
mysqli_select_db($conn, "users");
//query
$sql= mysqli_query($conn, "SELECT person_id,first_name FROM users");
echo "<select name='dropdown' onchange='displayInfo' id='dropdown'>";
while ($row = mysqli_fetch_array($sql))
{
//display friends' first names on dropdown
if($row['person_id'] == $row['first_name']) {
echo "<option value='" . $row['person_id'] . "' selected>" . $row['list_name'] . "</option>";
} else {
echo "<option value='" . $row['person_id'] . "'>" . $row['first_name'] . "</option>";
}
}
echo "</select>";
Best way to call a Javascript function on option select like this
<?php
//connect
$conn = mysqli_connect("localhost","user","123abc");
mysqli_select_db($conn, "users");
//query
$sql= mysqli_query($conn, "SELECT person_id,first_name FROM users");
echo "<select name='dropdown' onchange='displayInfo' id='dropdown'>";
while ($row = mysqli_fetch_array($sql))
{
//display friends' first names on dropdown
if($row['person_id'] == $row['first_name']) {
**echo "<option onchange='myFunction(this);' value='" . $row['person_id'] . "' selected>" . $row['list_name'] . "</option>";**
} else {
echo "<option onchange='myFunction(this);' value='" . $row['person_id'] . "'>" . $row['first_name'] . "</option>";
}
}
echo "</select>";
<script>
function myFunction(control){
$(control).val() //to access value or any other functionality
}
</script>

PHP Dropdown list maintain on reload

I am new to PHP and have just started the professional development in this language. I have created a dynamic dropdown list in php as under:
$sql="select DISTINCT State from branchinfo";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
echo "< SELECT NAME='states'>";
while($row=$result->fetch_assoc())
{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}<br>
echo "< /SELECT>";
}
echo "< INPUT TYPE='submit' name='submit' value='submit'>";
Problem is when I select a state and click on submit, the list reloads and my selection is lost, the first option gets selected by default. I have tried to embed the script within OPTION but it didn't worked, I tried it as under:
echo "< OPTION NAME='" . $row['State'] . "'" . " VALUE='" . $row['State'] . "'" . if(isset($_POST["submit"])){ if($_POST["states"] == $row['State']) echo "selected";} . " >" . $row["State"];<br>
I am not using any javasrcipt / jquery till now on this page and not planning to use it either. plz provide a solution within this code. Please help.
Some additional information
The mthod i tried as mentioned above, works fine on hardcoded static drop downlist items written in html form. It stops working for dynamically generated list.
You have to add dropdown inside form tag, then it will work.
$sql = "select DISTINCT State from branchinfo";
$result = $conn->query($sql);
echo "<form method='POST'>";
if ($result->num_rows > 0) {
echo "<SELECT NAME='states'>";
while ($row = $result->fetch_assoc()) {
$sel = ( $_POST['states'] == $row['State'] ) ? "selected" : "";
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "' " . $sel . ">" . $row["State"] . "</OPTION>";
}
echo "</SELECT>";
}
echo "<INPUT TYPE='submit' name='submit' value='submit'>";
echo "</form>";
Use session to remember your choice. And when the page is reloaded just retrieve your previous selected value.
like
while($row=$result->fetch_assoc())
{
if($sessionvalue==$row['State']){
echo "< OPTION SELECTED NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}else{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}
}

Enter an "Any" value in a drop down from a query in PHP

I run a query to create a drop down.
$sql_course = "SELECT * FROM hc_course";
$result_course = mysql_query($sql_course);
echo "<select name='course_num'>";
while ($row = mysql_fetch_array($result_course)) {
echo "<option value='" . $row['course_num'] . "'>" . $row['course_name'] . "</option>";
}
echo "</select>";
This produces the list with all the names correctly.
However, I would like to inject an "any" entry that would then turn the next query based off of this from a 'course_num' into a *.
You mean like this?
echo "<select name='course_num'>";
echo "<option value='*'>Any</option>";
while ($row = mysql_fetch_array($result_course)) {
....
echo "<select name='course_num'>";
echo "<option value='*'>Any</option>";
while ($row = mysql_fetch_array($result_course)) {
echo "<option value='" . $row['course_num'] . "'>" . $row['course_name'] . "</option>";
}
echo "</select>";
You should never trust user input so you would need to add some form of PHP sanitisation to process the request.
Initial code:
$sql_course = "SELECT * FROM hc_course";
$result_course = mysql_query($sql_course);
echo "<select name='course_num'>";
echo "<option value="">Any</option>"; // New code for all courses
while ($row = mysql_fetch_array($result_course)) {
echo "<option value='" . htmlspecialchars($row['course_num']) . "'>" . htmlspecialchars($row['course_name']) . " </option>";
}
echo "</select>";
And the code on the page handling this request should work something like this:
if (empty($_POST["course_num"]))
{
// Run SQL to select all courses
}
else
{
// Sanitise the $_POST["course_num"]
// Run SQL to select a specific course item
}

Categories