I guess I need advice here. I am not sure if I am doing this the right way. So I have this input field value which is fetched from the database (option table).
<!-- input to get amount of people -->
<tr>
<th scope="row"><?php _e('How many people do you want to add?', 'keeping-points'); ?></th>
<td>
<input type="text" size="15" name="posk_options[ppl_amount]" value="<?php _e( $options['ppl_amount'], 'keeping-points'); ?>" />
</td>
</tr>
<?php dlaugh_ppl_names(); ?>
The user types how many users that they would like to have input fields for and the function below (dlaugh_ppl_names) will create the input fields on submit of the form. It goes through a for loop to create the number of fields.
function dlaugh_ppl_names() {
$options = get_option('posk_options');
$name_num = $options['ppl_amount'];
for($i=0;$i < $name_num; $i++) {
$point = "txt_" . $i;
$name_build = $point . "_name";
$point_total = $point . "_point_total";
$name_print = $options[$name_build];
$changed_name = str_replace(' ', '_', $name_print);
$point_build = "point_total_" . $changed_name;
if ($options[$name_build] == "") {
$set_points = $name_num - $i;
echo $i;
$final_ppl = $name_num - $set_points;
$options['ppl_amount'] = $final_ppl;
$options[$name_build] = null;
if ($options[$name_build] == null) {
$options[$point_total] = null;
}
$options = array_filter( $options );
update_option( 'posk_options', $options );
break;
}
if ($options[$name_build] == true) {
?>
<!-- Add Person Name -->
<tr>
<th scope="row">
<?php if ($options[$name_build] == "" ) {
_e('Person ' . $i + 1, 'keeping-points'); ?>
<?php } else
_e($options[$name_build], 'keeping-points');
?>
</th>
<td>
<input type="text" size="20" name="posk_options[<?php echo $name_build ?>]" value="<?php _e( $options[$name_build], 'keeping-points'); ?>" />
<input type="text" size="10" name="posk_options[<?php echo $point_build; ?>]" value="<?php _e( $options[$point_build], 'keeping-points'); ?>" />
</td>
</tr>
<?php
}
}
for($i=0;$i < $name_num; $i++) {
$point = "txt_" . $i;
$name_build = $point . "_name";
$point_total = $point . "_point_total";
$name_print = $options[$name_build];
$changed_name = str_replace(' ', '_', $name_print);
$point_build = "point_total_" . $changed_name;
if ($options[$name_build] == false && $options[$name_build] == null) {
?>
<!-- Add Person Name -->
<tr>
<th scope="row"><?php _e('Person' . ' ' . ($i + 1), 'keeping-points'); ?></th>
<td>
<input type="text" size="20" name="posk_options[<?php echo $name_build ?>]" value="<?php _e( $options[$name_build], 'keeping-points'); ?>" />
<input type="hidden" name="posk_options[<?php echo "point_total_" . $name_print ?>]" value="<?php _e( $options[$point_build], 'keeping-points'); ?>" />
</td>
</tr>
<?php
}
}
}
After the fields are created then the input holding ppl_amount will show how many fields the user created. (lets say the typed 15, pressed submit, fields are created, then 15 is still there.) I want the 15 to be there, but on submit i want the 15 to change based on how many input fields for names where actually filled out. Lets say they typed 15...then only filled out 5. Well, the 15 value would then change to five. the code I have works, but my problem is that the value 15 is resent as the number chosen by the person, but if i refresh the page then the fields will work as I am expecting. So I want the ppl_amount to change based on how many inputs are filled out that come from the dlaugh_ppl_names() function.
Hope it's not too confusing. Thanks for any advice! I thought about having an ajax call to refresh the page after each input field is filled out, but that would be really annoying. I fill like there is an easy answer to this that I am just missing.
The best solution I found was to add a class to the person input and then count how many input fields were filled in and then put that value in the input field with a question. That way it dynamically updates that field value depending on how many inputs are filled in. I will post a link to the finished WordPress plugin soon, just in case you would like a full demo on this.
using Ajax
jQuery(document).ready(function($){
$('.people').blur(function() {
var data = {
'action': 'points_action'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
var searchCount = $('.people').filter(function(){
return $(this).val();
}).length;
$('#ppl_amount').val(searchCount);
});
});
});