php
  • yii2
  • 2017-10-05 114 views -2 likes 
    -2

    我正在創建一個測驗應用程序,並將問題保存在數據庫中。每個測驗的問題數量是無限的。這是ERD。 enter image description herePHP動態輸入標籤

    使用此ERD,我可以顯示特定測驗的問題,就像下面的代碼:

    $questions= select * from questions where quiz_id=$quizid; 
    foreach($questions as $q){ 
        echo "<input type='text' name='input".$q->quiz_id."'>" 
    } 
    

    所以輸入值的名稱是input加上question id就像input1。如果有5個問題,提交的值應該從input1input5

    enter image description here

    我如何能夠捕捉到在POST方法提交的值以這樣的方式,我可以能夠將其保存在答案表?

    回答

    1

    將字段的名稱輸入作爲數組元素。

    $questions= select * from questions where quiz_id=$quizid; 
    foreach($questions as $key=>$q){ 
    echo "<input type='text' name='input[".$key."]'>" 
    } 
    

    現在您可以獲取數組中的所有輸入值。

    $input = $_POST['input']; 
    
    +0

    OMG。這一整天都陷入了這個問題。我無法想象我的問題是如此簡單的解決。謝謝。 – beginner

    相關問題