How do I get the current value from a column in the database table and and show as selected on edit form page in a select dropdown so when I go the edit form it has the value from the db showing in the select dropdown menu
I can’t work out how to do it, I have tried googling it and trying all sorts of ways I have found but nothing has found
the customer_name is what it’s called and being stored in the db table
Thank you, I added that line in and it just displays the first option in the select dropdown, it needs to show the value as the option from the db table column called customer_name, below is the coding I currently have
<select name="customer_name" class="form-control">
<?php
$con=mysqli_connect("localhost","username","password","dbname");
if (mysqli_connect_errno())
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}
$sql="SELECT customer_name FROM customers";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result))
{
echo '<option value='.$row['customer_name'].($row === $selectedValue ? 'selected="selected"' : '').'>'.$row['customer_name'].'</option>';
}
?>
</select>
you’re comparing the entire $row array to your variable called $selectedValue, so that won’t work - you need to compare the individual array element that you are looking at, I presume that’s $row['customer_name'] here.
Have you set an appropriate value for $selectedValue somewhere that you don’t show in the code above?
Also on that same line of code, and it’s a minor thing, if you’re going to use the same value for the option value as you display, there’s no need to specify it - if there’s no value tag it will automatically be the same as your display text. However in your case, I would suspect you’d want to put the customer unique id field rather than the name so that the customer details can be retrieved on that.
OK, so as we’re comparing the retrieved value to that variable, it’s not going to work even when you compare it to the correct field.
You need to set $selectedValue to the value that you want to be the selected option. So if you’ve retrieved an invoice, for example, and you want this select-tag to default to the customer for that invoice, it needs to be set to the customer name (or id, as I wrote above).
seems weird, in that I didn’t think that html option tags had an id attribute. I thought that should be value. As you’re passing the same as you are displaying, it won’t make any difference, other than being invalid (I think) HTML.
Still seems a bit strange that you have some Ajax that calls the same page, but then does nothing with the response. Maybe it’s me just not seeing what is happening.