Forum Moderators: coopster
$table = $_GET['table'];
$acceptable = array('table1', 'table2', 'table3');
if(!in_array($table, $acceptable)) exit;
$query = sprintf("SELECT `col` FROM `%s`", $table);
#etc....
$query = "SELECT Name FROM " . $category . " ORDER BY Name";
<?php
include ('inc/dbconnect.php');
if(empty($_GET['_Category'])) { # if url query is left empty, do nothing but terminate the script
exit();
}
$category = $_GET['_Category'];
//echo $category;
#
#select records based on 'topic'
$query = "SELECT Name FROM " . $category . " ORDER BY Name";
$result = mysql_query($query);
#
if($result) { #make sure the query was successful
$items = array();
#
while($row = mysql_fetch_array($result)) {
$items[] = $row['Name']; #push all of the results into an array
}
#
$string = implode(',',$items); #implode the results separated by commas
echo $string; #here we echo the string to the browser; this is what the javascript will be receiving
}
?>
$_GET['_Category'] ?_Category=table_name. Change this to the following: $_GET['Category'] and make sure your URL query looks like this: ?Category=table_name
$result = mysql_query($query) or die(mysql_error());
function get_subcat($name,$subcattable) {
$subcat = "<label for=\"$name\">Subcategory:</label>
<select name=\"$name\" id=\"$name\">
<option value=\"\">Select Subcategory</option>
";
$query = "select id,title from $subcattable order by title";
$result = mysql_query or die("cannot query for subcat " . mysql_error());
while ($row=mysql_fetch_array($result)) {
$subcat .= '<option value="' . $row['id'] . '"';
if (isset($_POST[$name]) and ($_POST[$name]==$row['id'])) { $subcat .= ' selected'; }
// selected="selected" for XHTML. :-/
$subcat .= '>' . $row['title'] . "</option>\n";
}
$subcat .= "</select>\n\n";
return $subcat;
}
sendRequest('fetch.php?topic='+el.options[selected].value);
$_GET['topic']. You would have to change this line to reflect the PHP code, assuming you are using the javascript provided in the example.
//globals
var first = "_Category"; //id of first SELECT
var second = "_Name"; //id of second SELECT
//
function sendRequest(url,params,HttpMethod) {
if(!HttpMethod) { //check if http method is defined, if not, set it to GET
HttpMethod="GET";
}
//
// initialize request object
req=null;
if(window.XMLHttpRequest){
req=new XMLHttpRequest; //mozilla/safari
} else if(window.ActiveXObject){
req=new ActiveXObject("Microsoft.XMLHTTP"); //internet explorer
}
//
//define callback handler
if(req) {
//
req.onreadystatechange=onReadyState;
req.open(HttpMethod,url,true);
req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
req.send(params);
}
}
//
function onReadyState() {
//
var ready=req.readyState;
var data=null;
if(ready==4){ //check ready state
data=req.responseText; //read response data
var items = data.split(',');
var length = items.length;
for(var i = 0; i < length; i++) {
var childEl = document.createElement('option'); //create option
var El = document.getElementById(second);
El.appendChild(childEl); //then append it to the second dropdown list
childEl.value = items[i];
childEl.innerHTML = items[i];
}
}
}
//
function clicked() {
//
var el = document.getElementById(first);
var ob2=document.getElementById(second);
var selected = el.selectedIndex;
//
while(ob2.hasChildNodes()) { //removes items from dropdown if some already exist
ob2.removeChild(ob2.firstChild);
}
if(selected!= 0) { //if they choose something other than the first select-->"Select topic first"
sendRequest('fetch.php?category='+el.options[selected].value);
ob2.disabled=0;
} else { //otherwise add the Select Topic First option and disable it
var childEl = document.createElement('option');
ob2.appendChild(childEl);
childEl.innerHTML = 'Select Category First';
ob2.disabled=1;
}
}
<?php
include ('inc/dbconnect.php');
if(empty($_GET['category'])) { # if url query is left empty, do nothing but terminate the script
exit();
}
$category = $_GET['category'];
//echo $category;
#
#select records based on 'topic'
$query = "SELECT Name FROM " . $category . " ORDER BY Name";
$result = mysql_query($query);
#
if($result) { #make sure the query was successful
$items = array();
#
while($row = mysql_fetch_array($result)) {
$items[] = $row['Name']; #push all of the results into an array
}
#
$string = implode(',',$items); #implode the results separated by commas
echo $string; #here we echo the string to the browser; this is what the javascript will be receiving
}
?>
Don't reveal your table name publicly, that's bereft with security issues. Map it, like this, where the ID's correspond to the selected category . . .