Select default value from GET value - php

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
}

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']) {

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>

returning result from database in drop down list with parse error

i am trying to echo the value of the field form the database in the drop-down list retrieved from another table in the database, but I keep getting the Parse error: syntax error, unexpected T_IF
<?php
$result1 = mysql_query("SELECT `CompanyID`, `Name` FROM `company`") or trigger_error(mysql_error());
while($row1 = mysql_fetch_array($result1)) {
foreach($row1 AS $key1 => $value1) {
$row1[$key1] = stripslashes($value1);
}
echo "<option value=" . nl2br( $row1['CompanyID']) . " ". if($row['CompanyID'] == $Merchant) echo 'selected = "selected"'
. ">" . nl2br( $row1['Name']) . "</option>";
}
?>
try this
if($row['CompanyID'] == $Merchant) {echo 'selected = "selected"';} to me it looks like you forgotten your curly brackets in your if statement
Inside a echo statement you should use a ternary operator, its basically a shortened IF/Else statement.
Here is the code with a ternary operator:
<?php
$result1 = mysql_query("SELECT `CompanyID`, `Name` FROM `company`") or trigger_error(mysql_error());
while($row1 = mysql_fetch_array($result1)) {
foreach($row1 AS $key1 => $value1) {
$row1[$key1] = stripslashes($value1);
}
echo "<option value='" . nl2br( $row1['CompanyID']) . "' ". (($row['CompanyID'] == $Merchant)?' selected ':''). ">" . nl2br( $row1['Name']) . "</option>";
}
?>
You can read more about ternary operators in PHP's documentation over here.
Please check this thread and do as follows if block inside echo statement?
in your case it should be something like this
. if($row['CompanyID'] == $Merchant) echo 'selected
changed to this
.(($row['CompanyID']==$Merchant)?'selected
You can write like this, it looks more clear
echo "<option value="
.nl2br( $row1['CompanyID'])
.($row['CompanyID'] == $Merchant)? 'selected = "selected">' : ">"
.nl2br( $row1['Name'])
."</option>";

Select option selected an not selected

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>";

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