Php无法检索动态生成的文本字段

时间:2015-03-21 17:11:32

标签: javascript php html

我无法检索我使用javascript创建的动态生成的文本字段和单选按钮,但是能够使用html检索我手动添加到表单中的那些按钮。

以前,我使用样式来隐藏和显示我的所有文本字段而不是动态创建的文本字段,所以我没有问题获取所有文本字段并且可以安全地假设我的PHP处理没有任何问题。

我的HTML /接口编码

<?php
for ($x = 1; $x <= 5; $x++)
{
    $inputAreaID = "inputArea".$x;// Creating all dynamic options and etc in this div using javascript 
    $typeID = "type".$x; //for select type id
    $opt2ID = "ans".$x; 
    $optID = "opt".$x; 
    $comID = "com".$x; 
    $qnName = "qn".$x // for qn textfield name?>
<table>
    <tr>
        <td>Question <?php echo $x ?></td>
        <td><input type="text" name='<?php echo $qnName ?>' required></td>
    </tr>

    <tr>
        <td>Question Type</td>
        <td>
        <select id="<?php echo $typeID ?>" name="<?php echo $typeID ?>" onclick="showQuestionSelect('<?php echo $x ?>', '<?php echo $typeID ?>', '<?php echo $inputAreaID ?>')">
        <option value="">Please select one option </option>                 
            <option value="Yes,No">Yes/No </option>
            <option value="True,False">True/False</option>
            <option value="3">A. B. C. D.</option>
        </select>
        </td>
    </tr>
 </table>   
        <div id='<?php echo $inputAreaID ?>' style="margin-bottom: 1px;margin-top:1px;"></div>

JavaScript编码

function showQuestionSelect(questionNo, typeID, inputArea) 
{
var s = String(typeID);
var i = String(inputArea);
var e = document.getElementById(s);
var selectedOpt = e.options[e.selectedIndex].value;

ansID = questionNo + "_ans";
comID = questionNo + "_com";
optA = questionNo + "_optA";
optB = questionNo + "_optB";
optC = questionNo + "_optC";
optD = questionNo + "_optD";

var msg = "";


if (selectedOpt == "True,False")
{
    msg ="<div style='margin-bottom: 10px;margin-top:10px;'><input type='radio' name='"+ ansID + "' value='True'> Submit True as Answer<input type='radio' name='" +ansID +"' value='False'> Submit False as Answer</div>";     
    msg +="<div style='margin-bottom: 10px;margin-top:10px;'>Reason for the answer: <input type='text' name='" + comID + "' value='' /></div>"
}
else if (selectedOpt == 3)
{
    msg = "<div style='margin-bottom: 10px;margin-top:10px;'>";
    msg += "Option A: <input type='text' name='" + optA + "' value='' /> <input type='radio' name='" + ansID + "' value='" + optA + "'> Submit as Answer  <br><br>";
    msg += "Option B: <input type='text' name='" + optB + "' value='' /> <input type='radio' name='" + ansID + "' value='" + optB + "'> Submit as Answer  <br><br>";
    msg += "Option C: <input type='text' name='" + optC + "' value='' /> <input type='radio' name='" + ansID + "' value='" + optC + "'> Submit as Answer  <br><br>";
    msg += "Option D: <input type='text' name='" + optD + "' value='' /> <input type='radio' name='" + ansID + "' value='" + optD + "'> Submit as Answer  </div>";      
    msg +="<div style='margin-bottom: 10px;'>Reason for the answer: <input type='text' name='" + comID + "' value='' /></div>";
}

document.getElementById(i).innerHTML = msg;

}

PHP处理编码

for ($x = 1; $x <= 5; $x++)
{
    $qn = $_POST["qn".$x]; // for qn textfield name
    $type = $_POST["type".$x]; //for select type id
    $ans = $_POST[$x."_ans"]; //for answer div id
    $reason = $_POST[$x."_com"]; //for answer div id

    $optionType = "";//to put into database

    if ($type == "Yes,No")
    {
        $ans = "";
        $reason = "";
    }

    if ($type == "3")
    {
        $optA = $_POST[$x."_optA"];
        $optB = $_POST[$x."_optB"];
        $optC = $_POST[$x."_optC"];
        $optD = $_POST[$x."_optD"];

        $optionType = $optA.",".$optB.",".$optC.",".$optD;

        if ($ans == $x."_optA")
            $ans = $optA;
        else if ($ans == $x."_optB")
            $ans = $optB;
        else if ($ans == $x."_optC")

        $ans = $optC;
        else 
            $ans = $optD;
    }
    else
        $optionType = $type;

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

首先,你应该检查服务器是否已获得那些生成的文本字段的值。尝试显示$_POST[]的所有成员。

我猜你被困在参数的名称中