0

How do I use update_option to give me a new option name each time a form is submitted? The code I'm using now overwrites the database value. Instead I need to have a new address saved each time.

The code:

                    // assign addrss to var
                    $address = esc_html( $_POST['address'] );

                    //assign JSON object to var using geocodeing function('address') input 
                    $address_results = boj_gmap_geocode($address);

                    // transfer data to options array
                    $options['py_name'] = $py_username;
                    $options['address'] = $address;
                    $options['coords']  = $address_results;

                    // create and name the option_name and set fields with $options array
                    update_option( 'py_menu_item', $options );  
1

2 Answers 2

0

To update a new option name each time you need something unique to be used as option name so may be using 'time' in the option name

$now = new DateTime();
$mytime = $now->format('Y-m-d H:i:s');    // MySQL datetime format
// $now->getTimestamp();           // Unix Timestamp

$option['prefix-'.$mytime] = 'your value';

Then you can update as you wish.

To recall it will be a bit hard code. You have to get all value that start with prefix and manual parse the date time to get the newest one. This method is easy to create but a bit more step when using.

Hope it help

-2

submit data as array, and add to array before submit

$all_data = get_option('all_data', array());

$all_data[] = $new_data;

update_option( 'all_data', $all_data );
4
  • A better explanation of the code would be helpful.
    – s_ha_dum
    Commented Dec 6, 2013 at 21:20
  • And how is this answer different from the one @Milo gave to your other question?
    – s_ha_dum
    Commented Dec 8, 2013 at 14:38
  • this is the same question, if you compare the code you can see that it is identical
    – user564285
    Commented Dec 22, 2013 at 20:07
  • And that means you can copy Milo's answer and paste it here without so much as a hat-tip to Milo? That was the point.
    – s_ha_dum
    Commented Dec 22, 2013 at 20:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.