dongsaolian8786 2019-03-15 00:20
浏览 86

在textarea中显示用户输入,并在数据库PHP中格式化为数组

I've created a custom options page for my WordPress theme and have a textarea for users to add options that will be appended to an array in another file. User input is successfully added to the appropriate location in the database, but it is being stored as a single string. I would like for the user to be able to enter one option on each line and have the inputs saved as an array with each line being saved as an element in the array.

for example:

Textarea user input:

'my option1'
'my option2'
'my option3'

is stored as:

a:1:{s:24:"my_textarea_field_0";s:34:"my option1
my option2
my option3";}

I would like for it to be stored as an array: {"my option1", "my option2", "my option3"}

Also, the text entered into the textarea is saved to the database but is not displayed in the textarea after the submit button is pressed. I would like for the user's input the be added to the database as an array and continue to be displayed in the textarea after it has been submitted.

Here is the relevant code:

function my_textarea_field_0_render() { 
        $this->$options = get_option('my_settings');
        ?>
        <p style="margin: .25em 0 .5em!important; display: inline-block;">
            <label for="my_settings[my_textarea_field_0]">
            Lorem ipsum dolor sit amet.</label>
        </p>
        <p>
        <textarea cols='145' rows='8' name='my_settings[my_textarea_field_0]'><?php echo $options['my_textarea_field_0'];?></textarea>
        </p>
        <?php
        add_option('my_settings', '$options');
}

I've spent a while searching for solutions and tinkering with the code, but I haven't been able to figure it out.

  • 写回答

1条回答 默认 最新

  • duan0504 2019-03-15 00:39
    关注
    1. To make array from value of textarea, use explode function. Maybe this sample code is useful for you.
    $arrTextarea = explode("
    ", str_replace("", "", $valTextarea));
    
    1. You should use only php code in function.
    function my_textarea_field_0_render() { 
        $this->$options = get_option('my_settings');    
        $html = `
            <p style="margin: .25em 0 .5em!important; display: inline-block;">
                <label for="my_settings[my_textarea_field_0]">
                Lorem ipsum dolor sit amet.</label>
            </p>
            <p>
                <textarea cols='145' rows='8' name='my_settings[my_textarea_field_0]'>` . 
                    $options['my_textarea_field_0'] . 
                `</textarea>
            </p>`;
        add_option('my_settings', '$options'); // I didn't understand what is $options.
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试