Support

Account

Home Forums Front-end Issues Increase/Decrease number field via front end form.

Solving

Increase/Decrease number field via front end form.

  • I have a simple number field on a page, which I would like users to be able to increase/decrease on the front end.

    Ideally this would be an input field where they could write in a positive or negative number, click submit and the number field would change by the amount entered, it’s fine if the page refreshes.

    Currently I’m using the acf_form to allow the number field to be edited, but this just replaces the number with whatever is entered, is there any way to adapt this for my purpose?

  • im just straggling same issue like you have. finally I figure it out and im sharing with you. some times you have to separate issue what we need
    1) update field and 2) increase decrease numbers with php.
    lets get started im 100 percent sure you already checked Update_field() documentation
    so in the samples

    
    // Get the current value.
    $count = (int) get_field('my_number_field');
    
    // Increase it.
    $count += 1;
    
    // Update with new value.
    update_field('views', $count);

    if you add this code the page you working and if you use your own CF name every page refresh number will be decrease also if you replace the $count with ( -= 1; ) now every page refresh value will be decrees so step one Is in the pocket ๐Ÿ™‚

    next step we need a simple php form to do that action by click

    
    <?php
    $count = (int) get_field('my_number_field'); //first we need our field value 
    if(isset($_POST['increase'])){ // increase 1 if clicked 
       $count += 1;
    }
    
    if(isset($_POST['decrease'])){ // decrease 1 if clicked
       $count -= 1;                                            
    }
    ?>
    
    <form method='post' action='#'> <!-- after click stay in this page -- >
       <td>
           <button name='increase'>+</button>
    <input type='text' size='1' name='item' value='<?= $count; ?>'/> <!-- its will be shows you current value or you change manually   -- >
           <button name='decrease'>-</button>
       </td>
    </form>
    

    couple days ago I just asked a question about changing value with QR code I just figure that one too while im helping you ๐Ÿ˜€

  • When I add this line:

    if(isset($_POST[‘increase’])){

    my website crash… shows a secure alert. Is there another option?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.