hannap Posted June 11, 2012 Share Posted June 11, 2012 I have a problem with saving data gained with Dropdown list. The script below saves the length of the vector $labels (which is 3) instead of the value chosen from a dropdown list. How can I save the value chosen from a Dropdown list to the MySQL table? Thank you!! <?php mysql_connect("","root"); mysql_select_db("chat"); $res1=mysql_query("select labelm1 from collaboration_cross1 where labelm1 != ''"); $dsatz1 = mysql_fetch_assoc($res1); $labm1=$dsatz1["labelm1"]; $res2=mysql_query("select labelm2 from collaboration_cross1 where labelm2 != ''"); $dsatz2 = mysql_fetch_assoc($res2); $labm2=$dsatz2["labelm2"]; $labels = array( '' => 1, $labm1 => 2, $labm2 => 3 ); function generateSelect($name, $options) { $html = '<form name="f" action="dropdown_CROSS1.php" method="post"><div align="center"><label for="frmcounty">Label:</label><select name="'.$name.'" id="LabelChos">'; foreach ($options as $option => $value) { $html .= '<option value='.$value.'>'.$option.'</option>'; } $html .= '</select></div></form>'; echo "$html"; return $value; } $value = generateSelect('labelchosen', $labels); if(isset($value)) { mysql_connect("","root"); mysql_select_db("chat"); mysql_query("insert into collaboration_cross1 (labelchosen1) values ('" . $value . "')"); /* Chat-Anzeige aktualisieren */ echo "<script type='text/javascript'> reload();</script>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/263981-sending-information-from-dropdown-list-to-mysql/ Share on other sites More sharing options...
Barand Posted June 11, 2012 Share Posted June 11, 2012 your function always returns the last value in the array Quote Link to comment https://forums.phpfreaks.com/topic/263981-sending-information-from-dropdown-list-to-mysql/#findComment-1352872 Share on other sites More sharing options...
hannap Posted June 11, 2012 Author Share Posted June 11, 2012 Thanks Barand! But what do I need to write to return (instead of return $value) to make the function to return the value chosen from the Dropdown list? Quote Link to comment https://forums.phpfreaks.com/topic/263981-sending-information-from-dropdown-list-to-mysql/#findComment-1352881 Share on other sites More sharing options...
Barand Posted June 11, 2012 Share Posted June 11, 2012 You need to submit the form to get the chosen value. The value will be sent to "dropdown_CROSS1.php" where you can get the value from $_POST['labelchosen'] Quote Link to comment https://forums.phpfreaks.com/topic/263981-sending-information-from-dropdown-list-to-mysql/#findComment-1352882 Share on other sites More sharing options...
hannap Posted June 11, 2012 Author Share Posted June 11, 2012 Now I got it! Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/263981-sending-information-from-dropdown-list-to-mysql/#findComment-1352887 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.