i create custom field for select list country and state using foreach. but the value not displaying in the attribute filed in post.php
<select name="countryId" id="countryId" onchange="window.CountryChange()" >
<?php
$_value = trim( get_post_meta( get_the_ID(), 'countryId', true) );
foreach ( array(
'0' => 'Select Country',
'United States' => 'United States',
'Canada' => 'Canada',
) as $value => $label ) :
$selected = selected( $value, $_value, false );
?>
<option value="<?php echo esc_attr( $value ); ?>"<?php echo $selected; ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<div id="stateField" style="display:none">
<label id="stateLabel">State:</label>
<select name="stateId" id="stateId">
//js var inside this area what can i change this area
</select>
</div>
i create county options in php. the field value is successfully update in the post meta field and the value displaying in the attribute filed. but the state field create with java script. how to put or modifying the code display the meta value in the attribute fields in wordpress. after saving the post only showing country fields. state field meta is update but the attribute filed is hiding.
<script>
window.CountryChange = function () {
var countryState = [
[
'United States', [
['', 'State/Province'],
['AL', 'Alabama'],
['AK', 'Alaska'],
['AZ', 'Arizona'],
['AR', 'Arkansas'],
],
],
[
'Canada', [
['', 'State/Province'],
['AB', 'Alberta'],
['BC', 'British Columbia'],
['MB', 'Manitoba'],
['NB', 'New Brunswick'],
]
]
];
var countryElement = document.getElementById('countryId');
var stateElement = document.getElementById('stateId');
var stateLabelElement = document.getElementById('stateLabel');
var stateFieldElement = document.getElementById('stateField');
if (countryElement && stateElement) {
var listOfState = [
['XX', 'None']
];
var currentCountry = countryElement.options[countryElement.selectedIndex].value;
for (var i = 0; i < countryState.length; i++) {
if (currentCountry == countryState[i][0]) {
listOfState = countryState[i][1];
}
}
if (listOfState.length < 2) {
stateFieldElement.style.display = "none";
} else {
stateFieldElement.style.display = "inline-block";
}
var selectedState;
for (var i = 0; i < stateElement.length; i++) {
if (stateElement.options[i].selected === true) {
selectedState = stateElement.options[i].value;
}
}
stateElement.options.length = 0;
for (var i = 0; i < listOfState.length; i++) {
stateElement.options[i] = new Option(listOfState[i][1], listOfState[i][0]);
if (listOfState[i][0] == selectedState) {
stateElement.options[i].selected = true;
}
}
}
}
</script>
//update post meta
function mpcf_save_meta_box( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}
$fields = [
'countryId',
'stateId',
];
foreach ( $fields as $field ) {
if ( array_key_exists( $field, $_POST ) ) {
update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
}
}
}
add_action( 'save_post', 'mpcf_save_meta_box' );
i found the answer my self. i change the js script function name window.onload, so now i open my post edit, js script automatic running
window.CountryChange = function ()
// change to on load
window.onload = function ()
also change the select function name
<select name="countryId" id="countryId" onchange="window.CountryChange()" >
// change to the js function name
<select name="mpcf-countryid" id="mpcf-countryid" onchange="window.onload()" >
set default select value get postmeta to js dropdown
<select name="mpcf-stateid" id="mpcf-stateid" >
<?php
$_value1 = trim( get_post_meta( get_the_ID(), 'mpcf-stateid', true) );
foreach ( array(
$_value1 => $_value1
) as $value1 => $label1 ) :
$selected1 = selected( $value1, $_value1, false );
?>
<option value="<?php echo esc_attr( $value1 ); ?>"<?php echo $selected1; ?>><?php echo esc_html( $label1 ); ?></option>
<?php endforeach; ?>
</select>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.