Jump to content

Recommended Posts

I am trying to accomplish a custom State / City drop down using a database and I am running into a small problem of pulling the wrong variable (field from the database). My end result is this

 

State is 4

City is Jonesboro

 

As you can see there are no states called "4", that is the cat_id assigned to the state which would be Arkansas. I tried switching around the variables a bit to try and display the state Arkansas instead of "4" but no such luck yet. Any ideas?

 

dd.php

<?php

@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
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>";
//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>

 

 

 

 

 

 

dd-check.php

<?php
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];

echo "State is $cat <br>City is $subcat ";

?>

Link to comment
https://forums.phpfreaks.com/topic/168145-variable-not-displayed-correctly/
Share on other sites

I take it the field category is the 'state' name?

 

 

 

Correct, I tried to switch out cat_id to category in the code but I am not getting it to work, I guess the coding is just not that good to manipulate ? I figured if I switched the value which is the variable that will be passed to the next page dd-check.php that it would work ..... Just cant get it to work

 

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

Ok, thought so, because you are never actually posting this value from the from. Only the category id and subcategory ever get posted, hence the values that you are outputting.

In that case try sending the category as a value in a hiden form field as it is only really needed for display purposes that i can see :-)

Ok, thought so, because you are never actually posting this value from the from. Only the category id and subcategory ever get posted, hence the values that you are outputting.

In that case try sending the category as a value in a hiden form field as it is only really needed for display purposes that i can see :-)

 

 

I think I understand what you are getting at and tried something like this

echo "<form method=get name=f1 action='dd-check.php'><input type='hidden' value='$noticia2[category]'>";
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";

 

and also tried putting the value hidden else where.....If this is not what you meant let me know.

 

 

Also for anyone else maybe you may see something else that could help me out as well, this is crazy.....

I still am trying to figure this out and have come up with a way to rig the code but it's just like an array and the fact that the information is already in the database and I can not pull it is frusturating....

 

<?php
$cat=$_GET['cat'];
$subcat=$_GET['subcat'];

if ($cat == '1'){$state = "AL";}
if ($cat == '2'){$state = "AK";}
if ($cat == '3'){$state = "AZ";}
if ($cat == '4'){$state = "AR";}
if ($cat == '5'){$state = "CA";}
if ($cat == '6'){$state = "CO";}
if ($cat == '7'){$state = "CT";}
if ($cat == '8'){$state = "DE";}
if ($cat == '9'){$state = "DC";}
if ($cat == '10'){$state = "FL";}

echo "State is $state <br>City is $subcat<br><br>";

 

As you can see I can easily just call the city by $subcat, but if i do state is $cat then it returns nothing, instead i have to delcare a variable for every state above.... really weird.... Trying to use less code...alot less. Let me know if I need to provide more code. This is pulling from 2 tables with in a database, one for each drop down, category being the state and subcategory being the city....each has a auto incrementing id assigned to each city or state, hence the error i have been getting.

I am not sure if this was your latest code or not, but i think the problem may be here

 

else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}

 

you are assigning a value to the option of the category ID rather than the name.  the "value" field is what will be sent to the next page.  Change it to

 

else{echo  "<option value='$noticia2[category]'>$noticia2[category]</option>";}

 

and i think it should work

your codes a mess, and very hard to read (but that may be due to the forums) but from what I can see, you never pass the name of the state to the next page.

 

<input type="hidden" value="<? echo $state_name; ?>" />

 

where $state_name is the name of the state you pulled from the data based. that will create a hidden field to send the state name to the next page, but I really can't read your code to well, so I can't tell if this will apply

 

hope that helps

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.