This question already has an answer here:
I am new to coding. trying to learn via building a personal finance web management app.
I have two dropdown menus - the second menu changes its options based upon the selection from the first dropdown.
I did not write the entirety of this code and therefore dont fully understand it.
My question is this: how do i send the selected option from each of the dropdowns to a Mysql database?
I have attempted to add code towards the bottom that does this however no matter what i put i keep getting this error " Undefined index" for line 74.
Again, forgive my ignorance - trying to learn by actually building something. this is just a chunk of code that i'm experimenting with - when it works i will fit it into my actual document.
<?php
$sports_arr = array();
$sports_arr[] = "Basketball";
$sports_arr[] = "Baseball";
$sports_arr[] = "Football";
$position = array();
$position['Basketball'][] = "Power Forward";
$position['Basketball'][] = "Small Forward";
$position['Basketball'][] = "Center";
$position['Soccer'][] = "Center Forward";
$position['Soccer'][] = "Right Wing";
$position['Soccer'][] = "Left Wing";
$position['Football'][] = "Halfback";
$position['Football'][] = "Fullback";
$position['Football'][] = "Wide Reciever";
$position['Football'][] = "Tight End";
$position['Football'][] = "Center";
?>
<!-- Firstdropdown selection -->
<div class="home">
<select id="s1">
<?php foreach($sports_arr as $sa) { ?>
<option value="<?php echo $sa; ?>"><?php echo $sa; ?></option>
<?php } ?>
</select>
<!-- Second dropdown selection -->
<select id="s2">
</select>
</div>
<script type="text/javascript">
var s1= document.getElementById("s1");
var s2 = document.getElementById("s2");
onchange(); //Change options after page load
s1.onchange = onchange; // change options when s1 is changed
function onchange() {
<?php foreach ($sports_arr as $sa) {?>
if (s1.value == '<?php echo $sa; ?>') {
option_html = "";
<?php if (isset($position[$sa])) { ?> // Make sure position is exist
<?php foreach ($position[$sa] as $value) { ?>
option_html += "<option><?php echo $value; ?></option>";
<?php } ?>
<?php } ?>
s2.innerHTML = option_html;
}
<?php } ?>
}
</script>
<input type="submit" name="Submit> value="Submit">
<?php
/*Connect to Database*/
$connect = mysqli_connect("localhost", "root", "", "test_db");
/* place coresponding values into database in same order*/
$sql = "INSERT INTO hello( Sport, Position)
VALUES
('".$_POST["option.html"]."')";
if(mysqli_query($connect, $sql))
{
echo 'Data Inserted';
}
?>
</div>