{"id":212,"date":"2012-01-17T00:22:56","date_gmt":"2012-01-16T23:22:56","guid":{"rendered":"http:\/\/gator4228.temp.domains\/~vasiljevski\/blog\/?p=212"},"modified":"2018-01-08T23:23:37","modified_gmt":"2018-01-08T22:23:37","slug":"how-to-set-the-selected-option-of-select-tag-from-the-php","status":"publish","type":"post","link":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/","title":{"rendered":"How to set the selected option of &lt;select&gt; tag from the PHP"},"content":{"rendered":"<p>There is a few ways to do this. The easiest way to do this is in fact really simple, you just need to add a keyword &#8220;selected&#8221; to the option that you want selected.<br \/>\nFor example, if You have something like this:<\/p>\n<pre lang=\"html\"><select>\r\n   <option value=\"1\">Apple<\/option>\r\n   <option value=\"2\">Samsung<\/option>\r\n   <option value=\"3\">HTC<\/option>\r\n<\/select><\/pre>\n<p><!--more--><br \/>\nIf we want to have &#8220;Samsung&#8221; selected:<\/p>\n<pre lang=\"html\"><select>\r\n   <option value=\"1\">Apple<\/option>\r\n   <option selected value=\"2\">Samsung<\/option>\r\n   <option value=\"3\">HTC<\/option>\r\n<\/select><\/pre>\n<p>Pretty easy, don&#8217;t you thing so \ud83d\ude42<\/p>\n<p>Now next thing that we need to do is to make our &lt;select&gt; submit so we could arrange the page accordingly.<br \/>\nTo do that, we need to put it inside a &lt;form&gt;, like this:<\/p>\n<pre lang=\"html\"><form>\r\n   <select id=\"company\" name=\"company\"> \r\n      <option value=\"1\">Apple<\/option> \r\n      <option value=\"2\">Samsung<\/option> \r\n      <option value=\"3\">HTC<\/option> \r\n   <\/select> \r\n<\/form><\/pre>\n<p>Depending on your needs and requirements you may need some extra parameters set to the <\/p>\n<form> tag, but for our example this is just fine<br \/>\nYou can notice I added &#8220;id&#8221; and a &#8220;name&#8221; to the &lt;select&gt; tag, we will you these names to access the value at the backend.<\/p>\n<p>Now for the fun part, let&#8217;s add some basic PHP code and bring our example to life.<\/p>\n<pre lang=\"php\"><?php\r\n\r\n(isset($_POST[\"company\"])) ? $company = $_POST[\"company\"] : $company=1;\r\n\r\n?>\r\n\r\n<form>\r\n<select id=\"company\" name=\"company\">\r\n<option <?php if ($company == 1 ) echo 'selected' ; ?> value=\"1\">Apple<\/option>\r\n<option <?php if ($company == 2 ) echo 'selected' ; ?> value=\"2\">Samsung<\/option>\r\n<option <?php if ($company == 3 ) echo 'selected' ; ?> value=\"3\">HTC<\/option>\r\n<\/select>\r\n<\/form><\/pre>\n<p>And that&#8217;s it. It has no real use, but it is good to get a grip of how the things work.<\/p>\n<p>Hope someone finds this useful, and also that someone can contribute and make the example better.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a few ways to do this. The easiest way to do this is in fact really simple, you just need to add a keyword &#8220;selected&#8221; to the option that you want selected. For example, if You have something like this: Apple Samsung HTC<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-212","post","type-post","status-publish","format-standard","hentry","category-php","content","has-not-featured-image"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to set the selected option of &lt;select&gt; tag from the PHP - Vasiljevski Nikola<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to set the selected option of &lt;select&gt; tag from the PHP - Vasiljevski Nikola\" \/>\n<meta property=\"og:description\" content=\"There is a few ways to do this. The easiest way to do this is in fact really simple, you just need to add a keyword &#8220;selected&#8221; to the option that you want selected. For example, if You have something like this: Apple Samsung HTC\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Vasiljevski Nikola\" \/>\n<meta property=\"article:publisher\" content=\"vasiljevski\/\" \/>\n<meta property=\"article:author\" content=\"vasiljevski\/\" \/>\n<meta property=\"article:published_time\" content=\"2012-01-16T23:22:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-08T22:23:37+00:00\" \/>\n<meta name=\"author\" content=\"Nikola Vasiljevski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vasiljevski\" \/>\n<meta name=\"twitter:site\" content=\"@vasiljevski\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikola Vasiljevski\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/webdev\\\/php\\\/how-to-set-the-selected-option-of-select-tag-from-the-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/webdev\\\/php\\\/how-to-set-the-selected-option-of-select-tag-from-the-php\\\/\"},\"author\":{\"name\":\"Nikola Vasiljevski\",\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/#\\\/schema\\\/person\\\/509c89ff2a8cf276387db60304561957\"},\"headline\":\"How to set the selected option of &lt;select&gt; tag from the PHP\",\"datePublished\":\"2012-01-16T23:22:56+00:00\",\"dateModified\":\"2018-01-08T22:23:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/webdev\\\/php\\\/how-to-set-the-selected-option-of-select-tag-from-the-php\\\/\"},\"wordCount\":215,\"commentCount\":27,\"publisher\":{\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/#\\\/schema\\\/person\\\/509c89ff2a8cf276387db60304561957\"},\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/webdev\\\/php\\\/how-to-set-the-selected-option-of-select-tag-from-the-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/webdev\\\/php\\\/how-to-set-the-selected-option-of-select-tag-from-the-php\\\/\",\"url\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/webdev\\\/php\\\/how-to-set-the-selected-option-of-select-tag-from-the-php\\\/\",\"name\":\"How to set the selected option of &lt;select&gt; tag from the PHP - Vasiljevski Nikola\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/#website\"},\"datePublished\":\"2012-01-16T23:22:56+00:00\",\"dateModified\":\"2018-01-08T22:23:37+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/webdev\\\/php\\\/how-to-set-the-selected-option-of-select-tag-from-the-php\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/\",\"name\":\"Vasiljevski Nikola\",\"description\":\"Art is never finished, only abandoned\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/#\\\/schema\\\/person\\\/509c89ff2a8cf276387db60304561957\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/#\\\/schema\\\/person\\\/509c89ff2a8cf276387db60304561957\",\"name\":\"Nikola Vasiljevski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g\",\"caption\":\"Nikola Vasiljevski\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g\"},\"sameAs\":[\"https:\\\/\\\/www.vasiljevski.com\",\"vasiljevski\\\/\",\"https:\\\/\\\/x.com\\\/vasiljevski\",\"nikola83\"],\"url\":\"https:\\\/\\\/www.vasiljevski.com\\\/blog\\\/author\\\/nikola\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to set the selected option of &lt;select&gt; tag from the PHP - Vasiljevski Nikola","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/","og_locale":"en_US","og_type":"article","og_title":"How to set the selected option of &lt;select&gt; tag from the PHP - Vasiljevski Nikola","og_description":"There is a few ways to do this. The easiest way to do this is in fact really simple, you just need to add a keyword &#8220;selected&#8221; to the option that you want selected. For example, if You have something like this: Apple Samsung HTC","og_url":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/","og_site_name":"Vasiljevski Nikola","article_publisher":"vasiljevski\/","article_author":"vasiljevski\/","article_published_time":"2012-01-16T23:22:56+00:00","article_modified_time":"2018-01-08T22:23:37+00:00","author":"Nikola Vasiljevski","twitter_card":"summary_large_image","twitter_creator":"@vasiljevski","twitter_site":"@vasiljevski","twitter_misc":{"Written by":"Nikola Vasiljevski","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/#article","isPartOf":{"@id":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/"},"author":{"name":"Nikola Vasiljevski","@id":"https:\/\/www.vasiljevski.com\/blog\/#\/schema\/person\/509c89ff2a8cf276387db60304561957"},"headline":"How to set the selected option of &lt;select&gt; tag from the PHP","datePublished":"2012-01-16T23:22:56+00:00","dateModified":"2018-01-08T22:23:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/"},"wordCount":215,"commentCount":27,"publisher":{"@id":"https:\/\/www.vasiljevski.com\/blog\/#\/schema\/person\/509c89ff2a8cf276387db60304561957"},"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/","url":"https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/","name":"How to set the selected option of &lt;select&gt; tag from the PHP - Vasiljevski Nikola","isPartOf":{"@id":"https:\/\/www.vasiljevski.com\/blog\/#website"},"datePublished":"2012-01-16T23:22:56+00:00","dateModified":"2018-01-08T22:23:37+00:00","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vasiljevski.com\/blog\/webdev\/php\/how-to-set-the-selected-option-of-select-tag-from-the-php\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.vasiljevski.com\/blog\/#website","url":"https:\/\/www.vasiljevski.com\/blog\/","name":"Vasiljevski Nikola","description":"Art is never finished, only abandoned","publisher":{"@id":"https:\/\/www.vasiljevski.com\/blog\/#\/schema\/person\/509c89ff2a8cf276387db60304561957"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vasiljevski.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.vasiljevski.com\/blog\/#\/schema\/person\/509c89ff2a8cf276387db60304561957","name":"Nikola Vasiljevski","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g","caption":"Nikola Vasiljevski"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/ec57b2f6f160422547bf7c9839dcdea6dec9a50dddffae650a5a393fea6953ec?s=96&r=g"},"sameAs":["https:\/\/www.vasiljevski.com","vasiljevski\/","https:\/\/x.com\/vasiljevski","nikola83"],"url":"https:\/\/www.vasiljevski.com\/blog\/author\/nikola\/"}]}},"_links":{"self":[{"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":21,"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":619,"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions\/619"}],"wp:attachment":[{"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vasiljevski.com\/blog\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}