-1

How can I update_option in WordPress from this: everything I do seems not to work; I must be missing something simple.

$l1teams = array(
    1=>"Caterham-Renault",
    2=>"Ferrari",
    3=>"Force India-Mercedes",
    4=>"HRT-Cosworth",
    5=>"Lotus-Renault",
    6=>"Marussia-Cosworth",
    7=>"Mercedes GP",
    8=>"Mclaren-Mercedes",
    9=>"Red Bull-Renault",
    10=>"Sauber",
    11=>"Toro Rosso-Ferrari",
    12=>"Williams-Renault",
    );

<td><select name="league1_driver1_team"><?php foreach($l1teams as $team){?> <option value="<?php echo $l1d1t ?>"><?php echo $team ?></option> <?php } ?> </select></td>

I have all the other code setup correctly; I just cannot insert this value into the database in WordPress and echo it back out.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1263909
  • 155
  • 9

1 Answers1

0

You are trying to echo $l1d1t in your option tag which seems to be undefined in your example. You probably want something want to change your code to something like this:

<td><select name="league1_driver1_team"><?php foreach($l1teams as $team_id => $team_name){?> <option value="<?php echo $team_id; ?>"><?php echo $team_name; ?></option> <?php } ?> </select></td>

Essentially what I am doing is setting the array index to $team_id and the array value to $team_name.

danielrsmith
  • 4,050
  • 3
  • 26
  • 32
  • Thanks for the reply, i did have something like that setup first using $key & $value. Ive changed the code to your example which is essentially the same as i had before i posted. My question is how can i update the options on the database to hold the value selected in the option when saved. My update_option code and variable are: ' $l1d1t = $_POST['league1_driver1_team']; update_option('league1_driver1_team', $l1d1t); $l1d1t = get_option('league1_driver1_team'); ' – user1263909 Apr 30 '12 at 17:51
  • That code should work. However take a look at the example in the Codex about `update_option`. You need to make sure that the option you are trying to update exists already, if not you need to use `add_option`. http://codex.wordpress.org/Function_Reference/update_option – danielrsmith Apr 30 '12 at 17:57
  • Like I said, you need to add some debug around the update_option() call and see what it is returning. It returns true on a successful update, false on a failure. – danielrsmith Apr 30 '12 at 18:11
  • what i did was: if ($variable == $key){echo "selected";} It was infact updating the database, i just was not calling the saved value to the selected field on the – user1263909 May 02 '12 at 00:55