elearnindia Posted July 4, 2010 Share Posted July 4, 2010 I am trying to create website with PHP,MySQL,Ajax.In my form,the values of sub-category select box are populated according to the value in the category select box by calling ajax function showsubCategory(str),below I am showing some of the code: Ajax code: <script type="text/javascript"> var xmlHttp function showsubCategory(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("HTTP Request is not supported by browser") return } var url="SubCategory_add.php" url=url+"?cat="+str url=url+"&rid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("Sub- Category").innerHTML=xmlHttp.responseText } } function GetXmlHttpObject() { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp } </script> classifiedform.php code: <form action="classifiedform_add.php" method="post"> <tr> <td style="color:#0099CC;width:250px"><div align="left">Select Category</div> </td> <td > <select name="select" id="select" style="width:150px" onchange="[b]showsubCategory[/b](this.value);" > <option value="" selected="selected">Select Category</option> <?php require_once('config.php'); //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } // Form a query to populate the category combo-box $query = "SELECT * FROM tbl_catagories;"; // Successful query? if($result = mysql_query($query)) { // If there are results returned, create options if($success = mysql_num_rows($result) > 0) { // For each item in the results... while ($row = mysql_fetch_array($result)) // Add a new option to the combo-box echo "<option value=\"$row[category_name]\">$row[category_name]</option>\n"; } else { echo "No results found."; } ?> </select> </td> </tr> <tr> <td style="color:#0099CC;width:250px;"> <div align="left"> Select Sub-Category </div> </td> <td> <div id="Sub-Category"> <select name="SubCategoryName" id="SubCategoryName" style="width:150px"> <option value="" selected="selected"> Select Sub-Category</option> </select> </div> </td> </tr> </form> classifiedform_add.php code: <?php //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //values received from form $category = clean($_POST['Categoryname']); $subcategory = clean($_POST['SubCategoryName']); //MySQL code ........................ ?> In classifiedform_add.php I am not getting the value of Sub-Category from the select box of subcategories which is populated by ajax function showsubCategory(str) to submit into MySQL database,when it is submitted from classifiedform.php.Any kind of help in getting the value of Sub-Category will be appreciated. Quote Link to comment 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.