Forum Moderators: coopster

Message Too Old, No Replies

Drop down box - keep last submited

many tries, no success!

         

henry0

11:31 am on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is from the select/update script, for posting purpose I only left 2 choices:
How it works:
Pre-loaded by default in the DB are the options: beginner and intermediate
when selected it feeds the choice to a col named "cook_rating_post"
as is it works ok but I have been pulling my hairs for MANY hours trying to feed back into the select box the "selected option"
Because regardless of the selection it of course defaults again to the first choice and if the user submits other changes that one will be also changed back to the first selection available out of the drop box.

I tried many means to feed back the info into the select fields
and "rebuilt" the drop down box to display the selected option first
like if $cook_rating_post == $intermediate echo intermediate as first option.
But it either breaks the drop down box or even has no effect
I am clue less, what can be done?
I have seen here many posts with the same object
If this can work I will post the whole thing with comments for everyone benefits.

<<< // select/update
<?
print query_select("beginner, intermediate
from users where username = '$username' and id = '$new_content' LIMIT 1");
?>
>>>

the fist script calls the following function()
<<<
function query_select()
{ $conn = db_connect();

$sql= "select beginner, intermediate from users LIMIT 1";
$result = mysql_query($sql,$conn);
echo "<select name=\"cook_rating_post\">";
while(list($beginner, $intermediate)=mysql_fetch_array($result)){
$cook_rating_post = stripslashes($cook_rating_post);

echo "<option value=$beginner>$beginner </option><br>";
echo "<option value=$intermediate>$intermediate </option><br>";

}
}

?>

hakre

12:55 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi henry0,

for the drop down box, you need to use the selected attribute on the toption tag:


<select name="dropdown">
<option>first option</option>
<option selected="selected">pre-selected option</option>
</select>

i guess that's what you're missing.

henry0

5:11 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi hakre, thanks.

I need to query the DB get the value from cook_rating_post compare with every other options and then dynamically rebuilt the options upon what is selected.
Therefore the selected one will top the option range

I tried your suggestion but either I got a blank or I got duplicate option
I cannot figure how to get that value
Even tried to get it by a secondary query as follow
<<<
function query_select()
{ $conn = db_connect();
$sql= "select cook_rating_post from users LIMIT 1";
$result = mysql_query($sql,$conn);
while ($qry = mysql_fetch_array($result)) {
$selected=$qry[cook_rating_post];
}
$sql= "select beginner, intermediate from users LIMIT 1";
$result = mysql_query($sql,$conn);
echo "<select name=\"cook_rating_post\">";
echo "<option selected=$selected>$selected</option><br>";
while(list($beginner, $intermediate)=mysql_fetch_array($result)){
$cook_rating_post = stripslashes($cook_rating_post);


echo "<option value=$beginner>$beginner </option><br>";
echo "<option value=$intermediate>$intermediate </option><br>";

}
}

<Edit> del useless script</edit>

mogenshoj

8:28 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



If you get the value $cook_rating_post back as the same as $beginner or $intermediate or whatever is in there, you can do this.

if ($cook_rating_post == $beginner) {$selected = " selected";} else {$selected = "";}
echo "<option value=\"$beginner\"$selected>$beginner </option>";

if ($cook_rating_post == $intermediate) {$selected = " selected";} else {$selected = "";}
echo "<option value=\"$intermediate\"$selected>$intermediate </option>";

Also remember that values you need inside a function, that are not set within the function needs to be set as function($need_this_variable) or as global.
Dont know if this is the case though.

coopster

8:30 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Something like this, henry0?

Populate a Select Tag from MySQL with options [webmasterworld.com]

henry0

8:46 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Coopster and mogenshoj - Thank you -

I made it working and am preparing to post the whole thing with comments so that it can be used again.
I checked many posts and quest similar to mine comes often.
My version uses a class which allows for loading many selects on one form in one shot and the function can be extended if needed.
I did not use my previous idea to work around cases or if and ==
I used the select option as suggested by hackre.

Will be back soon :)

henry0

9:39 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My drop down box contrib:

Drop down box scripts.
Relies on DB and functions.
Using the function allows to add more drop down boxes if needed
by transforming the () in a class and allowing to extend the class if needed (only optional).

The form: As is offers to select a qualification to receive a tutorial based on the chosen option
The form relies on the three scripts served by: "include_fns.php"

Here is the include_fns:
<<<
<?
include_once("db_fns.php"); //the DB conn script- and two complementary functions
include_once("user_auth_fns.php"); //an authentication function (you may use your own)
include_once("select_fns.php"); //the drop down box function
?>
>>>

<<<
Here is the db_fns_function:
<?
function db_connect()
{
$result = @mysql_pconnect("HOST", "USER_NAME", "PWPWPW");
if (!isset($result) && empty($result))
{echo "can't connect!"; }
if (!@mysql_select_db("DB_NAME"))
return false;
return $result;
}

function get_writer_record($username)
{
$conn = db_connect();
$sql = "select * from A TABLE where username = '$username'";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}

$id=$_POST['id'];
$new_content=$id;

function get_new_content_record($new_content)
{
$conn = db_connect();

$sql = "select * from A TABLE where id = '$new_content'";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));

}

?>
>>>

Here is the select_fns function
<<<
<?php
function query_select()
{ $conn = db_connect();

// it first looks for the default DB preloaded options to build the drop box
// then it looks for the cook_rating_post value that is the result of the selection loaded in a col named cook_rating_post

$sql= "select beginner, intermediate, experienced, very_good, chef from A TABLE LIMIT 1";
$result = mysql_query($sql,$conn);
echo "<select name=\"cook_rating_post\">";
while(list($beginner, $intermediate, $experienced, $very_good, $chef)=mysql_fetch_array($result)){
$cook_rating_post = stripslashes($cook_rating_post);
$conn = db_connect();

$username=$_SESSION['username'];
$sql= "select cook_rating_post from A TABLE where username = '$username' ";
$result = mysql_query($sql,$conn);
while ($qry = mysql_fetch_array($result)) {
$cook_rating_post=$qry[cook_rating_post];
}

echo "<option selected=$cook_rating_post>$cook_rating_post</option><br>";
echo "<option value=$beginner>$beginner </option><br>";
echo "<option value=$intermediate>$intermediate </option><br>";
echo "<option value=$experienced>$experienced </option><br>";
echo "<option value=$very_good>$very_good </option><br>";
echo "<option value=$chef>$chef</option>";

}
}
?>

>>>

AND FINALLY THE FORM
<<<
session_start();
//error_reporting (E_ALL);
include "include_fns.php";
// includes the function: select_fns.php
//
$conn = db_connect();

$username=$_SESSION['username'];

$s = get_new_content_record($new_content);
?>

<tr>
<td align="left" valign="top">
<div class="form_subject"><b>Cooking Knowledge<br>
Which cook are you:</b><br>
<b>Select </b> </div>

<?
print query_select("beginner, intermediate, experienced, very_good, chef
from users where username = '$username' and id = '$new_content' LIMIT 1");
?>
</td></tr>
<td align="left" valign="top">
// to be on the safe side I echo again the chosen option
<b> You Selected --></b><!-- ## show selected value ## --><input type="text" size="10" name="" value="<? echo $s[cook_rating_post];?>">
//if you wonder about $s
//go back to the top of the form and look for:
// $s = get_new_content_record($new_content);
// then look for db_fns.php and review its last function
<br></td></tr>
<input type=SUBMIT VALUE="submit">
</form>

jatar_k

10:13 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



thanks henry0