{"id":456,"date":"2012-12-03T05:39:43","date_gmt":"2012-12-03T09:39:43","guid":{"rendered":"http:\/\/www.searchenginegenie.com\/programming-blog\/?p=456"},"modified":"2013-05-13T01:29:56","modified_gmt":"2013-05-13T05:29:56","slug":"tutorial-to-get-values-from-database-on-selection-of-dropdown-in-php","status":"publish","type":"post","link":"https:\/\/www.searchenginegenie.com\/programming-blog\/tutorial-to-get-values-from-database-on-selection-of-dropdown-in-php\/","title":{"rendered":"Tutorial to get values from database on selection of dropdown in PHP"},"content":{"rendered":"<h2>PHP Fetch Tutorial<\/h2>\n<h3>Fetching Data From Database on selection of dropdown value<\/h3>\n<p><strong>Step 1:\u00a0<\/strong><\/p>\n<p>Get the list of countries from database and populate in drop-down.<\/p>\n<p>[php]<br \/>\n$ctry_qry = mysql_query(\u201cSELECT * FROM countries ORDER BY name;\u201d);?&gt;<br \/>\n\/\/Calling Function<br \/>\n&lt;select name=&quot;\u201dctry\u201d&quot;&gt; &lt;option value=&quot;\u201d&amp;quot;&quot;&gt;Select Country&lt;\/option&gt;&lt;\/select&gt;<br \/>\n&lt;select name=&quot;\u201dctry\u201d&quot;&gt;&lt;option value=&quot;\u201d&lt;?PHP&quot;&gt;\u201d&gt;&lt;!&#8211;?PHP echo $ctry[&#8216;name&#8217;]; ?&#8211;&gt;&lt;\/option&gt;&lt;\/select&gt;<br \/>\n[\/php]<\/p>\n<p>Consider that we choose\u00a0 India from the dropdown list.<\/p>\n<p><a href=\"http:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/countries-dropdown-list-values-from-database.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-534\" title=\"countries-dropdown-list-values-from-database\" alt=\"countries-dropdown-list-values-from-database\" src=\"http:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/countries-dropdown-list-values-from-database-300x229.png\" width=\"300\" height=\"229\" srcset=\"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/countries-dropdown-list-values-from-database-300x229.png 300w, https:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/countries-dropdown-list-values-from-database.png 466w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Step:2<\/strong><\/p>\n<p><strong><\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Add a simple javascript function which will run onchange<\/p>\n<p>[javascript]<br \/>\nfunction reloaddata(ctryid) {<br \/>\nwindow.location.href=&quot;http:\/\/localhost\/rotatetheglobe\/admin\/try.php?ctryid=&quot; + ctryid;<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<p><strong>Step:3<\/strong><\/p>\n<p><strong><\/strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Reload the page in JS and pass the currently selected country id<\/p>\n<p><a href=\"http:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/values-fetched-from-db-on-country-selection.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-535\" title=\"values-fetched-from-db-on-country-selection\" alt=\"values-fetched-from-db-on-country-selection\" src=\"http:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/values-fetched-from-db-on-country-selection-300x229.jpg\" width=\"300\" height=\"229\" srcset=\"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/values-fetched-from-db-on-country-selection-300x229.jpg 300w, https:\/\/www.searchenginegenie.com\/programming-blog\/wp-content\/uploads\/2012\/12\/values-fetched-from-db-on-country-selection.jpg 603w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Step:4<\/strong><\/p>\n<p>Get the country id from query string and fetch the required values from the database<\/p>\n<p>[php]<br \/>\n&lt;?PHP&lt;\/pre&gt;<br \/>\n&lt;div&gt;&lt;code&gt;if&lt;\/code&gt;&lt;code&gt;(isset(&lt;\/code&gt;&lt;code&gt;$_REQUEST&lt;\/code&gt;&lt;code&gt;[&lt;\/code&gt;&lt;code&gt;&#8217;ctryid'&lt;\/code&gt;&lt;code&gt;])) {&lt;\/code&gt;&lt;\/div&gt;<br \/>\n&lt;div&gt;&lt;code&gt;$ctryinfo_qry&lt;\/code&gt; &lt;code&gt;= mysql_query(&lt;\/code&gt;&lt;code&gt;&quot;SELECT * FROM country_info where ctry_root_id=&#8217;&quot;&lt;\/code&gt;&lt;code&gt;.&lt;\/code&gt;&lt;code&gt;$_REQUEST&lt;\/code&gt;&lt;code&gt;[&#8216;ctryid&lt;\/code&gt;&lt;code&gt;&#8217;].&quot;'&lt;\/code&gt; &lt;code&gt;ORDER BY ctry_info_id ;&quot;);&lt;\/code&gt;&lt;\/div&gt;<br \/>\n&lt;div&gt;&lt;code&gt;?&gt;&lt;\/code&gt;&lt;\/div&gt;<br \/>\n&lt;pre&gt;[\/php]<\/p>\n<p>Fetch array is used to fetch the respective row from record set . Using fetch array in while loop is used get a new row of MySQL information until the condition is TRUE.When there are no more rows the function will return FALSE causing the while loop to stop!<\/p>\n<p>[php]<br \/>\n&lt;textarea id=&quot;msgpost&quot; name=&quot;ctry_about&quot; rows=&quot;10&quot; cols=&quot;50&quot;&gt; &lt;?php echo $ctry_inf[&#8216;ctry_about&#8217;];?&gt;&lt;\/textarea&gt;<br \/>\n &lt;!&#8211;?php } ?&#8211;&gt;<\/p>\n<p>[\/php]<\/p>\n<h2><a href=\"http:\/\/www.rotatetheglobe.com\/admin\/update-country-information.php\">CLICK HERE <\/a>to view the complete output<\/h2>\n<p><strong>Possible Errors to occur:<\/strong><\/p>\n<ul>\n<li>JS Error<\/li>\n<li>Couldn&#8217;t get the value from the query string<\/li>\n<li>Page not found<\/li>\n<\/ul>\n<p><strong>Must Follow<br \/>\n<\/strong><\/p>\n<ul>\n<li>Make Sure the querystring variable and the $_REQUEST variable is same<\/li>\n<\/ul>\n<ul>\n<li>Make sure your onchange function is same name as that you call in your code.<\/li>\n<\/ul>\n<p><strong>Must know before reading this article<\/strong><\/p>\n<ul>\n<li>PHP Basics<\/li>\n<li>HTML Basics<\/li>\n<li>DB Handling<\/li>\n<\/ul>\n<p><strong>Coming Up:<\/strong><\/p>\n<ul>\n<li>How to configure a site to work in your local host when you are running windows<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>PHP Fetch Tutorial Fetching Data From Database on selection of dropdown value Step 1:\u00a0 Get the list of countries from database and populate in drop-down. [php] $ctry_qry = mysql_query(\u201cSELECT * FROM countries ORDER BY name;\u201d);?&gt; \/\/Calling Function &lt;select name=&quot;\u201dctry\u201d&quot;&gt; &lt;option value=&quot;\u201d&amp;quot;&quot;&gt;Select Country&lt;\/option&gt;&lt;\/select&gt; &lt;select name=&quot;\u201dctry\u201d&quot;&gt;&lt;option value=&quot;\u201d&lt;?PHP&quot;&gt;\u201d&gt;&lt;!&#8211;?PHP echo $ctry[&#8216;name&#8217;]; ?&#8211;&gt;&lt;\/option&gt;&lt;\/select&gt; [\/php] Consider that we choose\u00a0 India from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-456","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/comments?post=456"}],"version-history":[{"count":66,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"predecessor-version":[{"id":526,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/posts\/456\/revisions\/526"}],"wp:attachment":[{"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.searchenginegenie.com\/programming-blog\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}