Jump to content

[SOLVED] Can any one help me in “ListBox”.


oceans

Recommended Posts

Dear People,

 

Can any one help me in “ListBox”.

I know how to get info from the list box and process.

Now the challenge is; when a data comes from the data base, the list box should select show the value. By default, first entry comes first right!

I have NOT done this before in ASP as well.

Link to comment
Share on other sites

Dear Friend,

 

You have done the search portion, I am doing the same, please help me with the selection portion. Lets say an example:

 

"

      <td><select name="Combo2" size="1" class="WorkPageField" id="Combo2">

        <option value="Yes">Yes</option>

        <option value="No">No</option>

      </select></td>

 

"

The above is with the values "Yes" and "No". My data from DataBase is "No", how shell I instruct the ListBox to Show "No", if not it will show "Yes", by default due it being the first Entry.

 

Thanks.

Link to comment
Share on other sites

<td><select name="Combo2" size="1" class="WorkPageField" id="Combo2">
        <option value="Yes" <? if($theValue = "Yes"){ echo "selected"; } ?>>Yes</option>
        <option value="No" <? if($theValue = "No"){ echo "selected"; } ?>>No</option>
      </select></td>

 

change $theValue to the name of the variable you are trying to match

Link to comment
Share on other sites

Yep, sorry, forgot to double up the = signs

 

Should be:

 

<td><select name="Combo2" size="1" class="WorkPageField" id="Combo2">
        <option value="Yes" <? if($theValue == "Yes"){ echo "selected"; } ?>>Yes</option>
        <option value="No" <? if($theValue == "No"){ echo "selected"; } ?>>No</option>
      </select></td>

 

Dont forget to ctrl+F5 to refresh the page after making the changes.  You need the form to reset itself properly

Link to comment
Share on other sites

Thanks SanFly,

 

"

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?PHP

//Transfer Data from Screen to Memory

$NumberOfTxtBoxes=5;

$NumberOfTxtBoxesToBeFilled=5;

$theValue = "No";

if (isset($_POST['Submit']))

{

for ($i=1; $i<=$NumberOfTxtBoxes; $i++)

{

$InputFromScreen[$i]=strtoupper($_POST["Txt".$i]);

}

}

else

{

for ($i=1; $i<=$NumberOfTxtBoxes; $i++)

{

$InputFromScreen[$i]="";

}

}

?>

<form id="form1" name="form1" method="post" action="Untitled-1.php">

  <table width="200" border="1">

    <tr>

      <td> </td>

      <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt33" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td>

    </tr>

    <tr>

      <td> </td>

<td><select name="Combo2" size="1" class="WorkPageField" id="Combo2">

        <option value="Yes" <? if($theValue == "Yes"){ echo "selected"; } ?>>Yes</option>

        <option value="No" <? if($theValue == "No"){ echo "selected"; } ?>>No</option>

      </select></td>

    </tr>

    <tr>

      <td> </td>

      <td><select name="select" size="1" class="WorkPageField" id="select">

        <option value="Yes">Yes</option>

        <option value="No">No</option>

      </select></td>

    </tr>

    <tr>

      <td> </td>

      <td><?PHP

?></td>

    </tr>

    <tr>

      <td> </td>

      <td><input type="submit" name="Submit" value="Submit" /></td>

    </tr>

  </table>

</form>

</body>

</html>

 

"

Link to comment
Share on other sites

you should also look at your html source code to see what's going on there.

this is what it should look like:

