dso407787736 2019-01-25 10:29
浏览 85

(填写联系人表单7中的下拉列表获取此错误 - 警告:array_keys()期望参数1为数组,null为

Ok where to start, I will try and explain as much as I can.

I am using wordpress with contact form 7 and I am trying to populate 3 dropdown items on the contact form, I found some code that I was able to use with no problem but the problem with this was that it was getting the information from a excel file, the file is now to big and will not run on my website anymore so I would like to get the information from my database now.

I have made a table in my database "vehicle_information" with 3 columns "vehicle_type", "vehicle_make", vehicle_model"

I have code in my functions.php and code in my footer to be able to use the cf7 shortcodes.

Code from funtions.php

function ajax_cf7_populate_values() {

//MySQLi information

$db_host     = '***';
$db_username = '***';
$db_password = '***';

$vehicles_makes_models = array();


//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die('Error ' . mysqli_error());

//select MySQLi dabatase table
$vehicles_makes_models = mysqli_select_db($connection, 'vehicle_information') or die('Error ' . mysqli_error());

$sql = mysqli_query($connection, 'SELECT * FROM vehicle_type');

while($row = mysqli_fetch_array($sql)) {
$vehicles_makes_models[$row[0]][$row[1]][] = $row[2];   }


}


    // setup the initial array that will be returned to the the client side script as a JSON object.

$return_array = array(
        'vehicles' => array_keys($vehicles_makes_models),
        'makes' => array(),
        'models' => array(),
        'current_vehicle' => false,
        'current_make' => false
    );

    // collect the posted values from the submitted form

$vehicle = key_exists('vehicle', $_POST) ? $_POST['vehicle'] : false;
$make = key_exists('make', $_POST) ? $_POST['make'] : false;
$model = key_exists('model', $_POST) ? $_POST['model'] : false;

    // populate the $return_array with the necessary values

if ($vehicle) {
    $return_array['current_vehicle'] = $vehicle;
    $return_array['makes'] = array_keys($vehicles_makes_models[$vehicle]);
    if ($make) {
    $return_array['current_make'] = $make;
    $return_array['models'] = $vehicles_makes_models[$vehicle][$make];
    if ($model) {
            $return_array['current_model'] = $model;
        }
        }
    }

    // encode the $return_array as a JSON object and echo it

    echo json_encode($return_array);
    wp_die();



 // These action hooks are needed to tell WordPress that the cf7_populate_values() function needs to be called
 // if a script is POSTing the action : 'cf7_populate_values'

 add_action( 'wp_ajax_cf7_populate_values', 'ajax_cf7_populate_values' );
 add_action( 'wp_ajax_nopriv_cf7_populate_values', 'ajax_cf7_populate_values' );

Code from my footer

<script>
(function($) {

    // create references to the 3 dropdown fields for later use.

    var $vehicles_dd = $('[name="vehicles"]');
    var $makes_dd = $('[name="makes"]');
    var $models_dd = $('[name="models"]');


    // run the populate_fields function, and additionally run it every time a value changes

    populate_fields();
    $('select').change(function() {
        populate_fields();
    });

    function populate_fields() {

        var data = {

            // action needs to match the action hook part after wp_ajax_nopriv_ and wp_ajax_ in the server side script.

            'action' : 'cf7_populate_values', 

            // pass all the currently selected values to the server side script.

            'vehicle' : $vehicles_dd.val(),
            'make' : $makes_dd.val(),
            'model' : $models_dd.val()
        };

        // call the server side script, and on completion, update all dropdown lists with the received values.

        $.post('<?php echo admin_url( 'admin-ajax.php' ) ?>', data, function(response) {
            all_values = response;

            $vehicles_dd.html('').append($('<option>').text(' -- choose vehicle -- '));
            $makes_dd.html('').append($('<option>').text(' -- choose make  -- '));
            $models_dd.html('').append($('<option>').text(' -- choose model -- '));

            $.each(all_values.vehicles, function() {
                $option = $("<option>").text(this).val(this);
                if (all_values.current_vehicle == this) {
                    $option.attr('selected','selected');
                }
                $vehicles_dd.append($option);
            });
            $.each(all_values.makes, function() {
                $option = $("<option>").text(this).val(this);
                if (all_values.current_make == this) {
                    $option.attr('selected','selected');
                }
                $makes_dd.append($option);
            });
            $.each(all_values.models, function() {
                $option = $("<option>").text(this).val(this);
                if (all_values.current_model == this) {
                    $option.attr('selected','selected');
                }
                $models_dd.append($option);
            });
        },'json');
    }

})( jQuery );

The problem is I am still learning and this is the first time I have had to use this funtion.

and I am getting an error on my website

Warning: array_keys() expects parameter 1 to be array, null given in /customers/4/0/0/motobid.co.uk/httpd.www/wp-content/themes/storevilla-child/functions.php on line 38 {"vehicles":null,"makes":[],"models":[],"current_vehicle":false,"current_make":false}

any help would be very greatful.

Just like to say code was supplied by BDMW.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码