Create a register where the user increments a new checkbox to save a new information in Mysql

Asked

Viewed 128 times

3

I need to create a client register, where certain part of the registration the user has several checkbox that would use to store if the client has such documents, for example:

[] RG 
[X] CPF
[] Certidão de nascimento

and a button so that you can "Add new element" that is not in the list, where each time you click it creates a new checkbox with the description the user wants and you can mark it to be saved in a mysql table.

How can I do that? or how can I search for something on the internet that has this similar code?

2 answers

-1

I found a code on the Net that gave me a light.. I haven’t implemented it yet but that’s basically it:

Javascript:

At First include the jQuery library.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

See the Below Javascript comment Lines. Maxfield variable holding the Maximum Fields increment Numbers, addButton variable holding the input Fields Adding button class, wrapper variable holding all input Fields Parent div class, fieldHTML variable holding new input field HTML code.

Once the "Add button" is clicked, we will check Maximum input Fields number. If field counter (x) is Less than Maxfield, the new input field HTML would be append into the Fields Parent div (wrapper). Also the field counter would be incremented.

Once the "Remove button" is clicked, the Parent div of that particular remove button would be Removed. Also the field counter would be decrement.

<script type="text/javascript">
$(document).ready(function(){
    var maxField = 10; //Input fields increment limitation
    var addButton = $('.add_button'); //Add button selector
    var wrapper = $('.field_wrapper'); //Input field wrapper
    var fieldHTML = '<div><input type="text" name="field_name[]" value=""/><a href="javascript:void(0);" class="remove_button" title="Remove field"><img src="remove-icon.png"/></a></div>'; //New input field html 
    var x = 1; //Initial field counter is 1
    $(addButton).click(function(){ //Once add button is clicked
        if(x < maxField){ //Check maximum number of input fields
            x++; //Increment field counter
            $(wrapper).append(fieldHTML); // Add field html
        }
    });
    $(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
        e.preventDefault();
        $(this).parent('div').remove(); //Remove field html
        x--; //Decrement field counter
    });
});
</script>

HTML:

<div class="field_wrapper">
    <div>
        <input type="text" name="field_name[]" value=""/>
        <a href="javascript:void(0);" class="add_button" title="Add field"><img src="add-icon.png"/></a>
    </div>
</div>

PHP:

Once the Multiple form Fields are submitted, then you can get the values as an array in PHP script.

  <?php
    print '<pre>';
    print_r($_REQUEST['field_name']);
    print '</pre>';
    //output
    Array
    (
        [0] => value1
        [1] => value2
        [2] => value3
        [3] => value4
    )
    ?>

Now you can run a foreach loop and Insert the values into the database or whatever you want.

  <?php
    $field_values_array = $_REQUEST['field_name'];
    foreach($field_values_array as $value){
        //your database query goes here
    }
    ?>

Source: http://www.codexworld.com/add-remove-input-fields-dynamically-using-jquery/

  • if you don’t have a better answer then have no negative pq post

-2

Good guy this is called: Building components at runtime. Good guy in php is easier you do this using php’s echo function. Example:

echo '<input type="checkbox" name="vehicle" value="Bike">';

I hope I’ve helped.

  • That doesn’t answer the question.

  • But how can you not? I gave the example of how to create the component at runtime now it will adapt in his code, because I do not have the code he is doing to put through, so I gave the example of how to create the element at runtime to adapt, Ué. The guy didn’t even give the code he’s trying, so I gave an example of how to do it. I think the people here are being too ignorant.

  • What I have noticed is that here are many fashion perfectionists, because what I see happening is half a dozen guys who copy content from other websites to answer the question here and put a 200-line text and everyone is positive (many don’t even read), Now if you give a simple but efficient answer always has a #$#$#$#@to give negative, And I say more... Who has to give negative is the boy who is asking he yes will see if the answer helps or not, worse than the people gets and often does not help at all.

  • Young man, I just said your answer doesn’t answer the question. She may be able to lead the OP to the right path, but by far it is not an answer that solves the problem. If you’re offended by all the negative you take around here, this might not be the best community for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.