<?php
        echo "
                <select name=\"Combo2\" size=\"1\" class=\"WorkPageField\" id=\"Combo2\">
                <option value=\"Yes\"". (($theValue == 'Yes') ? (' SELECTED') : ('') .">Yes
                <option value=\"No\"". (($theValue == 'No') ? (' SELECTED') : ('') .">No
                </select>
        ";
?>

 

there needs to be a space between the rest of your option tags and 'SELECTED'.

Link to comment
Share on other sites

SanFly,

 

Initially I had a lot of problem with my server php MySQL, after 3 days of forums, I was instructed to add

 

"

<?php

ob_start();

?>

"

 

then the problem went away.

 

My situation was the same then, forum's advisor had no problem, but I was dieing.

 

Still no luck, I can't understand your solution boo_lolly.

Link to comment
Share on other sites

There was a space, directly after the "Yes" or "No"

 

yes, but if one of them wasn't 'pre-selected', there would be a space between value="" and >. some browsers will react differently to this html syntax error. so, in my opinion, the space should be with the SELECTED.

Link to comment
Share on other sites

assuming that

 

//put this inside conditional if(isset($_POST.....

$Combo2 = $_POST['Combo2'];



<select name="Combo2" size="1" class="WorkPageField" id="Combo2">
<option value="Yes" <? if($Combo2 == "Yes"){ echo "selected"; } ?>>Yes</option>
<option value="No" <? if($Combo2 == "No"){ echo "selected"; } ?>>No</option>
</select>

Link to comment
Share on other sites

whoops, i guess i lost u a lil bit...

 

if you want these fields to be populated by vars set in the database, make sure that in the db the field values are listed as either yes or no, then query the db and generate an array of the row in question

 

if you want these to be populated by posted content, i.e. after form is posted and form is returned, values are selected, use the other method i told u about

 

both examples are in the code, just delete the commented lines above and below the code you wish to use

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?PHP
//Transfer Data from Screen to Memory
$NumberOfTxtBoxes=5;
$NumberOfTxtBoxesToBeFilled=5;
$theValue = "No";
if (isset($_POST['Submit']))
{

/* this grabs the variables from the posted form, if thats what you want
   $var1 = $_POST['var1'];
   $var2 = $_POST['var2'];
*/

/* this grabs the variables from a table
   $sql = "SELECT * from TABLE WHERE field='SOMETHING'";
   $result = mysql_query($sql);
   $row = mysql_fetch_array($result);
   $var1 = $row['var1'];
   $var2 = $row['var2'];
*/

   for ($i=1; $i<=$NumberOfTxtBoxes; $i++)
   {
      $InputFromScreen[$i]=strtoupper($_POST["Txt".$i]);
   }
}
else
{
   for ($i=1; $i<=$NumberOfTxtBoxes; $i++)
   {
      $InputFromScreen[$i]="";
   }
}
?>
<form id="form1" name="form1" method="post" action="Untitled-1.php">
  <table width="200" border="1">
    <tr>
      <td> </td>
      <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt33" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td>
    </tr>
    <tr>
      <td> </td>
   <td><select name="var2" size="1" class="WorkPageField" id="var2">
<option value="Yes" <? if($var2 == "Yes"){ echo "selected"; } ?>>Yes</option>
<option value="No" <? if($var2 == "No"){ echo "selected"; } ?>>No</option>
</select></td>
     </tr>
    <tr>
      <td> </td>
      <td><select name="var1" size="1" class="WorkPageField" id="var2">
<option value="Yes" <? if($var1 == "Yes"){ echo "selected"; } ?>>Yes</option>
<option value="No" <? if($var1 == "No"){ echo "selected"; } ?>>No</option>
</select></td>
    </tr>
    <tr>
      <td> </td>
      <td><?PHP
?></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>

Link to comment
Share on other sites

Thanks for your time,

 

Please run this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?PHP

//Transfer Data from Screen to Memory

$NumberOfTxtBoxes=5;

$NumberOfTxtBoxesToBeFilled=5;

  $var1 = "No";

  $var2 = "No";

if (isset($_POST['Submit']))

{

 

/* this grabs the variables from the posted form, if thats what you want

  $var1 = $_POST['var1'];

  $var2 = $_POST['var2'];

*/

 

/* this grabs the variables from a table

  $sql = "SELECT * from TABLE WHERE field='SOMETHING'";

  $result = mysql_query($sql);

  $row = mysql_fetch_array($result);

  $var1 = $row['var1'];

  $var2 = $row['var2'];

*/

 

  for ($i=1; $i<=$NumberOfTxtBoxes; $i++)

  {

      $InputFromScreen[$i]=strtoupper($_POST["Txt".$i]);

  }

}

else

{

  for ($i=1; $i<=$NumberOfTxtBoxes; $i++)

  {

      $InputFromScreen[$i]="";

  }

}

?>

<form id="form1" name="form1" method="post" action="Untitled-1.php">

  <table width="200" border="1">

    <tr>

      <td> </td>

      <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td>

    </tr>

    <tr>

      <td> </td>

      <td><input name="Txt33" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td>

    </tr>

    <tr>

      <td> </td>

  <td><select name="var2" size="1" class="WorkPageField" id="var2">

<option value="Yes" <? if($var2 == "Yes"){ echo "selected"; } ?>>Yes</option>

<option value="No" <? if($var2 == "No"){ echo "selected"; } ?>>No</option>

</select></td>

    </tr>

    <tr>

      <td> </td>

      <td><select name="var1" size="1" class="WorkPageField" id="var2">

<option value="Yes" <? if($var1 == "Yes"){ echo "selected"; } ?>>Yes</option>

<option value="No" <? if($var1 == "No"){ echo "selected"; } ?>>No</option>

</select></td>

    </tr>

    <tr>

      <td> </td>

      <td><?PHP

?></td>

    </tr>

    <tr>

      <td> </td>

      <td><input type="submit" name="Submit" value="Submit" /></td>

    </tr>

  </table>

</form>

</body>

</html>

 

Link to comment
Share on other sites

try this...

 

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?PHP
//Transfer Data from Screen to Memory
$NumberOfTxtBoxes=5;
$NumberOfTxtBoxesToBeFilled=5;

if (isset($_POST['Submit']))
{


   $var1 = $_POST['var1'];
   $var2 = $_POST['var2'];


/* this grabs the variables from a table
   $sql = "SELECT * from TABLE WHERE field='SOMETHING'";
   $result = mysql_query($sql);
   $row = mysql_fetch_array($result);
   $var1 = $row['var1'];
   $var2 = $row['var2'];
*/

   for ($i=1; $i<=$NumberOfTxtBoxes; $i++)
   {
      $InputFromScreen[$i]=strtoupper($_POST["Txt".$i]);
   }
}
else
{
   for ($i=1; $i<=$NumberOfTxtBoxes; $i++)
   {
      $InputFromScreen[$i]="";
   }
}
?>
<form id="form1" name="form1" method="post" action="">
  <table width="200" border="1">
    <tr>
      <td> </td>
      <td><input name="Txt1" value="<?PHP echo $InputFromScreen[1]; ?>" type="text" id="Txt1" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt2" value="<?PHP echo $InputFromScreen[2]; ?>" type="text" id="Txt2" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt3" value="<?PHP echo $InputFromScreen[3]; ?>" type="text" id="Txt3" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt4" value="<?PHP echo $InputFromScreen[4]; ?>" type="text" id="Txt4" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input name="Txt5" value="<?PHP echo $InputFromScreen[5]; ?>" type="text" id="Txt33" /></td>
    </tr>
    <tr>
      <td> </td>
   <td><select name="var2" size="1" class="WorkPageField" id="var2">
<option value="Yes" <? if($var2 == "Yes"){ echo "selected"; } ?>>Yes</option>
<option value="No" <? if($var2 == "No"){ echo "selected"; } ?>>No</option>
</select></td>
     </tr>
    <tr>
      <td> </td>
      <td><select name="var1" size="1" class="WorkPageField" id="var2">
<option value="Yes" <? if($var1 == "Yes"){ echo "selected"; } ?>>Yes</option>
<option value="No" <? if($var1 == "No"){ echo "selected"; } ?>>No</option>
</select></td>
    </tr>
    <tr>
      <td> </td>
      <td><?PHP
?></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Submit" /></td>
    </tr>
  </table>
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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