Page 1 of 1

Please Help: form list box....

Posted: Wed Dec 17, 2008 8:53 pm
by avon267
Please Help: I want to update the list box form value!!!!!!!PLEASE HELP ME IN STEP 3

step 1:values displayed in form
<select name="optionlist">
<option value="Option 1" selected>Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>


step 2:Saving value in db.
For example user selected option 2 and it get saved as " optionlist" = "option 2"

step 3: editing value
Please help.
How can I make "option 2" value as selected when I open form to update saved value "option 2", latter.

Re: Please Help: form list box....

Posted: Wed Dec 17, 2008 11:12 pm
by pcoder
Try to write some code to achieve your result. It's soo easy and you can do it.
Cheers

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 1:39 am
by syth04
avon267 wrote:Please Help: I want to update the list box form value!!!!!!!PLEASE HELP ME IN STEP 3

step 1:values displayed in form
<select name="optionlist">
<option value="Option 1" selected>Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>


step 2:Saving value in db.
For example user selected option 2 and it get saved as " optionlist" = "option 2"

step 3: editing value
Please help.
How can I make "option 2" value as selected when I open form to update saved value "option 2", latter.

I have created this just for you. You can see where to change values and what not.
There are 2 files, I can provide the sql for the db if you would like it. But to make it simple, you have a db called 'some_db', with table called 'options_table' with one field called 'id' , I hope this is helpful. This is my first help post, since learning php.

filename : db_option.php

Code: Select all

<?php # Setting up DB Connection
 
    #   Create a db connection for queries 
$dbconnect  = mysql_connect('localhost','someuser','somepassword');
    # Error Catching
            if (!$dbconnect) {
                die("Database connection failed: " . mysql_error());
            }
 
    #   Select DB to use
$dbselect = mysql_select_db('some_db',$dbconnect);
    #   Error Catching
            if (!$dbselect) {
                die("Database selection failed: " . mysql_error());
            }
?>
 
<?php # Gather session data
$option_update = trim($_POST['optionlist']);
$old_option = trim($_POST['oldoption']);
?>
 
<?php # Query to select the option
$query_select = "SELECT *" ; # select your colums that you need
$query_select .= "FROM options_table" ; 
$query_select .= "WHERE id = " . $old_option ;  # checks to see if option is avaiable
?>
 
<?php # Query to update $old_option  to $option_update 
    $query_update = "UPDATE options_table SET "; # Selects the table
    $query_update .="id = '{$option_update}' "; # Updates new option
    $query_update .="WHERE id = ". $old_option . " ;" ; # finds old option
    $result_set = mysql_query($query_update,$dbconnect);
    # Error Catching 
    if (!$result_set) {
            die("Database query failed: " . mysql_error());
        } else { $message =  " Update Successful ";
    
 
/*              while ($result = mysql_fetch_array($result_set)) {
                            if (!isset($message)){
                            $message =  " Update Successful "; 
                            } else { $message = " Multiple Updates was applied "  ;
                            }
                        }
 */     }
    
?>
<!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>db_option.php</title>
</head>
 
<body>
<?php echo $query_update . $message ; ?>
</body>
<?php #Close MY SQL Connection 
    mysql_close($dbconnect);
?>
</html>
 
filename options.php

Code: Select all

<?php # Create php session
if (!Session_id())  {
  session_start();
} ?>
<!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>Option List</title>
</head>
 
<body>
<form action="db_option.php" method="post" name="options" target="_self">
<select name="optionlist">
<option value="1" selected>Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
<input name="oldoption" type="text" value="1" />
<input name="Update" type="submit" value="submit" id="submit" />
</form>
</body>
</html>
 

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 1:37 pm
by avon267
syth04 wrote:
I have created this just for you. You can see where to change values and what not.
There are 2 files, I can provide the sql for the db if you would like it. But to make it simple, you have a db called 'some_db', with table called 'options_table' with one field called 'id' , I hope this is helpful. This is my first help post, since learning php.

 

Thanks syth04,
But, what I am trying to do is this:
Step 1: I have form with list box (on form submission values get stored in db)
Step 2: (I Need Help)Form open to update values, (I want)same value selected in the list box.
But how??? can user see same value in edit form he selected before.

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 1:57 pm
by syth04
avon267 wrote:
syth04 wrote:
I have created this just for you. You can see where to change values and what not.
There are 2 files, I can provide the sql for the db if you would like it. But to make it simple, you have a db called 'some_db', with table called 'options_table' with one field called 'id' , I hope this is helpful. This is my first help post, since learning php.

 

Thanks syth04,
But, what I am trying to do is this:
Step 1: I have form with list box (on form submission values get stored in db)
Step 2: (I Need Help)Form open to update values, (I want)same value selected in the list box.
But how??? can user see same value in edit form he selected before.
you would have to right a query to read the option value to the value text box. I did not right the query. You would add a if statement to the list box option if $option = db_current_option then echo "selected >" else " > ". I hope this helps.

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 2:03 pm
by avon267
pcoder wrote:Try to write some code to achieve your result. It's soo easy and you can do it.
Cheers
<form action="add.php">
Select Option:
<select name="optionlist">
<option value="Option 1" selected>Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option></select>
<input type="submit" name="Add to Db" />
</form>


