简体   繁体   中英

How can I select an option in a drop down box from $_GET in Php

This question looks similar to others but seems to be unique (to the forums at least).

I have a form on a page in which a user selects an option from a form:

<!-- <option value="" onClick="" >Select Country</option>  -->
<option value="38" onClick="" >Canada</option>
<!-- <option value="222" onClick="" >United Kingdom</option> -->
<option value="223" onClick="" >United States</option></select></td>

It is passed on to a new form page which is coded identically to the first form. For the other fields in the form I am able to use the following echo but it does not work to select the proper option in the dropdown.

Code to enter user original form data into new form text field:

<input type="text" id='fields_city' name='fields_city'  value="<?php echo $_GET['Ship_City']; ?>" />

Code to enter user original form data into new form dropdown field:

<select border="0"  class="" id="country" style="width:149px;" name="country"  size="1" selected="selected"  value="<?php echo $_GET['Ship_Country']; ?>"

I basically just need to prepopulate the dropdown with the user selected item.

Using select_name since you didn't show your select. Also assuming get since that's what you used and didn't show the form. This echos selected if it was on the previous form, therefore selecting that option:

<option value="38" onClick="" <?php echo $_GET['select_name'] == 38 ? "selected" : ""; ?>>Canada</option>
<?php If ($_GET['Ship_Country'] == "38") { echo "selected"; } ?>

On the new page select box for each option you could do something like this. I believe this should work, unless I misunderstood you.

<option value="38" <?= $_GET['Ship_City'] == '38' ? selected : ""; ?>>Canada</option>
<option value="223" <?= $_GET['Ship_City'] == '223' ? selected : ""; ?>>United States</option></select></td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM