0
votes

I am a newbier wordpress and php. I am wrote a form with select and put into function form() in my widget file

But I don't know how can I get the value of options.

This is my form html:

<label>Chose one:</label>
<select name="test">
<option value="One">Select One</option>
<option value="Two">Select Two</option>
<option value="Three">Select Three</option>
</select> 

I had tried to retrieve value by: $get_var = $POST[test]; and try var_dump it where my widget codes show (function widget() ) but it return null.

Please help me, Thanks a lot!

1

1 Answers

0
votes

Make the form in below format

public function form( $instance ) {
        $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
        ?>
        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label> 
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
        </p>
        <?php 
    }

Update the values to database in the below format

public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

        return $instance;
    }

and then you can fetch the values in front-end in the below way :

public function widget( $args, $instance ) {
        echo $args['before_widget'];
        if ( ! empty( $instance['title'] ) ) {
            echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
        }
        echo esc_html__( 'Hello, World!', 'text_domain' );
        echo $args['after_widget'];
    }

For details read this : https://codex.wordpress.org/Widgets_API