Jump to content

Setting a default value to a combo box?


shure2

Recommended Posts

Hi,

 

I have scoured the net and can't find an answer so I wondered if you guys can help.

 

I have a combo box that selects data from a database, I can then add the selection of the combo box and other fields to another table, however when I do this with php the page refreshes and the value of the combo box changes back to the default order (where I want it to show the option I just added to the database)

 

I have tried the below code, theoretically I thought it might work but it doesn't (the leadProductCategory is the combo box selection that I add to the database - it is set in the php file where I add to the database).

 

$selectLeadProductCatQuery = "SELECT * FROM category where userid = '".$_SESSION['userid']."' ORDER BY .'".$_SESSION['leadProductCategory']."';
		$selectLeadProductCatResult = mysql_query($selectLeadProductCatQuery) or die(mysql_error());

		while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult))
                        { 
                             echo '<option value="'.$row1LeadProductCat['catname'].'">'.$row1LeadProductCat['catname'].'</option>'; 

           	        }

 

Link to comment
Share on other sites

I solved it by putting the value into a session variable when its added to the database and then used the following code:

 

        <select name="leadprodcategory">
	<?php
		$selectLeadProductCatQuery = "SELECT * FROM category where userid = '".$_SESSION['userid']."' ORDER BY catname";
		$selectLeadProductCatResult = mysql_query($selectLeadProductCatQuery) or die(mysql_error());

                        while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult)) {

     echo '<option value="' . $row1LeadProductCat['catname'].'"';
     if( $row1LeadProductCat['catname'] == $_SESSION['leadProductCategory'] ) { //change 'select_name' to the value of your <select name="
          echo ' selected="selected"';
     }
     echo '>' . $row1LeadProductCat['catname'] . '</option>';
}


	?>
	</select>

 

hope this is useful for someone else!

Link to comment
Share on other sites

This has been asked and answered many times on this forum. Here's a modification of your code that is cleaned up and more secure

<select name="leadprodcategory">
<?php

    $userID = mysql_real_escape_string($_SESSION['userid']);
    $query = "SELECT catname FROM category where userid = '{$userID}' ORDER BY catname";
    $result = mysql_query($selectLeadProductCatQuery) or die(mysql_error());

    while($record = mysql_fetch_assoc($result))
    {
        $selected = ($record['catname'] == $_SESSION['leadProductCategory']) ? ' selected="selected"': '';
        echo "<option value=\"{$record['catname']}\"{$selectred}>{$record['catname']}</option>\n";
    }
?>
</select>

Link to comment
Share on other sites

This has been asked and answered many times on this forum. Here's a modification of your code that is cleaned up and more secure

<select name="leadprodcategory">
<?php

    $userID = mysql_real_escape_string($_SESSION['userid']);
    $query = "SELECT catname FROM category where userid = '{$userID}' ORDER BY catname";
    $result = mysql_query($selectLeadProductCatQuery) or die(mysql_error());

    while($record = mysql_fetch_assoc($result))
    {
        $selected = ($record['catname'] == $_SESSION['leadProductCategory']) ? ' selected="selected"': '';
        echo "<option value=\"{$record['catname']}\"{$selectred}>{$record['catname']}</option>\n";
    }
?>
</select>

 

thank you for the advice and better technique, I modified my code and it works great, however I have a combo box which is slightly different and I'm struggling to do what I have with you code above to it.

 

 

<select name="leadid" onchange="showLeadName()">
	<?php
		$userID = mysql_real_escape_string($_SESSION['userid']);
                        $query = "SELECT * FROM lead where userid = '{$userID}' ORDER BY leadname";
		$result = mysql_query($selectLeadQuery) or die(mysql_error());

		while($record=mysql_fetch_array($result))
                        { ?>
                           <?php echo '<option value="'.$record['leadid'].'">'.$record['leadname'].'</option>'; ?>

           	<?php   }
	?>
    </select>

 

Link to comment
Share on other sites

You are defining the string "$query", but then trying to run a query against the string "$selectLeadQuery" - which, presumably, does not exist.

 

<select name="leadid" onchange="showLeadName()">
<?php
    $userID = mysql_real_escape_string($_SESSION['userid']);
    $query = "SELECT `leadid`, `leadname`
              FROM `lead`
              WHERE userid = '{$userID}'
              ORDER BY `leadname`";
    $result = mysql_query($query) or die(mysql_error());
    while($record=mysql_fetch_array($result))
    {
        echo "<option value=\"{$record['leadid']}\">{$record['leadname']}</option>\n";
    }
?>
</select>

Link to comment
Share on other sites

You are defining the string "$query", but then trying to run a query against the string "$selectLeadQuery" - which, presumably, does not exist.

 

<select name="leadid" onchange="showLeadName()">
<?php
    $userID = mysql_real_escape_string($_SESSION['userid']);
    $query = "SELECT `leadid`, `leadname`
              FROM `lead`
              WHERE userid = '{$userID}'
              ORDER BY `leadname`";
    $result = mysql_query($query) or die(mysql_error());
    while($record=mysql_fetch_array($result))
    {
        echo "<option value=\"{$record['leadid']}\">{$record['leadname']}</option>\n";
    }
?>
</select>

 

yes you are right, i started changing the code to reflect your advice but missed that variable, could you help me change the code like the other list box so that the session['leadname'] appears in the listbox as a default.

Link to comment
Share on other sites

while($record=mysql_fetch_array($result))
{
    $selected = ($record['leadname']==$_SESSION['leadname']) ? ' selected="selected"' : '';
    echo "<option value=\"{$record['leadid']}\"{$selected}>{$record['leadname']}</option>\n";
}

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.