Inserting a text input with a button in PHP

1

I am a beginner in PHP and would like to know if there is a way to add an input text in PHP using a button. I'm developing a php application for warehouse control and I want to create a commodity-receiving routine called 'loading'. Then 'loading' would consist of a Loading Number and the products contained therein. However, because the different product numbers a shipment may have, it is necessary to add new entries in the form according to the needs of the user. Is this possible?

    
asked by anonymous 03.04.2016 / 05:23

2 answers

2

Good evening, according to my understanding, your doubt can be solved with JavaScript, more precisely jQuery ..

If in PHP, you want PHP to write the form, do more or less like this using for () :

form.php

<?php

$numCampos = $_GET["num_campos"];

echo '<form method="POST"> <!-- Dados repetidos --!> ';

for($i = 0; $i < $numCampos; $i++) { echo '<label for="amox_'.$i.'">Amox '.($i+1).'</label> <input type="text" name="amox[]" id="amox_'.$i.'"><br /> '; }

echo '</form>';

?>

And in the browser you access: link

And it will return:

<form method="POST">
<!-- Dados repetidos -->
<label for="amox_0">Amox 1</label> <input name="amox[]" id="amox_0" type="text"><br>
<label for="amox_1">Amox 2</label> <input name="amox[]" id="amox_1" type="text"><br>
<label for="amox_2">Amox 3</label> <input name="amox[]" id="amox_2" type="text"><br>
<label for="amox_3">Amox 4</label> <input name="amox[]" id="amox_3" type="text"><br>
</form>

Or you can do in jQuery that will be much more dynamic and visually beautiful for the user, using jQuery.append () .

I hope I have helped.

    
03.04.2016 / 06:36
3
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script><scriptsrc="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <style type="text/css">
        * { font-family:Arial; }
        h2 { padding:0 0 5px 5px; }
        h2 a { color: #224f99; }
        a { color:#999; text-decoration: none; }
        a:hover { color:#802727; }
        p { padding:0 0 5px 0; }

        input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border
    </style>
        <script type="text/javascript">

            $(function() {
                var scntDiv = $('#p_scents');
                var i = $('#p_scents p').size() + 1;

                $('#addScnt').live('click', function() {
                    $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="" /></label> <a href="#" id="remScnt">X</a></p>').appendTo(scntDiv);
                    i++;
                    return false;
                });

                $('#remScnt').live('click', function() { 
                    if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                    }
                    return false;
                });
            });


        </script>
    </head>
    <body>
        <h2><a href="#" id="addScnt">Adicionar campo</a></h2>

        <div id="p_scents">
            <p>
                <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="" /></label>
            </p>
        </div>
    </body>
    </html>
    
03.04.2016 / 07:05