chrisidas Posted July 25, 2011 Share Posted July 25, 2011 Hey, I have a form which includes drop down boxes that show a list of options from my database. The problem i have is, i use a function to create my <options>, and they all have the same <select> name, so i have no idea how to fetch them in my form script. Here's my code to make the options, thanks to Nightslyr <?php function makeSelects($dbResult) { mysql_data_seek($dbResult, 0); while($row = mysql_fetch_assoc($dbResult)) { echo "<option value='{$row['playername']}'>{$row['playername']}</option>"; } } function homeScorers($dbResult) { echo '<select name="homeScorers[]" class="hidden-home" >'; makeSelects($dbResult); echo '</select>'; } function awayScorers($dbResult) { echo '<select name="awayScorers[]" class="hidden-away" >'; makeSelects($dbResult); echo '</select>'; } function homeYellowCards($dbResult) { echo '<select name="homeYellowCards[]" class="hidden-home" >'; makeSelects($dbResult); echo '</select>'; } function awayYellowcards($dbResult) { echo '<select name="awayYellowCards[]" class="hidden-away" >'; makeSelects($dbResult); echo '</select>'; } function homeRedCards($dbResult) { echo '<select name="homeRedCards[]" class="hidden-home" >'; makeSelects($dbResult); echo '</select>'; } function awayRedCards($dbResult) { echo '<select name="awayRedCards[]" class="hidden-away" >'; makeSelects($dbResult); echo '</select>'; } $homeQuery = "SELECT * FROM players WHERE teamname = '$hometeam'"; $homeResult = mysql_query($homeQuery); $awayQuery = "SELECT * FROM players WHERE teamname = '$awayteam'"; $awayResult = mysql_query($awayQuery); ?> and here's the code for the form <form action="result-script.php" method="post" name="myForm"> <table class="post-result" border="0" cellspacing="0" cellpadding="10"> <tr> <td class="post-result-home"><?php echo $hometeam ?> <input type="hidden" name="hometeam" value="<?php echo $hometeam ?>"> </td> <td class="post-result-away"><?php echo $awayteam ?> <input type="hidden" name="awayteam" value="<?php echo $awayteam ?>"> </td> </tr> <tr> <td class="post-result-home">Home Goals <select name="homescore" id="homescore"> <?php for($a = 0; $a < 11; ++$a) { echo "<option value='$a'>$a</option>"; } ?> </select> </td> <td class="post-result-away"> <select name="awayscore" id="awayscore"> <?php for($a = 0; $a < 11; ++$a) { echo "<option value='$a'>$a</option>"; } ?> </select> Away Goals</td> </tr> <tr> <td class="post-result-home"> <?php for($i = 0; $i < 10; ++$i) { homeScorers($homeResult); } ?></td> <td class="post-result-away"> <?php for($i = 0; $i < 10; ++$i) { awayScorers($awayResult); } ?> </td> </tr> <tr> <td class="post-result-home">Home Yellow Cards <select name="homeyellowcards" id="homeyellowcards"> <?php for($a = 0; $a < 11; ++$a) { echo "<option value='$a'>$a</option>"; } ?> </select></td> <td class="post-result-away"><select name="awayyellowcards" id="awayyellowcards"> <?php for($a = 0; $a < 11; ++$a) { echo "<option value='$a'>$a</option>"; } ?> </select> Away Yellow Cards</td> </tr> <tr> <td class="post-result-home"> <?php for($i = 0; $i < 10; ++$i) { homeYellowCards($homeResult); } ?></td> <td class="post-result-away"> <?php for($i = 0; $i < 10; ++$i) { awayYellowCards($awayResult); } ?> </td> </tr> <tr> <td class="post-result-home">Home Red Cards <select name="homeredcards" id="homeredcards"> <?php for($a = 0; $a < 11; ++$a) { echo "<option value='$a'>$a</option>"; } ?> </select></td> <td class="post-result-away"><select name="awayredcards" id="awayredcards"> <?php for($a = 0; $a < 11; ++$a) { echo "<option value='$a'>$a</option>"; } ?> </select> Away Red Cards</td> </tr> <tr> <td class="post-result-home"> <?php for($i = 0; $i < 10; ++$i) { homeRedCards($homeResult); } ?></td> <td class="post-result-away"> <?php for($i = 0; $i < 10; ++$i) { awayRedCards($awayResult); } ?> I've tried various ways of getting the result in my script but they all return errors. How would i make it so if two options are selected for say"homeScorers", i can set them as different variables in my form script. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/ Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 Neither of your scripts you posted have the same input name. You are using a loop to generate the option, not the <select> itself. Quote Link to comment https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/#findComment-1246463 Share on other sites More sharing options...
chrisidas Posted July 25, 2011 Author Share Posted July 25, 2011 What do you mean? How would i go about changing it so i could get the values in my script? Quote Link to comment https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/#findComment-1246473 Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 What do you mean? How would i go about changing it so i could get the values in my script? What do you mean change it? you have all of your <select> with a name assign to it, you don't get values based on the <option> names.. Try this and see if you figure it out, I dont want to just spoon feed you the answers. <?php <?php $test=$_POST['homeScorer']; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } ?> ?> Quote Link to comment https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/#findComment-1246477 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 The way you word your question is very confusing. Either way, if you make your select box "homeScorer[]" but don't make it like <select name="homeScorer[]" multiple>, then having [] on homeScorer is pointless. Quote Link to comment https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/#findComment-1246481 Share on other sites More sharing options...
chrisidas Posted July 25, 2011 Author Share Posted July 25, 2011 Cheers guys, will give it a go Quote Link to comment https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/#findComment-1246482 Share on other sites More sharing options...
chrisidas Posted July 25, 2011 Author Share Posted July 25, 2011 I've tried both of the above and still no closer to figuring it out. Is it actually possible to get values, with the way that my <select> and <option> are created? I think the reason they are created like that is because i use JavaScript to show and hide them on the page. Quote Link to comment https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/#findComment-1246738 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.