簡體   English   中英

PHP,動態表單,訪問 POST 變量,復選框

[英]PHP, dynamic form, access POST variables, checkboxes

我正在嘗試制作動態表格,允許人們將多個牙齒數據添加到數據庫中。 我遇到的問題是,提交表單后,我無法訪問給定的變量和數組(復選框)只有 1 個值(第一個)。

在此處輸入圖片說明

為了生成我使用 jQuery 的表單,關於牙齒的數據是復選框。

生成表單動態部分的代碼:

<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>

表格代碼:

echo("
    <div style='margin-left: 30px'>
            <hr>
            <h4>Procedure data</h4>
            <div id='form_div'>
                <div style='margin-left: 20px'>
                <form method='post' 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>

如果它不為空,我的 php 部分將訪問 POST 數據:

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>");
        }
    }
}

此時我只想在提交后訪問變量並回顯它們,當這部分工作時,我將執行事務以將數據插入數據庫。 我將不勝感激任何建議或建議,我被困在這一點上。

(運行表單第一張圖片的結果是數組只有一個元素並且 VAT_doctor、VAT_client 等為空)

我試過你的代碼,對我來說看起來不錯。

我將向您展示我的輸出,以便您可以驗證這是否是您想要的正確行為。

輸入這些值:

在此處輸入圖片說明

將導致您的 PHP 輸出:

在此處輸入圖片說明

對我來說,它看起來確實是正確的。 你能驗證並告訴我你仍然得到什么錯誤嗎? 你得到相同的輸出嗎?

我通過從動態形式傳遞 post 變量解決了這個問題。 我通過 GET 方法傳遞的其他變量。 我只是設法通過 post 再傳遞一個變量,我將它作為數組傳遞,其中值作為第一個元素,其余為空元素。

    <form method='post' action='dental_chart.php?VAT_client=$VAT_client_g&VAT_doctor=$VAT_doctor_g&date=$date_g'>
    <p>Description:
        <input type='text' name='description[]' placeholder='describe procedure'/>
    </p>
    <hr>
<h4>Insert procedure charting data - gap per tooth:</h4>
<p></p>
<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM