Link to home
Start Free TrialLog in
Avatar of t3chguy
t3chguyFlag for United States of America

asked on

Option Value Selected from array Help

I have a program in which I'm allowing a user to enter the number of inputs per question.  The POST works fine, but the part I'm stuck on is setting those numbers to selected when the page is reposted.

The source code is actually setting selected to multiple values:

<select name="inputsdesired1" onChange = "this.form.submit()">                                    
<option value="1" selected = 'selected'1>1</option>
<option value="2" 1>2</option>
<option value="3" 1>3</option>
<option value="4" 1>4</option>
<option value="5" 1>5</option>
<option value="6" 1>6</option>
<option value="7" 1>7</option>
<option value="8" 1>8</option>
<option value="9" 1>9</option>
<option value="10" 1>10</option>
</select>

<tr>
<td valign="top" align="right">Question 2: </td>
<td>
<select name="inputsdesired2" onChange = "this.form.submit()">                                    
<option value="1" selected = 'selected'2>1</option>
<option value="2" 2>2</option>
<option value="3" 2>3</option>
<option value="4" 2>4</option>
<option value="5" 2>5</option>
<option value="6" 2>6</option>
<option value="7" 2>7</option>
<option value="8" selected = 'selected'2>8</option>
<option value="9" 2>9</option>
<option value="10" 2>10</option>
</select>

<tr>
<td valign="top" align="right">Question 3: </td>
<td>
<select name="inputsdesired3" onChange = "this.form.submit()">                                    
<option value="1" selected = 'selected'3>1</option>
<option value="2" 3>2</option>
<option value="3" 3>3</option>
<option value="4" 3>4</option>
<option value="5" selected = 'selected'3>5</option>
<option value="6" 3>6</option>
<option value="7" 3>7</option>
<option value="8" selected = 'selected'3>8</option>
<option value="9" 3>9</option>
<option value="10" 3>10</option>
</select>


My code is here:


for($i = 1; $i <= $totalquestions; ++$i)
      {
      echo "Inputs Desired: " . htmlentities($_POST['inputsdesired' . $i]) . '<br /><br />';
      }

            <tr>
                  <td valign="top" colspan="2">How many total inputs per question would you like?</td>
            </tr>
            <?php
                        
            for($i = 1; $i <= $totalquestions; ++$i)
                  {
                  if ($_POST['inputsdesired' . $i] == '1')
                  {
                  $inputsdesired1 = $i;
                  $inputsdesired1 = "selected = 'selected'";
                  }
                  
            if ($_POST['inputsdesired' . $i] == '2')
                  {
                  $inputsdesired2 = $i;
                  $inputsdesired2 = "selected = 'selected'";
                  }
                  
            if ($_POST['inputsdesired' . $i] == '3')
                  {
                  $inputsdesired3 = $i;
                  $inputsdesired3 = "selected = 'selected'";
                  }
            
            if ($_POST['inputsdesired' . $i] == '4')
                  {
                  $inputsdesired4 = $i;
                  $inputsdesired4 = "selected = 'selected'";
                  }
                  
            if ($_POST['inputsdesired' . $i] == '5')
                  {
                  $inputsdesired5 = $i;
                  $inputsdesired5 = "selected = 'selected'";
                  }
                  
                  
            if ($_POST['inputsdesired' . $i] == '6')
                  {
                  $inputsdesired6 = $i;
                  $inputsdesired6 = "selected = 'selected'";
                  }
                  
            if ($_POST['inputsdesired' . $i] == '7')
                  {
                  $inputsdesired7 = $i;
                  $inputsdesired7 = "selected = 'selected'";
                  }
                  
            if ($_POST['inputsdesired' . $i] == '8')
                  {
                  $inputsdesired8 = $i;
                  $inputsdesired8 = "selected = 'selected'";
                  }
                  
            if ($_POST['inputsdesired' . $i] == '9')
                  {
                  $inputsdesired9 = $i;
                  $inputsdesired9 = "selected = 'selected'";
                  }
                  
            if ($_POST['inputsdesired' . $i] == '10')
                  {
                  $inputsdesired10 = $i;
                  $inputsdesired10 = "selected = 'selected'";
                  }
            
            else
                  {
                  $inputsdesired = '';
                  }
                        
      
                  ?>
                  <tr>
                        <td valign="top" align="right">Question <?php echo $i;?>: </td>
                        <td>
                        <?php echo '<select name="inputsdesired'.$i.'" onChange = "this.form.submit()">';?>
                                    <option value="1" <?php echo $inputsdesired1.$i;?>>1</option>
                                    <option value="2" <?php echo $inputsdesired2.$i;?>>2</option>
                                    <option value="3" <?php echo $inputsdesired3.$i;?>>3</option>
                                    <option value="4" <?php echo $inputsdesired4.$i;?>>4</option>
                                    <option value="5" <?php echo $inputsdesired5.$i;?>>5</option>
                                    <option value="6" <?php echo $inputsdesired6.$i;?>>6</option>
                                    <option value="7" <?php echo $inputsdesired7.$i;?>>7</option>
                                    <option value="8" <?php echo $inputsdesired8.$i;?>>8</option>
                                    <option value="9" <?php echo $inputsdesired9.$i;?>>9</option>
                                    <option value="10" <?php echo $inputsdesired10.$i;?>>10</option>
                        </select>
                        
                  <? } ?>
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

First off, why dont put the list into an array:

and use a for loop to print out and analyse the values?
Avatar of t3chguy

ASKER

Which list into a for loop?
Try this code.

<?php
      for( $i = 1; $i <= $totalquestions; ++$i )
      {
            echo "Inputs Desired: " . htmlentities($_POST['inputsdesired' . $i]) . '<br /><br />';
      }
?>
      <tr>
            <td valign="top" colspan="2">How many total inputs per question would you like?</td>
      </tr>
<?php
      for($i = 1; $i <= $totalquestions; ++$i)
      {
?>
      <tr>
            <td valign="top" align="right">Question <?php echo $i;?>: </td>
            <td>
                  <select name="inputsdesired<?php echo $i;?>" onChange = "this.form.submit()">
<?php
            for( $j=1; $j <= 10 ; $j++ )
            {
                  if( $_POST['inputsdesired'.$i] == $j )
                  {
?>
                        <option value="<?php echo $j;?>" title="<?php echo $i;?>" selected="selected"><?php echo $j;?></option>
<?php
                  }
                  else
                  {
?>
                        <option value="<?php echo $j;?>" title="<?php echo $i;?>"><?php echo $j;?></option>
<?php
                  }
            }
?>
                  </select>
            </td>
      </tr>
<?php
      }
?>
I'm not sure I understand the question, but have a look at this simplified example to see what I think you may be asking about.  I used GET instead of POST but the principles are the same.
www.laprbass.com/RAY_temp_t3chguy.php
<?php // RAY_temp_t3chguy.php
error_reporting(E_ALL);
echo "<pre>";

// SHOW THE INPUTS FROM THE REQUEST
var_dump($_GET);

// PRESERVE STATE FROM THE REQUEST TO THE FORM
$s[1] = $s[2] = $s[3] = $s[4] = NULL;
if (!empty($_GET['thing']))
{
    $s[$_GET['thing']] = ' selected';
}

// CREATE THE FORM USING HEREDOC NOTATION
$htm = <<<HTM
<form>
<select name="thing" onChange = "this.form.submit()">
<option value="1"{$s[1]}>1</option>
<option value="2"{$s[2]}>2</option>
<option value="3"{$s[3]}>3</option>
<option value="4"{$s[4]}>4</option>
</select>
</form>
HTM;

echo $htm;

Open in new window

Run it and use "view source" to see the generated HTML.  HTH, ~Ray
<?
if (isset($_POST)){
print_r($_POST);
}
$c=range(0,10);
echo "<form action='".$_SERVER['PHP_SELF']." 'method='post'>";
echo "<select name='tt'>";
$selected="selected='selected'";
foreach($c as $item){
    echo "<option name='test' value='$item'".(($_POST['tt']==$item)?$selected:"").">$item</option>";
}
echo "</select>";
echo "<input type='submit'>";
echo "</form>";

Open in new window

here is the same one with 3 options:

<?
if (isset($_POST)) {
    print_r($_POST);
}

echo "<form action='" . $_SERVER['PHP_SELF'] . " 'method='post'>";
$c = range(0, 20);
echo "<select name='tt' onChange='this.form.submit();'>";
$selected = "selected='selected'";
foreach ($c as $item) {
    echo "<option name='test' value='$item' " . (($_POST['tt'] == $item) ? $selected : "") . ">$item</option>";
}
echo "</select><br><br>";
$c = range(0, 3);
echo "<select name='ff' onChange='this.form.submit();'>";
foreach ($c as $item) {
    echo "<option name='test' value='$item' " . (($_POST['ff'] == $item) ? $selected : "") . ">$item</option>";
}
echo "</select><br><br>";
$c = range(0, 9);
echo "<select name='gg' onChange='this.form.submit();'>";
foreach ($c as $item) {
    echo "<option name='test' value='$item' " . (($_POST['gg'] == $item) ? $selected : "") . ">$item</option>";
}
echo "</select><br><br>";
echo "</form>";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial