functionto $_POST url constructed with php
This commit is contained in:
parent
66340ac819
commit
2f834083c9
|
@ -6,9 +6,6 @@
|
||||||
* basic start
|
* basic start
|
||||||
* https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
|
* https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
|
||||||
*
|
*
|
||||||
* wordpress full form example
|
|
||||||
* https://wordpress.stackexchange.com/questions/383599/form-checkbox-value-going-to-dynamic-url
|
|
||||||
*
|
|
||||||
* checkbox adaptation query
|
* checkbox adaptation query
|
||||||
* https://wordpress.stackexchange.com/a/102915
|
* https://wordpress.stackexchange.com/a/102915
|
||||||
*
|
*
|
||||||
|
@ -17,169 +14,140 @@
|
||||||
*
|
*
|
||||||
* @package Oficina_de_Suport_theme
|
* @package Oficina_de_Suport_theme
|
||||||
*/
|
*/
|
||||||
?>
|
|
||||||
|
|
||||||
<?php
|
//function POST_look_values_to_submit() {
|
||||||
|
// // look checked values to send
|
||||||
|
// // https://stackoverflow.com/a/10808238
|
||||||
|
// if( isset($_POST['submit-ajut']) ){
|
||||||
|
// echo '<p style="color:#0b0;">$_POST[submit-ajut] set</p>';
|
||||||
|
// if( !empty($_POST[ $fields['name'] ]) ){
|
||||||
|
// echo '<p style="color:#0b0;">$_POST[$fields[name]] not empty</p>';
|
||||||
|
// foreach( $_POST[ $fields['name'] ] as $post_fields_name ) {
|
||||||
|
// $post_fields_name_value_checked[] = $post_fields_name;
|
||||||
|
// };
|
||||||
|
// $all_post_fields_name_values_selection[] = $fields['name'] . '=' . implode(',',$post_fields_name_value_checked);
|
||||||
|
// unset($post_fields_name_values_checked);
|
||||||
|
// } else {
|
||||||
|
// echo '<p style="color:#b00;">$_POST[$fields[name]] empty</p>';
|
||||||
|
// };
|
||||||
|
// //end look for checked values to send
|
||||||
|
// echo '<p style="color:#0b0;">after $_POST:' . $url . '</p>';
|
||||||
|
// };
|
||||||
|
// // end look checked values to send
|
||||||
|
//}
|
||||||
|
|
||||||
if( ! function_exists( 'ofisuport_ajuts_filters_form' ) ):
|
if( ! function_exists( 'ofisuport_ajuts_filters_form' ) ):
|
||||||
function ofisuport_ajuts_filters_form() {
|
function ofisuport_ajuts_filters_form() {
|
||||||
?>
|
|
||||||
<div id="archive-filters" class="ofisuport-filters">
|
echo '<div id="archive-filters" class="ofisuport-filters">';
|
||||||
<?php
|
|
||||||
// output all possible values of a checkbox
|
// output all possible values of a checkbox
|
||||||
|
|
||||||
|
|
||||||
$groupkey = "group_63ab636898703"; // write here the key for the group of fields from acf
|
$groupkey = "group_63ab636898703"; // write here the key for the group of fields from acf
|
||||||
if( $groupkey ) {
|
if( $groupkey ) {
|
||||||
?>
|
|
||||||
<?php // start the form ?>
|
|
||||||
<form id="form-ajuts" name="search-ajuts">
|
|
||||||
|
|
||||||
<?php
|
// start the form
|
||||||
|
echo '<form name="search-ajuts" method="post" action="' . $url . '">';
|
||||||
|
|
||||||
$groupkey_fields = acf_get_fields($groupkey);
|
$groupkey_fields = acf_get_fields($groupkey);
|
||||||
foreach( $groupkey_fields as $field_key ) {
|
foreach( $groupkey_fields as $field_key ) {
|
||||||
|
|
||||||
if( $field_key['type'] == 'checkbox' ){
|
if( $field_key['type'] == 'checkbox' ){
|
||||||
|
|
||||||
|
//echo '<span style="color: #00b">' . ($field_key['key']) . '</span><br/>' . '<span style="color: #00b">' . ($field_key['label']) . '</span><br/>' . '<span style="color: #00b">' . ($field_key['name']) . '</span><br/>';
|
||||||
|
|
||||||
// https://wordpress.stackexchange.com/a/102915
|
// https://wordpress.stackexchange.com/a/102915
|
||||||
$fields = get_field_object($field_key['key']);
|
$fields = get_field_object($field_key['key']);
|
||||||
if( $fields ) {
|
if( $fields ) {
|
||||||
|
|
||||||
|
// look checked values to send
|
||||||
|
//POST_look_values_to_submit();
|
||||||
|
// look checked values to send
|
||||||
|
// https://stackoverflow.com/a/10808238
|
||||||
|
if( isset($_POST['submit-ajut']) ){
|
||||||
|
echo '<p style="color:#0b0;">$_POST[submit-ajut] set</p>';
|
||||||
|
if( !empty($_POST[ $fields['name'] ]) ){
|
||||||
|
echo '<p style="color:#0b0;">$_POST[$fields[name]] ' . $fields['name'] . ' not empty</p>';
|
||||||
|
foreach( $_POST[ $fields['name'] ] as $post_fields_name ) {
|
||||||
|
$post_fields_name_value_checked[] = $post_fields_name;
|
||||||
|
};
|
||||||
|
$all_post_fields_name_values_selection[] = $fields['name'] . '=' . implode(',',$post_fields_name_value_checked);
|
||||||
|
unset($post_fields_name_values_checked);
|
||||||
|
} else {
|
||||||
|
echo '<p style="color:#b00;">$_POST[$fields[name]] ' . $fields['name'] . ' empty</p>';
|
||||||
|
};
|
||||||
|
//end look for checked values to send
|
||||||
|
echo '<p style="color:#0b0;">after $_POST:' . implode('?',$all_post_fields_name_values_selection) . '</p>';
|
||||||
|
};
|
||||||
|
// end look checked values to send
|
||||||
|
|
||||||
// check for values from meta fields db in url
|
// check for values from meta fields db in url
|
||||||
foreach( $GLOBALS['my_query_filters'] as $key => $fieldname ) {
|
foreach( $GLOBALS['my_query_filters'] as $key => $fieldname ) {
|
||||||
|
|
||||||
// check for values in url and get value if available
|
// check for values in url
|
||||||
|
// get value if available
|
||||||
if( isset($_GET[ $fieldname ]) ) {
|
if( isset($_GET[ $fieldname ]) ) {
|
||||||
|
|
||||||
$filteredvalues['value'] = explode(',', $_GET[ $fieldname ]);
|
$filteredvalues['value'] = explode(',', $_GET[ $fieldname ]);
|
||||||
//$filteredvalues['value'] = ($_GET[ $fieldname ]);
|
|
||||||
};
|
};
|
||||||
//end check for values in url
|
//end check for values in url
|
||||||
|
|
||||||
// construct the checkboxes
|
|
||||||
if( $fieldname == $fields['name']) {
|
// construct the checkboxes
|
||||||
?>
|
if( $fieldname == $fields['name']) {
|
||||||
|
|
||||||
<section id="fund-filter-<?php echo $fields['name']; ?>" class="<?php echo $fields['name']; ?>">
|
echo '<nav id="fund-filter" class="' . $fields['name'] . '">' ;
|
||||||
<h5><?php echo $fields['label'];?></h5>
|
echo '<h5>' . $fields['label'] . '</h5>';
|
||||||
<?php foreach( $fields['choices'] as $choicevalue => $choicelabel ) { ?>
|
echo '<ul>';
|
||||||
|
foreach( $fields['choices'] as $choicevalue => $choicelabel ) {
|
||||||
<div class="filter" data-filter="<?php echo $fields['name'];?>">
|
|
||||||
<input
|
echo '<li>';
|
||||||
class="ofisuport-filter"
|
echo '<label for="' . $choicevalue . '">';
|
||||||
type="checkbox"
|
echo '<input class="ofisuport-filter filter" type="checkbox" ';
|
||||||
<?php if( in_array($choicevalue,$filteredvalues['value']) ) { echo 'checked';}; ?>
|
echo ' value="' . $choicevalue . '" ';
|
||||||
value="<?php echo $choicevalue ?>"
|
echo ' name="' . $fields['name'] . '[]" ';
|
||||||
name="<?php echo $fields['name'];?>" />
|
if( in_array($choicevalue,$filteredvalues['value']) ) {
|
||||||
<label for="<?php echo $choicevalue; ?>"><?php echo $choicelabel;?></label>
|
|
||||||
</div>
|
echo ' checked="checked" ';
|
||||||
|
|
||||||
<?php };//end foreach fields['choices'] ?>
|
};
|
||||||
|
echo ' />';
|
||||||
</section>
|
echo $choicelabel;
|
||||||
|
echo '</label>';
|
||||||
<?php }; //end if fieldname == fields['name'] ?>
|
echo '</li>';
|
||||||
<?php // end of construct checkboxes ?>
|
|
||||||
|
};
|
||||||
<?php }; // end check for values from meta fields db in url ?>
|
echo '</ul>';
|
||||||
|
echo '</nav>';
|
||||||
<?php }; //end if field_key['key'] ?>
|
};
|
||||||
|
// end of construct checkboxes
|
||||||
<?php }; //end if type checkbox ?>
|
|
||||||
|
};
|
||||||
<?php }; //end foreach groupkey field ?>
|
// end check for values from meta fields db in url
|
||||||
<p><b>Results:</b> <span id="results"></span></p>
|
|
||||||
<button type="submit" class="button-more">enviar</button>
|
|
||||||
</form>
|
};
|
||||||
<?php //close the form and subit ?>
|
//end if field_key['key']
|
||||||
|
|
||||||
<?php }; //end if groupkey ?>
|
};
|
||||||
|
//end if type checkbox
|
||||||
</div>
|
|
||||||
|
};
|
||||||
<script type="text/javascript">
|
//end foreach groupkey field
|
||||||
|
|
||||||
//https://wordpress.stackexchange.com/questions/383599/form-checkbox-value-going-to-dynamic-url
|
$url = home_url('ajuts/?') . implode('&',$all_post_fields_name_values_selection);
|
||||||
// sample form on submiit
|
echo '<p>' . $url . '</p>';
|
||||||
//
|
//header('Location:'. $url);
|
||||||
(function($){
|
echo '<button type="submit" name="submit-ajut" class="button-more">enviar</button>';
|
||||||
|
echo '</form>';
|
||||||
$('#form-ajuts').on('submit', function(e) {
|
//close the form and subit
|
||||||
|
//POST_look_values_to_submit();
|
||||||
// prevent default
|
|
||||||
e.preventDefault();
|
};
|
||||||
|
//end if groupkey
|
||||||
// set empty submission object
|
|
||||||
let submission = {};
|
echo '</div>';
|
||||||
|
};
|
||||||
// for each of this form submit event target object entries as key/field
|
//end ofisuport_display_filters()
|
||||||
for (const [key, field] of Object.entries(e.target)) {
|
endif;
|
||||||
|
|
||||||
// if object entry (field) has a name attribute
|
|
||||||
if (field.name) {
|
|
||||||
|
|
||||||
// if field type is
|
|
||||||
if (field.type === 'checkbox') {
|
|
||||||
|
|
||||||
// set field name as array
|
|
||||||
submission[field.name] = [];
|
|
||||||
// if checkbox is checked
|
|
||||||
if (field.checked) {
|
|
||||||
|
|
||||||
// add name/value to submission object
|
|
||||||
submission[field.name].push(field.value);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (field.value) {
|
|
||||||
|
|
||||||
// add name/value to submission object
|
|
||||||
submission[field.name] = field.value;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// now let loop through our submission and check for arrays
|
|
||||||
for (const [name, value] of Object.entries(submission)) {
|
|
||||||
|
|
||||||
// if submission value is array
|
|
||||||
//if (Array.isArray(value)) {
|
|
||||||
// let convert array to string
|
|
||||||
//submission[name] = value.join(',');
|
|
||||||
|
|
||||||
//}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert submission object to url safe params string
|
|
||||||
let submission_url_params = $.param(submission);
|
|
||||||
|
|
||||||
// if we have submission params
|
|
||||||
if(submission_url_params) {
|
|
||||||
|
|
||||||
// success actions
|
|
||||||
|
|
||||||
// create our submission action url
|
|
||||||
let submission_url_action = this.action + (submission_url_params ? '?' + submission_url_params : '');
|
|
||||||
|
|
||||||
// un-comment this to actually fire the form submission
|
|
||||||
window.location.href = submission_url_action;
|
|
||||||
|
|
||||||
// else no submission params
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// no submission data actions
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
}; //end ofisuport_display_filters()
|
|
||||||
endif; // end if ! functionexists
|
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Reference in New Issue