<?
//lets say in Step 1 user selected "option 2"
//I retrive previously selected value of "optionlist" using recordset
//$myoption = $recordset['optionlist'];

echo "Previous value of optionlist is: $myoption";//it will display ---"Option 2"
echo "Use Form to update your value";
//PLEASE SUGGEST HOW CAN I MAKE "Option 2" PRE-SELECTED
//I think I can use comparison operator but to me it looks like value are static in form and not dynamic from db
//Other way I can think of is getting all list values using db, but it doesnot seems practile to do that form just 1 list box. I dont know..How should I follow...
?>



<form action="update.php">
ReSelect Option:
<select name="optionlist">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option></select>
<input type="submit" name="Add to Db" />
</form>

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 2:12 pm
by avon267
syth04 wrote:
avon267 wrote:
syth04 wrote: you would have to right a query to read the option value to the value text box. I did not right the query. You would add a if statement to the list box option if $option = db_current_option then echo "selected >" else " > ". I hope this helps.
//lets say I have a "Option 2" value stored in db and I retrieve it using recordset from db
//$myoption = $recordset['optionlist'];
//NOW MY VALUE IS IN $myoption variable...


//HOW DO YOU SUGGEST QUERY SHOULD GO FROM HERE TOMAKE SURE "Option 2" IS SELECTED OR OTHERWISE....I dont understand this part..please suggest


<form action="update.php">
ReSelect Option:
<select name="optionlist">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option></select>
<input type="submit" name="Update to Db" />
</form>

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 2:26 pm
by syth04
example of how it may be done, though its NOT complete. Due to not knowing your db configurations

Code: Select all

 
<body>
 
<form method="add.php">
Select Option:
<select name="optionlist">
    <option value="Option 1" selected>Option 1</option>
    <option value="Option 2">Option 2</option>
    <option value="Option 3">Option 3</option>
    <option value="Option 4">Option 4</option></select>
<input type="submit" name="Add to Db" />
</form>
 
 
<?
//lets say in Step 1 user selected "option 2"
//I retrive previously selected value of "optionlist" using recordset
//$myoption = $recordset['optionlist'];
 
echo "Previous value of optionlist is: $myoption";//it will display ---"Option 2"
echo "Use Form to update your value";
//PLEASE SUGGEST HOW CAN I MAKE "Option 2" PRE-SELECTED
//I think I can use comparison operator but to me it looks like value are static in form and not dynamic from db
//Other way I can think of is getting all list values using db, but it doesnot seems practile to do that form just 1 list box. I dont know..How should I follow...
?>
 
<?php 
$query = " SELECT * ";
$query .= "FROM table ";
$recordset = mysql_query($query,$dbconnection);
$message = '';
while ($record = mysql_fetch_array($recordset)){
        if (selected_option == $record_option) {{$message .= "<option value =\"" . $record[option] . "\" selected=\"selected\">" . $record[option] . "</option>" ;
        } else {$message .= "<option value =\"" . $record[option] . "\">" . $record[option] . "</option>";
        }
        
?>
 
<form method="update.php">
ReSelect Option:
<select name="optionlist"> 
<?php
echo $message;
?>
    
</select>
<input type="submit" name="Add to Db" />
</form>
 

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 2:45 pm
by avon267
syth04 wrote:example of how it may be done, though its NOT complete. Due to not knowing your db configurations
 
Thanks syth04,
You are suggesting me to get the entire option list from db and getting it all done before user get to see the list box again,
Problem is I donot have "all" list box values in db, its a static, "hard coded" form and not db driven,
There has to be a better way to achieve it, like a using "ereg" or "trip" or something similar function checking each value using loop, to achieve the same results as you pointed. I am not sure though...

RESOLVED

Posted: Thu Dec 18, 2008 4:24 pm
by avon267
One way to do is using db, as stated above. Thanks GUYS..
Secondly if we dont want to use db use array as I did in "example below". It's fairly simple.
Third is using eregi()functions with trim() and lot more I guess. I haven't finished it but I am sure it is possible.




FORM 1
<?
$category = Array("1","2","3","4","5"); ?>


<form method="post" action="submit">

<select name="carcategory">
<? foreach($category As $categoryValue)
{
echo "<option value=\"$categoryValue\">$categoryValue</option>"; } ?>
</select>

<input type="submit" /></form>

FORM 2
<?
$category = Array("1","2","3","4","5"); ?>


<?
$mycategory = $_POST["carcategory"];

echo $mycategory; ?>


<form method="post" action="#">

<select name="carcategory">
<? foreach($category As $categoryValue)
{
if($categoryValue == $mycategory)
echo "<option value=\"$categoryValue\""." "."selected>$categoryValue</option>";
else
echo "<option value=\"$categoryValue\">$categoryValue</option>"; } ?>
</select>

<input type="submit" /></form>

Re: Please Help: form list box....

Posted: Thu Dec 18, 2008 5:59 pm
by Stryks
Just as an aside ....
avon267 wrote:echo "<option value=\"$categoryValue\""." "."selected>$categoryValue</option>";
... will work in some browsers, but not all. You'd be better off with ...

Code: Select all

echo "<option value=\"$categoryValue\""." "."selected=\"selected\">$categoryValue</option>";