John Davidson

Chance ACF Field with Javascript over a php function

0 comments
Message:


I want to write the dropdown-change into to an ACF field.
i have a ninja table with a dropdown and i added this code on dropdown:


<?php
wp_register_script( 'custom-acf-js', get_template_directory_uri() . '/library/scripts/script-js.js', ['acf-input'], '1.0.0', true);
wp_enqueue_script('custom-acf-js');

function feldUpdate($selector,$value,$post_id){
update_field('bewerber_einstufen', $value , $post_id );
};
//feldUpdate('bewerber_notiz','eingeladen', 192);
?>

<script type="text/javascript">
// Ninja Table Loaded initially
jQuery(document).on('ninja_table_loaded', function (event, $table, settings) {
console.log('ninja_table_loaded');
let changeButton = document.querySelectorAll('select');
var acfVersion = acf.get('acf_version');
//alert(acfVersion);
for(let i=0; i < changeButton.length; i++){
changeButton[i].addEventListener("change",function(){
let rowidparent = this.parentElement.parentElement;
let rowid = (rowidparent.querySelector("p").innerHTML);
console.log(rowid);

//feldUpdate('bewerber_notiz','eingeladen', rowid);
});

};
});
</script>

So how can i write the javascript code (variables) into my php function.


Kind regards, Daniel



My Answer:
javascript
jQuery(document).ready(function($) {
$('#my-dropdown').change(function() {
var selectedValue = $(this).val();
// Update the ACF field with the selected value
acf.fields.my_acf_field.val(selectedValue);
});
});


In this code snippet, replace `#my-dropdown` with the ID of your dropdown element and `acf.fields.my_acf_field` with the ACF field you want to update. Make sure to include this code within a `

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us