如何确定单击了哪个<SELECT><OPTION</OPTION>?

时间:2020-06-22 作者:GreatDayDan

我正在尝试构建一个具有下拉列表(<;select)、文本输入、文本区域和提交输入的小部件。当小部件功能构建组件时;填充数据库自定义表中的下拉列表。我想做的是,当用户单击下拉列表时,文本输入将从<;选项将用于获取描述,该描述将进入文本区域。任何新的描述都将更新db记录。任何新的事件名称都将添加到表中。

我最终发现,所有的用户控件都是由小部件函数绘制的,所以这就是我的代码所在。正如我所研究的;“如何”;我添加了更多的代码,其中一些我不知道如何使用。就像我在select中添加了onclick一样,但是结果是如何返回的,我如何使用它?Javascript也有同样的问题。

下面是插件代码。擅长这方面的人能帮我工作吗?谢谢Dan\'l公司

<?php

/**
 * Plugin Name:   Mo_Events
 * Plugin URI:    http://memoriesof.website
 * Description:   Allows the user to select/enter an Event
 * Version:       0.12345
 * Author:        Dan Statham
 * Author URI:    http://www.memoriesof.website
 */
 
function doclick($arg) {
    write_log(\'gdd >doclick \' . print_r($arg));

    $ev = null;
    $id = null;
    if (isset($arg)) {
        $ev = $arg[\'event\'];
        write_log(\'gdd <doclick \' . print_r($ev));
        return $ev;
    }
}

class gdd_Events_Widget extends WP_Widget {

    // Set up the widget name and description.
    public function __construct() {
        $widget_options = array(\'classname\' => \'gdd_Events_Widget\', \'description\' => \'Select an Event\');
        parent::__construct(\'gdd_Events_Widget\', \'MO Events\', $widget_options);
    }

    // Create the widget output.
    public function widget($args, $instance) {
        if (isset($instance)) {
            //$title = apply_filters( \'widget_title\', $instance[ \'title\' ] );
            //$blog_title = get_bloginfo( \'name\' );
            //$tagline = get_bloginfo( \'description\' );
        } else {
            $title = \'Just My Size\';
        }
        global $event;
        global $events;
        global $wpdb;

        write_log(\'gdd before_widget\');

        $events = $wpdb->get_results("SELECT id, event FROM mo_events order by event", ARRAY_A);
        write_log(\'gdd Build form\');
        ?>
        <form name=\'f1\' method="POST" action="">
            <fieldset>
                <legend> Select an Event </legend>
                <select class="dropdown" id="eventsdd" name="eventsdropdown" title="Events Dropdown" onclick="doclick($selected)">

                <?php write_log(\'gdd foreach\');
                foreach ($events as $event) {
                    ?>
                    <option value=" <?php echo $event[\'id\'] ?>"><?php echo $event[\'event\'];; ?> </option> 
                    <?php } write_log(\'gdd Build inputs\');
                    ?>
                </select>     
                <p>
                    <label id="label4" for="DESCRIPTION">Description</label>
                    <textarea rows="5" cols="50" form="f1" name="DESCRIPTION" placeholder="Describe the event" id="DESCRIPTION"></textarea>

                <p>
                    <label id= "label2" for="ename">Event</label><br>
                    <input type="text" id="ename" name="EVENT" value = "" placeholder="Select an existing or enter a new event">
                </p>
                </p>
            </fieldset>
            
            <script>
                (function () {
                    var sel;
                    var el;



                    // get references to select list and display text box
                    var sel = document.getElementById(\'eventsdd\');
                    var el = document.getElementById(\'ename\');


                    function getSelectedOption(sel) {

                        var opt;
                        for (var i = 0, len = sel.options.length; i < len; i++) {
                            opt = sel.options[i];
                            if (opt.selected === true) {
                                break;
                            }
                        }
                        return opt;
                    }

                    // assign onclick handlers to the buttons
                    document.getElementById(\'eventsdd\').onclick = function () {
                        el.value = sel.value;
                    }

                }());
        // immediate function to preserve global namespace

                <\\script>   
                    </form>
                <?php
                echo $args[\'after_widget\'];
                write_log(\'gdd after_widget\');
            }

            // Create the admin area widget settings form.
            public function form($instance) {
                write_log(\'gdd begin form\');

                global $event;
                global $events;
                global $wpdb;
                $title = !empty($instance[\'title\']) ? $instance[\'title\'] : \'\';
                ?>
                < /p>
                <?php
                write_log(\'gdd end form\');
            }

            // Apply settings to the widget instance.
            public function update($new_instance, $old_instance) {
                $instance = $old_instance;
                $instance[\'title\'] = strip_tags($new_instance[\'title\']);
                $instance[\'page\'] = strip_tags($new_instance[\'page\']);
                $instance[\'post\'] = strip_tags($new_instance[\'post\']);
                $instance[\'dropdown\'] = strip_tags($new_instance[\'dropdown\']);
                return $instance;
                write_log(\'<gdd update\');
            }

        }

// Register the widget.
        function gdd_register_event_widget() {
            register_widget(\'gdd_Events_Widget\');
        }

        add_action(\'widgets_init\', \'gdd_register_event_widget\');
        ?>

1 个回复
SO网友:GreatDayDan

这就是我想出的行之有效的方法。在类的函数小部件中:

public function widget($args, $instance) {
        if (isset($instance)) {
            //$title = apply_filters( \'widget_title\', $instance[ \'title\' ] );
            //$blog_title = get_bloginfo( \'name\' );
            //$tagline = get_bloginfo( \'description\' );
        } else {
            $title = \'Just My Size\';
        }
        global $selectedevent;
        global $event;
        global $events;
        global $wpdb;
       
        global $event2;
        
        write_log(\'gdd before_widget\');

        $events = $wpdb->get_results("SELECT id, event FROM mo_events order by event", ARRAY_A);
        write_log(\'gdd Build form\');
        ?>
        <form name=\'f1\' method="POST" action="process_post.php">
            <fieldset>
                <legend> Select an Event </legend>
                <select class="dropdown" id="eventsdd" name="eventsdropdown" title="Events Dropdown"  onclick="doclick(this)" >
                <?php write_log(\'gdd foreach\');?>
                <?php foreach ($events as $event) { 
                   ?> <option value=" <?php echo $event[\'id\'] ?>"><?php echo $event[\'event\'];  ?> </option>
                    
                  <?php } ?>                      
                </select>                

                <p>
                    
                   <input type="text" id="hideme">
                    <input type="text" id="ename" name="EVENT" value = "" placeholder="Select an existing or enter a new event">
                </p>
                <p>
                    <label id="label4" for="DESCRIPTION">Description</label>
                    <textarea rows="5" cols="50" form="f1" name="DESCRIPTION" placeholder="Describe the event" id="DESCRIPTION"></textarea>
                </p>
                
                <input type=\'submit\' id=\'submit_id\' name=\'submit_id\'</input>
            </fieldset>
            
                <?php
                echo $args[\'after_widget\'];
                write_log(\'gdd after_widget\');
            
    }
以及javascript代码:


<script>
    
 function isset(_var){
     return !!_var; // converting to boolean.
}   
    var eventId;
    var xmlhttp;
    var xmlhttp = new XMLHttpRequest;
    
    function doclick(ddl){
        $evtid=-1;      
        $evtid =ddl.options[ddl.selectedIndex].value;
              document.getElementById(\'hideme\').value = 
              ddl.options[ddl.selectedIndex].value;
              document.getElementById(\'ename\').value = 
              ddl.options[ddl.selectedIndex].text;
              xmlhttp = new XMLHttpRequest();
              xmlhttp.onreadystatechange = callback;
              xmlhttp.open("POST", "http://www.memoriesof.website/id.php", true);
              xmlhttp.send();       
        
        }
          
    function callback(){
    
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById(\'DESCRIPTION\').innerHTML = xmlhttp.responseText;
        }
    }
</script>