Forum Moderators: coopster

Message Too Old, No Replies

PHP Mysql and dropdown boxes

         

Daxman23

3:34 pm on Nov 22, 2007 (gmt 0)

10+ Year Member



Hi all!
I wrote this 3 level dropdown menu (SEE BELOW) which is populated by 3 Mysql tables.

I would like the user to write its choices into a 4th table that is made just to save these choices

The table is: cat1 - cat2 - cat3

When the button is presset that refferes to a insert.php file the information is written to the database but in the following format:

Example:

1 6 Chicago

Would anyone like to help me with getting the information into the dbase so that the first 2 options also are put there in words instead of numbers?

Thanks a lot!

<?php

$dbservertype='mysql';

$servername='localhost';

$dbusername='#*$!#*$!';

$dbpassword='#*$!#*$!';

$dbname='#*$!#*$!';

connecttodb($servername,$dbname,$dbusername,$dbpassword);

function connecttodb($servername,$dbname,$dbuser,$dbpassword)

{

global $link;

$link=mysql_connect ("$servername","$dbuser","$dbpassword");

if(!$link){die("Could not connect to MySQL");}

mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());

}

?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>

<title></title>

<meta name="GENERATOR" content="Arachnophilia 4.0">

<meta name="FORMATTER" content="Arachnophilia 4.0">

<SCRIPT language=JavaScript>

function reload(form)

{

var val=form.cat.options[form.cat.options.selectedIndex].value;

self.location='dd3.php?cat=' + val ;

}

function reload3(form)

{

var val=form.cat.options[form.cat.options.selectedIndex].value;

var val2=form.subcat.options[form.subcat.options.selectedIndex].value;

self.location='dd3.php?cat=' + val + '&cat3=' + val2 ;

}

</script>

<style type="text/css">
<!--
.stil1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-weight: bold;
}
-->
</style>
</head>

<body>

<p>
<?

$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");

$cat=$HTTP_GET_VARS['cat']; // This line is added to take care if your global variable is off

if(isset($cat) and strlen($cat) > 0){

$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory where cat_id=$cat order by subcategory");

}else{$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory order by subcategory"); }

$cat3=$HTTP_GET_VARS['cat3'];

if(isset($cat3) and strlen($cat3) > 0){

$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 where subcat_id=$cat3 order by subcat2");

}else{$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 order by subcat2"); }

echo "<form method=post name=f1 action='insert_teknik.php'>";

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>";

echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";

while($noticia = mysql_fetch_array($quer)) {

if($noticia['subcat_id']==@$cat3){echo "<option selected value='$noticia[subcat_id]'>$noticia[subcategory]</option>"."<BR>";}

else{echo "<option value='$noticia[subcat_id]'>$noticia[subcategory]</option>";}

}

echo "</select>";

echo "<select name='subcat3' ><option value=''>Select one</option>";

while($noticia = mysql_fetch_array($quer3)) {

echo "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";

}

echo "</select>";

echo "<input type='submit' value='Skicka'>";

echo "</form>";

?>
</p>

<label>

</label>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>

</html>

[edited by: Daxman23 at 3:35 pm (utc) on Nov. 22, 2007]

PHP_Chimp

5:12 pm on Nov 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow there is a lot of code in there. Looking at that much code when im not at work makes my eyes bleed...

$cat=$HTTP_GET_VARS['cat']; // This line is added to take care if your global variable is off

Or you could just use $_GET['cat']

Seeing as my bleeding eyes are getting in the way of me looking through all of your code, could you explain how your cat, subcat and subcat3 (the name's of your select tags) relate to your cat1-cat2-cat3 from your sql table?

You may also want to look at -

<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"

As it is the value that will be entered into the $_GET array. So you are using $noticia[cat_id] for the value (I guess that this is a numeric answer), but $noticia[category] as the text that will be visible to the user. You may well want to use [category] in both places so that you dont get the numeric answer in your database.

Daxman23

9:06 am on Nov 23, 2007 (gmt 0)

10+ Year Member



Hey PHP_Chimp and sorry that i made your eyes bleed:)

To be honest i have tried every single way i can come up with to populate a dropdown box with tables in the database and write "users choice" to a seperate table.

At this point i am pulling my hair out and feel like killing somebody just to get rid of my frustration:)

Can you (or anyone else) PLEASE give me an example of how to accomplish this?

I get the dropdown to work and get the form to call the insert.php file. I also see a new row in the table but the row is either empty or numeric and i need it to be alphabetic.

Your help is greatly appriciated.

//Daxman

[edited by: Daxman23 at 9:39 am (utc) on Nov. 23, 2007]

PHP_Chimp

7:21 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its ok my eyes are getting better :)
Ok I will try to explain and hopefully that will help you sort out your code.
As im not sure how the cat, subcat & subcat3 relate to your sql tables I will use other names, but hopefully it will point you in the correct direction.

Assuming this is the html that you are working with -
You already have the code for generating similar html in your post, so i haven't included all of the php needed to get to this.


<select name="yourInput">
<option value="yourValue1">yourValue1</option>
<option value="yourValue2">YourValue2</option>
</select>
<select name="userInput">
<option value="userValue1">userValue1</option>
<option value="userValue2">userValue2</option>
</select>

If you user chooses yourValue2 and userInput1 then the $_GET array will look like -

array (
yourInput => yourValue2
userInput => userValue1
)

What you had in your original code was a different string (I would say different value...but in the interest of clarity as I'm going to be talking about values in terms of the select tag, I will call it a string) in the value as opposed the html in between the option tags. So you see something like myChoice but the string that is sent to the $_GET array is 2 as this is the string in the value attribute.
So the $_GET array key comes from the name of the select tag and the value comes from the value attribute selected. However if you have a different value to the string that is shown to the user it is the value attribute that is used not the string between the option tags.
So im guessing that if you change
<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"

to -

<option selected value='$noticia2[category]'>$noticia2[category]</option>"

You will have what you want.

<disclaimer>
I still haven't read through all of your code...so this may not be exactly what you want. However hopefully it will get you closer then you could repost less code and we could work from there ;)