Thursday, November 17, 2022
HomeWordPress Developmentphp - Contact type with dynamic dropdown and filter

php – Contact type with dynamic dropdown and filter


A shopper desires to create a contact type for a moto components retailer that permits customers to decide on their moto make-model-year. They need this info to offer higher help after the contact.
They need one thing like this:

enter image description here


however inside a contact type like this:

enter image description here

I’ve simply learn a weblog publish on “Find out how to create dynamically populated cascading dropdown-lists for Contact Type 7”.
I’m new to programming, I do not know something about PHP.
So I attempted step-by-step this:

  1. I created my database .csv file.

  2. I uploaded the file at public_html/wp-content/uploads/make-model-year.csv (Is it the right path?)

  3. Then I pasted this perform to features.php:

perform ajax_cf7_populate_values() {

    // learn the CSV file within the $makes_models_years array

$makes_models_years = array();
$uploads_folder = wp_upload_dir()['basedir'];
$file = fopen($uploads_folder.'make_model_year.csv', 'r');

$firstline = true;
whereas (($line = fgetcsv($file)) !== FALSE) {
    if ($firstline) {
        $firstline = false;
        proceed;
    }
    $makes_models_years[$line[0]][$line[1]][] = $line[2];

}
fclose($file);

    // setup the preliminary array that shall be returned to the the shopper aspect script as a JSON object.

$return_array = array(
        'makes' => array_keys($makes_models_years),
        'fashions' => array(),
        'years' => array(),
        'current_make' => false,
        'current_model' => false
    );

    // accumulate the posted values from the submitted type

$make = key_exists('make', $_POST) ? $_POST['make'] : false;
$mannequin = key_exists('mannequin', $_POST) ? $_POST['model'] : false;
$12 months = key_exists('12 months', $_POST) ? $_POST['year'] : false;

    // populate the $return_array with the required values

if ($make) {
    $return_array['current_make'] = $make;
    $return_array['models'] = array_keys($makes_models_years[$make]);
    if ($mannequin) {
    $return_array['current_model'] = $mannequin;
    $return_array['years'] = $makes_models_years[$make][$model];
    if ($12 months) {
            $return_array['current_year'] = $12 months;
        }
        }
    }

    // encode the $return_array as a JSON object and echo it
    
    echo json_encode($return_array);
    wp_die();

}

// These motion hooks are wanted to inform WordPress that the cf7_populate_values() perform must be referred to as
// if a script is POSTing the motion : ‘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’ );

However when I attempt to save features.php, it alerts “One thing has gone flawed. Your change couldn’t be saved. Please attempt once more. You may additionally must manually right it and add the file through FTP.”

I actually do not know the place to start, as a result of my data of programming is so fundamental. I’m utilizing WordPress, Elementor and Contact Type 7.

I’d be so grateful in case you might present me the way in which and assist me with this.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments