0

I'm trying to retrieve data from a SQL DB when the user types a category ID in to search with. It should then autofill (populate) the html form data with the retrieved data from the row's columns (upon pressing the submit button).

Everything is working properly, but I don't know how to change a dropdown menu

For example: <input name="prodname" type="text" value="<?php echo $pName; ?>"/>

Fills the form value successfully after I press the search(submit) button.

This following code does not change the drop down values at all (<select> vs <input>)

<select name="supplierID" value="<?php $suppID; ?>">

How can I change the drop down values of a while only using HTML/PHP?

Thank you.

4
  • Loop through the list of values you want to show. For each value, create an HTML option element within the select. Commented Nov 6, 2019 at 3:45
  • 1
    I fear that it (showing search results based on userinput value) can not be done only using php and HTML. Since php is compiled at server side. You might want to consider client side scripting like JavaScript. Commented Nov 6, 2019 at 3:49
  • You need to set the selected attribute on the selected option element. Just setting the value of the select element won't work IIRC. Commented Nov 6, 2019 at 3:53
  • @Kei I believe that's how I have it set up right now, for example: <select name="supplierID" value="<?php $suppID; ?>"> <option value="1">1</option> <option value="2">2</option> etc... Commented Nov 6, 2019 at 4:21

2 Answers 2

1

To select a option in <select> tag you need to set selected attribute to that option. The following code may help you to understand.

<select name="supplierID">
    <option value="1" <?php if($suppID == 1) echo 'selected'; ?> >1</option>
    <option value="2" <?php if($suppID == 2) echo 'selected'; ?> >2</option>
    <option value="3" <?php if($suppID == 3) echo 'selected'; ?> >3</option>
    <option value="4" <?php if($suppID == 4) echo 'selected'; ?> >4</option>
</select>

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

To select the values in drop down, the selected attribute is required. You may check with the code below:

<select name="supplierID">
  <option value="1" <?php if($id == 1) echo "selected"; ?>> A </option>
  <option value="2" <?php if($id == 2) echo "selected"; ?>> B </option>        
  <option value="3" <?php if($id == 3) echo "selected"; ?>> C </option>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.