0

I have the following function:

function si_ad_call_models(){
    // title of the page
    echo '<h2>Add new ad call model</h2>';
    ?>
    <form action="" method="post">
        <label for="add">Enter name of ad model: </label>
        <input type="text" name="new_model_name"/>
        <input type="submit" name="submit" value="Add your new ad model"/>
    </form>
    <?php
    if(isset($_POST['submit'])){
         add_option('si_ad_call_model', serialize($_POST['new_model_name']));
    }
    $myopt =  unserialize(get_option('si_ad_call_model'));
}

The variable $myopt outputs the first thing that i enter in the text field. The question is how can i add the values in an array and every time i submit the form it should update the option value? Is that possible?

3
  • is this really a WordPress related Questions?
    – Bainternet
    Commented Aug 19, 2013 at 13:06
  • why do you think this is not? i mean it's playing with the get_option update_option and those are wordpress related.
    – emcee22
    Commented Aug 19, 2013 at 13:49
  • because its a more of a php question and not really a WordPress one!
    – Bainternet
    Commented Aug 20, 2013 at 8:39

1 Answer 1

0

Something like this should work.

function si_ad_call_models() {

    if ( isset( $_POST['submit'] ) ) {

        // Checking that $_POST['new_model_name'] is set is probably not enough validation.
        if ( isset( $_POST['new_model_name'] ) ) {

            // Get the stored models.
            if ( get_option( 'si_ad_call_model' ) )
                $si_ad_call_models = unserialize( get_option( 'si_ad_call_model' ) );

            else
                $si_ad_call_models = array();

            // Add the new model to the end of the models array.
            // @TODO Clean $_POST['new_model_name'] before adding it to database.
            $si_ad_call_models[] = $_POST['new_model_name'];

            // Store the updated array of models.
            if ( update_option( 'si_ad_call_model', serialize( $si_ad_call_models ) ) ) {
                // @TODO Tell user about success.

            } else {
                // @TODO Tell user about failure.
                // @TODO Log database update failure.
            }

        } else {
            // @TODO Add validation error code.
        }
    }

    // Title of the page.
    echo '<h2>Add new ad call model</h2>';
?>
    <form action="" method="post">
        <label for="add">Enter name of ad model: </label>
        <input type="text" name="new_model_name"/>
        <input type="submit" name="submit" value="Add your new ad model"/>
    </form>
<?php
}

I did not fully test this code.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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