geekisthenewsexy Posted January 7, 2011 Share Posted January 7, 2011 Hi guys. I am having a hard time finding a solution for this, is it possible to get not the value of a dropdown (oh what's it called??? ) but what is in between of the <option> tag?like, <select name="catID"> <option value=$row['c_id']>$row['c_name']</option> and save it to the database??cuz I'm using a dynamic dropdown which bases the content of another dropdown by the id of the previous. And so, if i save it to the database, instead of for example "BSA" is saved, the id of "BSA" which is "1" is saved..any ideas guys? Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/ Share on other sites More sharing options...
AE117 Posted January 7, 2011 Share Posted January 7, 2011 Thats mighty confusing do you have any code I can see the written description is throwing me off Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156134 Share on other sites More sharing options...
dragon_sa Posted January 7, 2011 Share Posted January 7, 2011 cant you just set $row['c_name'] as the value? Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156135 Share on other sites More sharing options...
geekisthenewsexy Posted January 7, 2011 Author Share Posted January 7, 2011 sorry guys if it's a bit confusing.. nope, afraid i can't dragon..because if i did that, it will ruin the dynamic dropdown thing.. Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156136 Share on other sites More sharing options...
geekisthenewsexy Posted January 7, 2011 Author Share Posted January 7, 2011 okay,here's the full code..should i post the javascript too? <table cellpadding="5px" cellspacing="4px;" style="margin-left:1px;"> <tr> <td><label for="course">Course: </label></td> <td><div id="first_drop_down"> <select name="catId" onChange="get_subcat(this.value);get_subcat2(this.value);get_subcat3(this.value)"> <option>Select one</option> <?php $query=mysql_query("SELECT DISTINCT c_id, c_name FROM admin_course"); while($row=mysql_fetch_array($query)) { echo "<option value='$row[c_id]'>$row[c_name]</option>"; } ?> </select> </div> </td> </tr> <tr> <td><label for="course">Year: </label></td> <td> <div id="subcat_div"> <?php include('b.php');?> </div> </td> </tr> <tr> <td><label for="course">Block: </label></td> <td> <div id="subcat2_div"> <?php include('c.php');?> </div> </td> </tr> <tr> <td><label for="course">Room: </label></td> <td> <div id="subcat3_div"> <?php include('d.php');?> </div> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156137 Share on other sites More sharing options...
dragon_sa Posted January 7, 2011 Share Posted January 7, 2011 you could set the value like this for example value='$row[c_id]-$row[c_name]' and then explode on the '-' to get the 2 values value[0] would be the id and value[1] would be the name between the option tags Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156139 Share on other sites More sharing options...
geekisthenewsexy Posted January 7, 2011 Author Share Posted January 7, 2011 and then explode on the '-' to get the 2 values value[0] would be the id and value[1] would be the name between the option tags thanks dragon_sa, but can you show me how to do that? Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156141 Share on other sites More sharing options...
dragon_sa Posted January 7, 2011 Share Posted January 7, 2011 $value=$_POST['catId']; $data=explode("-", $value); $id=$data[0]; $name=$data[1]; Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156145 Share on other sites More sharing options...
geekisthenewsexy Posted January 7, 2011 Author Share Posted January 7, 2011 thanks! i'll try it out. Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156148 Share on other sites More sharing options...
geekisthenewsexy Posted January 8, 2011 Author Share Posted January 8, 2011 Hi guys. i've tried the code and it worked well but i have a bit of a problem. on the second dropdown which is 'yearlevel', the value that's being saved is always 0..the other dropdowns are okay. i don't know where i've gone wrong. please take look at the code.. <?php include("dbconnection_wmsuipil.php"); $subjvalue=$_POST['mainCat']; $subjdata=explode("-", $subjvalue); $subjid=$subjdata[0]; $subjname=$subjdata[1]; ///for first dropdown $cvalue=$_POST['catId']; $cdata=explode("-", $cvalue); $cid=$cdata[0]; $cname=$cdata[1]; ///for second dropdown (yearlevel) $yval=$_POST['subcatId']; $ydata=explode("-", $yval); $yid=$ydata[0]; $yname=$ydata[1]; ///third dropdown $bvalue=$_POST['subcat2Id']; $bdata=explode("-", $bvalue); $bid=$bdata[0]; $bname=$bdata[1]; $sql=mysql_query("INSERT INTO stud_records (teacher_id, teacher_name,subj_id, prospectuscode, subj_code, course_desc, unit, day, time, time2,course, yearlevel,block,room, Sem, school_year) VALUES ('$_POST[t_id]','$_POST[name]','$subjname','$_POST[pname]','$_POST[code]','$_POST[desc]','$_POST[units]','$_POST[days]','$_POST[time1]','$_POST[time2]','$name','$yname','$name3','$_POST[subcat3Id]','$_POST[sem1]','$_POST[school_year1]')")or die (mysql_error()); echo "1 record added"; ?> this is b.php: <?php include("dbconnection_wmsuipil.php"); @$catId=intval($_GET['catId']); if ($catId!=0) { $query=mysql_query("SELECT DISTINCT year, y_id FROM admin_year where c_id='$catId'")or die (mysql_error()); } else { $query=mysql_query("SELECT DISTINCT year, y_id FROM admin_year order by year")or die (mysql_error()); } ?> <select name="subcatId" onchange="get_subcat2(this.value)"> <option>Select one</option> <?php while($row=mysql_fetch_array($query)) { echo "<option value='$row[y_id]-$row[year]'>$row[year]</option>"; } ?> </select> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156541 Share on other sites More sharing options...
dragon_sa Posted January 8, 2011 Share Posted January 8, 2011 the first thing to do is look at the html output for b.php and see what values you are getting for the second dropdown box also change if ($catId!=0) to if ($catId!='0') Quote Link to comment https://forums.phpfreaks.com/topic/223660-need-help-with-dynamic-dropdown/#findComment-1156578 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.