Select an option with a GET value?

<?php

$category = isset($_GET['category']) ? $_GET['category'] : ""; //results will be "Private" or "Company" or empty

?>
    <form>
    <select name="customer_category" id="customer_category">
		<option value="Private" /*if $category == "Private" {echo "selected";}*/ >Private Individual</option>
		<option value="Company" /*if $category == "Company" {echo "selected";}*/>Company</option>
	</select>
    </form>
   <button> Save Changes </button>

Very simplified example of what I’m trying to achieve. Loops and iterating over items have never been my strong point, can someone help me to select an option depending on a dynamic value?

This is going to be used in an edit form. I have the same question about getting the correct checkbox and radio option checked depending on a dynamic value. Any tips would be appreciated.

Thank you

You should use a post method form when performing an action on the server.

The choices for the options, check boxes, and ratio buttons should be defined in database table(s), with an id and a name/label. You would query for the choices and store the result in array(s). You would loop over these arrays to dynamically produce the html output, pre-selecting/checking any choice that corresponds to, initially the existing values (also retrieved from wherever they are stored), then the current values that were submitted from the form (in the case where an error occurred when processing the form submission.) The submitted form values should be the id of the choice, not the name/label and you would store the id in any database table holding the selected choices. To switch from using the initial existing values to the current values from the form, define an empty array variable, named something like $post, then inside the form processing code copy the submitted $_POST data to the $post variable, then use the contents of the $post variable throughout the remainder of the code. After the end of the form processing code, you would test if the $post variable is empty to decide if you need to query for and retrieve the initial values that are being edited. You would retrieve and store the initial values into the same $post variable.

Sponsor our Newsletter | Privacy Policy | Terms of Service