Jump to content

Need help with dynamic dropdown


geekisthenewsexy

Recommended Posts

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??? :facepalm: ) 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? :(

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.