Jump to content

How do i get these values from my form?


chrisidas

Recommended Posts

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 :)

 

Link to comment
https://forums.phpfreaks.com/topic/242687-how-do-i-get-these-values-from-my-form/
Share on other sites

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 />';}
}
?>
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.