Ajax code:
[text]<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>[/text]
classifiedform.php code:
Code: Select all
<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>Code: Select all
<?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
........................
?>