Forum Moderators: coopster

Message Too Old, No Replies

Dropdown option to activate a select query

         

ski442

10:51 pm on Jan 7, 2010 (gmt 0)

10+ Year Member



Hi Guys.

Would like some help or pointers please on how i would, on clicking of subcatagory in the drop down menu have that selection turn into $subcatagory variable and insert it to the select statement to query the database.
The drop down is also calling from the same table as the mysql_select

So each time something is selected from the drop down, the page will return results = to subcatagory and display in the table.

Here is what i have so far.

<label for="subcatagory">SubCatagory:</label>
<select name="subcatagory">
<?php
echo"<option value=\"0\" selected>--Choose One--</option>";
$sql = mysql_query("SELECT DISTINCT subcatagory FROM products ORDER BY subcatagory ASC ");
while ($typesentered = mysql_fetch_array($sql))
{
$optionname = $typesentered['subcatagory'];
echo"<option value=\"?listing=".$optioname."\" >".$optionname."</option>";
}
?>
</select>

$sql="SELECT * FROM $tbl_name WHERE subcatagory = '$subcatagory'";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td width="50">ID</td>
<td width="50">New Line</td>
<td width="80">Stock Code</td>
<td width="42">QTY Stock</td>
<td width="62">Price</td>
<td width="40">Cost</td>
<td width="60">Offer Price</td>
<td width="60">On Offer</td>
<td width="80">catagory</td>
<td width="80">subcatagory</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="newline[]" type="text" style="width:50px;" id="newline" value="<? echo $rows['newline']; ?>"></td>
<td align="center"><input name="stockcode[]" type="text" style="width:50px;" id="stockcode" value="<? echo $rows['stockcode']; ?>"></td>
<td align="center"><input name="stock_in[]" type="text" style="width:50px;" id="stock_in" value="<? echo $rows['stock_in']; ?>"></td>
<td align="center"><input name="price[]" type="text" style="width:50px;" id="price" value="<? echo $rows['price']; ?>"></td>
<td align="center"><input name="cost[]" type="text" style="width:50px;" id="cost" value="<? echo $rows['cost']; ?>"></td>
<td align="center"><input name="onoffer[]" type="text" style="width:50px;" id="onoffer" value="<? echo $rows['onoffer']; ?>"></td>
<td align="center"><input name="offer[]" type="text" style="width:50px;" id="offer" value="<? echo $rows['offer']; ?>"></td>
<td align="center"><input name="catagory[]" type="text" style="width:80px;" id="catagory" value="<? echo $rows['catagory']; ?>"></td>
<td align="center"><input name="subcatagory[]" type="text" style="width:80px;" id="subcatagory" value="<? echo $rows['subcatagory']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET newline='$newline[$i]', stockcode='$stockcode[$i]', stock_in='$stock_in[$i]', price='$price[$i]', cost='$cost[$i]', onoffer='$onoffer[$i]', offer='$offer[$i]', catagory='$catagory[$i]', subcatagory='$subcatagory[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}
mysql_close();
?>

Any help will great.
Thanks Ski442

rocknbil

3:13 am on Jan 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you might appreciate the assistance of Javascript on this one. :-) Here's some "baby logic" for a framework.

// Don't use is_numeric. Zero is also numeric, and you should never
// have any records with a unique id of zero.
// Text input via XSS/Injection will evaluate to zero, at best.


if (isset($_POST['category']) and ($_POST['category'] > 0)); {
// Generate select list. Store in $selectList
// select id,name from subcats where cat=$_POST[/category']
$selectList = '
<input type="hidden" name="category" value=' . $_POST['category'] . '">
<select name="subcategory" id="subcategory">
';
// query for options, concatenate to $selectList
$selectList .= '<select>';
// Make sure the "rest" of your form is inside this IF
}
else {
// Ditto, for category, except note onchange, and button
$selectList = '
<select name="category" id="category" onchange="this.form.submit();">
';
// In case JS is disabled, you need a button
$selectList .= '<select>
<input type="submit" value="Get Subcategories &gt;&gt;">
';
}

Then put $selectList where you want it. Personally, I like to have a space for both cat and subcat so if the user decides to change category, they can.

rocknbil

10:12 pm on Jan 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Typo alert:

if (isset($_POST['category']) and ($_POST['category'] > 0)); {

Remove semicolon . . . not working code anyway . . .