hitman47 Posted October 24, 2013 Share Posted October 24, 2013 (edited) Hallo. I have a form <form id="form2" method="post" action="insert_data.php>"> <select name="catid" id="catid" onChange="showkatigoria(this.value);"> <option value="'. $row['catid'].'"><h4>' . $row['catname'].'</option> // something i need with php, i dont' think it matters <option value="'. $row['catid'].'"><h4>' . $row['catname'].'</option> <option value="'. $row['catid'].'"><h4>' . $row['catname'].'</option> //option value bla bla bla bla i have 4-5 options value. </select> My javascript: <script type="type/javascript"> function showkatigoria(str) { if (str=="") { document.getElementById("catid").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("catid").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","insert_data.php?category="+str,true); xmlhttp.send(); } </script> It doesn't work what ever i do. Please help! I don't know javascript, let alone ajax. It's urgent. Please help! Edited October 24, 2013 by hitman47 Quote Link to comment https://forums.phpfreaks.com/topic/283244-php-ajax-select-with-onchange-event-doesnt-work/ Share on other sites More sharing options...
hitman47 Posted October 24, 2013 Author Share Posted October 24, 2013 I want to mention that all my code is in one file. In insert_data.php. Shall i use more? Shall i split the data? I dont know ajax. Please help Quote Link to comment https://forums.phpfreaks.com/topic/283244-php-ajax-select-with-onchange-event-doesnt-work/#findComment-1455291 Share on other sites More sharing options...
Ch0cu3r Posted October 25, 2013 Share Posted October 25, 2013 I would help if you explained what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/283244-php-ajax-select-with-onchange-event-doesnt-work/#findComment-1455350 Share on other sites More sharing options...
hitman47 Posted October 25, 2013 Author Share Posted October 25, 2013 Its easy. I have a form with many select options. Like this website: http://www.tospitimou.gr/en/index.jsp Check it at the top where it says YOU ARE AT. IF you choose an area then in the select box that follows some options are shown. If you choose something differnet in teh first select different choises come up in the second. Same thing in this site: http://www.realestatecorner.gr/ I foy choose something in the "Περιοχή:" that means location in greek, then a new select option emerges, that was not shown in the beggining. If you choose another location, other options come up. How shall i do iti? I dont have a clue about ajax. With the code i gave you Quote Link to comment https://forums.phpfreaks.com/topic/283244-php-ajax-select-with-onchange-event-doesnt-work/#findComment-1455369 Share on other sites More sharing options...
Ch0cu3r Posted October 25, 2013 Share Posted October 25, 2013 Have a look at the following example to get an idea of how to do this http://www.9lessons.info/2010/08/dynamic-dependent-select-box-using.html http://www.satya-weblog.com/2007/04/dynamically-populate-select-list-by.html Quote Link to comment https://forums.phpfreaks.com/topic/283244-php-ajax-select-with-onchange-event-doesnt-work/#findComment-1455373 Share on other sites More sharing options...
hitman47 Posted October 25, 2013 Author Share Posted October 25, 2013 (edited) Thank you bro. One question only. In the first link for example ajax sends the variable 'id' with post to a different page than the original-mother select is. The mother select is in sections_demo.php and ajax sends the variable to ajax_city.php From there it queries the dtabase and the extra option field is echoed. Why? Could this be done within the same page sections_demo.php? This is something i don't understand Edited October 25, 2013 by hitman47 Quote Link to comment https://forums.phpfreaks.com/topic/283244-php-ajax-select-with-onchange-event-doesnt-work/#findComment-1455378 Share on other sites More sharing options...
Ch0cu3r Posted October 25, 2013 Share Posted October 25, 2013 (edited) From there it queries the dtabase and the extra option field is echoed. Why? Because the output of the ajax request (ajax_city.php) is used to add the city options to the page for the selected country. Could this be done within the same page sections_demo.php? Yes. You just need to make sure when your php script receives the ajax request it only outputs the city options and not your whole webpage. For you to understand this more you'll want to research more about what is ajax and how it works. Edited October 25, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283244-php-ajax-select-with-onchange-event-doesnt-work/#findComment-1455384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.