jeudi 5 décembre 2019

PHP, dynamic form, access POST variables, checkboxes

I am trying to make dynamic form which will allow person to add multiple tooth data to the database. The problem I have is that after submitting the form I cant access the given variables and arrays(checkboxes) have only 1 value(first one).

enter image description here

To generate form im using jquerry, data about teeths are checkboxes.

Code to generate dynamic part of the form:

    <script type="text/javascript">
        function add_row()
        {
            $rowno=$("#teeth_table tr").length;
            $rowno=$rowno+1;
            $("#teeth_table tr:last").after("<tr id='row"+$rowno+"'>" +
                "<td style='margin-left: 15px'> Quarter <input type='number' name='quarter[]' min='1' max='4' placeholder='1-4'></td>" +
                "<td style='margin-left: 15px'> Tooth number <input type='number' name='number[]' min='1' max='8' placeholder='1-8'></td>" +
                "<td style='margin-left: 15px'> Description <input type='text' name='desc[]'></td>" +
                "<td style='margin-left: 15px'> Measurement <input type='text' name='measure[]'></td>" +
                "<td style='margin-left: 15px'><input type='button' value='DELETE' onclick=delete_row('row"+$rowno+"')></td></tr>");
        }
        function delete_row(rowno)
        {
            $('#'+rowno).remove();
        }
    </script>

The form code:

echo("
    <div style='margin-left: 30px'>
            <hr>
            <h4>Procedure data</h4>
            <div id='form_div'>
                <div style='margin-left: 20px'>
                <form method='get' action='dental_chart.php'>
                    <p>VAT_client:
                        <input type='text' name='VAT_client' value='$VAT_client' disabled/></input>
                    </p><p>VAT_doctor:
                        <input type='text' name='VAT_doctor' value='$VAT_doctor' disabled/></input>
                    </p><p>Date: 
                        <input type='date' name='date' value='$date' disabled/></input>
                    </p><p>Procedure type:
                        <input type='text' name='type' value='$procedure' disabled/></input>
                    </p><p>Description: 
                        <input type='text' name='description' placeholder='describe procedure'/>
                    </p>
                </div>
        ");
        ?>
        <hr>
        <h4>Insert procedure charting data - gap per tooth</h4>
        <div style='margin-left: 20px'>
            <table id="teeth_table">
                <tr id="row1">
                    <td style="margin-left: 15px"> Quarter <input type='number' name='quarter[]' min="1" max="4"
                                                                  placeholder="1-4"></td>
                    <td style="margin-left: 15px"> Tooth number <input type='number' name='number[]' min="1" max="8"
                                                                       placeholder="1-8"></td>
                    <td style="margin-left: 15px"> Description <input type='text' name='desc[]'></td>
                    <td style="margin-left: 15px"> Measurement <input type='text' name='measure[]'></td>
                </tr>
            </table>
            <input type="button" onclick="add_row();" value="ADD TOOTH">
        </div>

        <p></p>
        <hr>
        <div style="text-align: center">
            <input type="submit" name="submit_row" value="SUBMIT FORM">
        </div>
        </form>

And my php part to access the POST data if it's not empty:

if (!empty($_POST)) {

    $VAT_client_p = $_POST['VAT_client'];
    $VAT_doctor_p = $_POST['VAT_doctor'];
    $date_p = $_POST['date'];
    $type = $_POST['type'];
    $description = $_POST['description'];

    # dental chart multiple data
    $quarter = $_POST['quarter'];
    $number = $_POST['number'];
    $desc_tooth = $_POST['desc'];
    $measure = $_POST['measure'];


    echo("<p>VAT_client = ". $VAT_client_p .", VAT_doctor = ". $VAT_doctor_p.", date: ".$date_p ."</p>");


    for($i=0;$i<count($_POST['quarter']);$i++)
    {
        if($quarter[$i]!="" && $number[$i]!="" )
        {
            echo("<p>". count($quarter)." </p>");
            echo("<p>". count($desc_tooth)." </p>");
            echo("<p>quarter = '$quarter[$i]', tooth number = '$number[$i]', tooth desc: '$desc_tooth[$i]'</p>");
        }
    }
}

At this moment I just want to access the variables after submitting and echo them, when this part will be working I will perform transaction to insert the data into database. I will be grateful for any suggestion or advices, im stuck at this part.

(the result from running form first image is that arrays have only one element and VAT_doctor, VAT_client etc. are empty)




Aucun commentaire:

Enregistrer un commentaire