Jump to content

[SOLVED] does anyone know of any good select from sql drop down form tutorials


Recommended Posts

Hi I am trying to create a dynamic drop down for my forms from an sql database does anyone know of any good tutorials that are not to hard to get your head around?

 

For example a drop down country which will automatically change the states drop down box value or category and subcategory

hi revraz I was using a script but i do not understand it I have a categories and subcategories fields in my forms I used someone elses script but can't get my head around it like when the user wants to edit the details i can't get it to load with the values they already selected

sorry totally lost me.

 

the original code

<?
$quer2=mysql_query("SELECT DISTINCT category,intCatID FROM tblcatdetails order by category"); 


if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails where intCatID=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails order by subcategory"); } 


echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['intCatID']==@$cat){echo "<option selected value='$noticia2[category]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[intCatID]'>$noticia2[category]</option>";}
}
echo "</select>";
?>





</td></tr>
<tr><td><span id="t_subcat">SUB CATEGORY</span></td></tr>
<tr><td>
<?
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";

;
?>

 

i updated it to this

 


<?
$quer2=mysql_query("SELECT DISTINCT category,intCatID FROM tblcatdetails where category='$category'"); 


if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails where where subcategory='$subcategory'"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails order by subcategory"); } 


echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['intCatID']==@$cat){echo "<option selected value='$noticia2[category]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[intCatID]'>$noticia2[category]</option>";}
}
echo "</select>";
?>





</td></tr>
<tr><td><span id="t_subcat">SUB CATEGORY</span></td></tr>
<tr><td>
<?
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";

;
?>

 

think i must have somethng wrong in my thinking?

 

here is the javascript in the head too

 

<SCRIPT language=JavaScript>
<!--
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='updatead.php?cat=' + val ;
}
function disableselect()
{
<?
if(isset($cat) and strlen($cat) > 0){
echo "document.postad.subcat.disabled = false;";}
else{echo "document.postad.subcat.disabled = true;";}
?>
}
//-->

 

can you please tell me if i am on the right track with this i have no idea never done anything like this before

 

 

 

code for that would be:

 

<?php
connect_db(); //or other db connection here

$query = "SELECT * from TABLE WHERE condition"; //edit table, ignore WHERE condition if no condition
$run_query =mysql_query($query); //run the query;

$selected = $row['selected']; //Change to get value of which one is selected
echo '<select name="name" id="id">'; //Start the select. Edit to ur desire
while($row = mysql_fetch_array($run_query)) {
  if($selected == $row['ROW']) {
  echo '<option selected="selected" value="'.$row['ROW'].'">'.$row['ROW'].'"</option>';  // Loops and makes the selections
}
else {
  echo '<option value="'.$row['ROW'].'">'.$row['ROW'].'"</option>';  // Loops and makes the selections
}
}

 

^^ That is all for looping and getting selected one from DB. Make sure u edit "ROW", $selected, and insert DB details.

Good luck :D

also another problem doesn't display the rest of the items from the dropdown list if the user wants to change the value?

 

<?
$query = "SELECT * from tblcatdetails WHERE category='$category'"; //edit table, ignore WHERE condition if no condition
$run_query =mysql_query($query); //run the query;

$selected = $row['category']; //Change to get value of which one is selected
echo '<select name="name" id="id">'; //Start the select. Edit to ur desire
while($row = mysql_fetch_array($run_query)) {
  if($selected == $row['category']) {
  echo '<option selected="selected" value="'.$row['category'].'">'.$row['category'].'"</option>';  // Loops and makes the selections
}
else {
  echo '<option value="'.$row['category'].'">'.$row['category'].'"</option>';  // Loops and makes the selections
}
}

?>

 

thanks works great except for one thing in the select box it shoes the option for example Boats" and his has an unwanted " next to the option value?

yes of course

here:

 

change :

  echo '<option value="'.$row['ROW'].'">'.$row['ROW'].'"</option>';  // Loops and makes the selections

 

to:

  echo '<option value="'.$row['ROW'].'">'.$row['ROW'].'</option>';  // Loops and makes the selections

 

sorry about that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.