Create a record in which the user increments a new checkbox to save new information in MySQL

2

I need to create a customer registry, where certain part of the user's registration has several checkboxes that would use to store if the client has such documents, for example:

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

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

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

    
asked by anonymous 02.02.2016 / 04:23

2 answers

-1

I found a code on the net that gave me a light .. I have not implemented it yet but it's basically this:

  

JavaScript:

At first include the jQuery library.

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

See the following JavaScript comment lines. maxField variable holding the maximum fields increment numbers, addButton variable holding the input fields adding button class, variable wrapper 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 HTML field input would be append to the parent fields 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 the 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: link

    
02.02.2016 / 13:24
-2

Good face this is called: Creating components at runtime. Good face in php is easier you do this using the echo function of php. Example:

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

I hope I have helped.

    
02.02.2016 / 04:38