How to save custom form data in wp_options table

I have some data that I want stored in the wp_option table.

I know the code is not right but I want to store data in this way:

global $wpdb;

if (isset($_POST['add_estimated_satisfaction'])) {
    // run validation if you're not doing it in js
    $option_name = $confused_about_treatment_editor;
    $option_name = $estimated_price_editor;
    $option_name = $satisfaction_guarantee_editor;

    $option_value = isset($_POST['confused_about_treatment']) ? $_POST['confused_about_treatment'] : '';
    $option_value = isset($_POST['estimated_price']) ? $_POST['estimated_price'] : '';
    $option_value = isset($_POST['satisfaction_guarantee']) ? $_POST['satisfaction_guarantee'] : '';


    $status = $wpdb->insert('www_options', array(
        'option_name'  => $option_name,
        'option_value' => $option_value,

    ));

    if ($status == false) {
        echo $msg_fail;
    } else {
        echo $msg_success_save;
    };
    go_back();
}
?>
<form method="POST">
    <table width="95%">
        <tr>
            <td width="10%">Confused about our treatment</td>
            <td width="90%">
                <?php
                $confused_about_treatment_content  = '';
                $confused_about_treatment_editor   = "confused_about_treatment";
                $confused_about_treatment_settings = array(
                    'textarea_name' => $confused_about_treatment_editor,
                    'textarea_rows' => 15,
                    'media_buttons' => false,
                    'teeny'         => false,
                    'tinymce'       => true,
                    'quicktags'     => false,
                );
                ?>
                <textarea style="display:none" name="<?php $confused_about_treatment_editor ?>" id="<?php $confused_about_treatment_editor ?>" rows="10" cols="100"><?php echo $confused_about_treatment_content ?></textarea>
                <?php wp_editor($confused_about_treatment_content, $confused_about_treatment_editor,
                    $confused_about_treatment_settings); ?>

            </td>
        </tr>

        <tr>
            <td width="10%">Tooltip Estimated Price</td>
            <td width="90%">
                <?php
                $estimated_price_content  = '';
                $estimated_price_editor   = "estimated_price";
                $estimated_price_settings = array(
                    'textarea_name' => $estimated_price_editor,
                    'textarea_rows' => 5,
                    'media_buttons' => false,
                    'teeny'         => false,
                    'tinymce'       => false,
                    'quicktags'     => false,
                );
                ?>
                <textarea style="display:none" name="<?php $estimated_price_editor ?>" id="<?php $estimated_price_editor ?>" rows="10" cols="100"><?php echo $estimated_price_content ?></textarea>
                <?php wp_editor($estimated_price_content, $estimated_price_editor, $estimated_price_settings); ?>

            </td>
        </tr>

        <tr>
            <td width="10%">Tooltip Satisfaction Guarantee</td>
            <td width="90%">
                <?php
                $satisfaction_guarantee_content  = '';
                $satisfaction_guarantee_editor   = "satisfaction_guarantee";
                $satisfaction_guarantee_settings = array(
                    'textarea_name' => $satisfaction_guarantee_editor,
                    'textarea_rows' => 5,
                    'media_buttons' => false,
                    'teeny'         => false,
                    'tinymce'       => false,
                    'quicktags'     => false,
                );
                ?>
                <textarea style="display:none" name="<?php $satisfaction_guarantee_editor ?>" id="<?php $satisfaction_guarantee_editor ?>" rows="10" cols="100"><?php echo $satisfaction_guarantee_content ?></textarea>
                <?php wp_editor($satisfaction_guarantee_content, $satisfaction_guarantee_editor,
                    $satisfaction_guarantee_settings); ?>

            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="add_estimated_satisfaction" value="Submit"/></td>
        </tr>

    </table>
</form>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

@Buttered_Toast’s solutions is what you are looking for, if you were in a position to use it.

There are a bunch of problems with your code.

  1. Variables $confused_about_treatment_editor, $estimated_price_editor, and $satisfaction_guarantee_editor are never instantiated.
  2. The values of $option_name and $option_valueare changed three times, but only the last value is ever used.
  3. There is no closing PHP tag between your code and your form.

I’m not sure if you intend to call go_back() every time, but it is called every time.

I would recommend using update_option() for form values that will be changed during the live of the site. For values that you don’t want mutated you should use add_option().

You could try removing everything between your // run validation comment, and $status variable instantiation with this…

$status = update_option('form_values', [
    'confused_about_treatment' => isset( $_POST['confused_about_treatment'] ) ? $_POST['confused_about_treatment'] : '',
    'estimated_price' => isset( $_POST['estimated_price'] ) ? $_POST['estimated_price'] : '',
    'satisfaction_guarantee' => isset( $_POST['satisfaction_guarantee'] ) ? $_POST['satisfaction_guarantee'] : '',
]);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x