du9537 2016-04-13 16:49
浏览 43

PHP $ _POST无法读取jQuery val()设置的值

I created a little script which allows users to shadow some input values from other inputs. The JavaScript part is working fine, but now when I'm trying to send the form, it doesn't send it, saying that the field the value is copied to is empty. What's the problem here? Thanks!

$(document).ready(function() 
{
    $(":text").blur(function() 
    {
        var input = $(this);
        var id = input.attr("id");
        var fieldname = "#".concat(id);

        for (i = 0; i < fields.length; i++)
        {
            if (fields[i][4] == id)
            {
                var boxname = "#copy_".concat(fields[i][0]);
                if ($(boxname).prop("checked"))
                {
                    var fieldname2 = "#".concat(fields[i][0]);
                    $(fieldname2).val($(fieldname).val());
                }
            }
        }
    });

    $(":checkbox").change(function()
    {
        var input = $(this);
        var id = input.attr("id");
        id = id.substring(id.indexOf("_") + 1, id.length);
        var fieldname = "#".concat(id);

        var checked = input.prop("checked");
        $(fieldname).prop("disabled", checked);

        var field = FindField(0, fields[FindField(0, id)][4]);

        if (checked)
        {
            var fieldname2 = "#".concat(fields[field][0]);
            $(fieldname).val($(fieldname2).val());
        }
    });
});

function FindField(index, value)
{
    for (i = 0; i < fields.length; i++)
    {
        if (fields[i][index] == value) return i;    
    }
    return -1;
}

The form (without some irrelevant stuff):

<form method="post" action="sendform.php">
    <table>

        <?php
            foreach ($fields as $field)
            {
                if ($field[0] == "divider") echo('<tr><td colspan="2"><hr /></td></tr>');
                else 
                {   
                    switch ($field[2])
                    {
                        case FIELD_TEXT:
                        {
                            echo('<tr><td><label for="' . $field[0] . '">' . $field[1] . ': </label></td><td>');

                            if ($field[4] != "") 
                            {
                                foreach ($fields as $field2)
                                {
                                    if ($field2[0] == $field[4])
                                    {
                                        echo('<input type="checkbox" id="copy_' . $field[0] . '" title="Kopeeri väljalt &quot;' . $field2[1] . '&quot;" />');
                                    }
                                }
                            }

                            echo('<input type="text" id="' . $field[0] . '" name="' . $field[0] . '" placeholder="' . $field[1] . '" /></td></tr>');

                            break;
                        }
                    }
                }
            }
        ?>

        <tr><td colspan="2"><hr /></td></tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="submit" value="Saada avaldus" name="saadaavaldus" />
            </td>
        </tr>

    </table>
</form>

A sample of the fields array:

var fields = <?php echo json_encode($fields); ?>

$fields = array
(
    //      ID/name         label/placeholder                       type            regex                                                          field copied from
    array(  "saatjanimi",   "Teie täisnimi",                        FIELD_TEXT,     "^[-,.'\s\pL]*\pL[-,.'\s\pL]\s+[-,.'\s\pL]*\pL[-,.'\s\pL]*$",   ""),
    array(  "meiliaadress", "Teie meiliaadress",                    FIELD_TEXT,     "^([\pL-.\d]+)@([\pL-.\d]+)((\.(\pL){2,63})+)$",                ""),
    array(  "isanimi",      "Lapse isa täisnimi",                   FIELD_TEXT,     "^[-,.'\s\pL]*\pL[-,.'\s\pL]\s+[-,.'\s\pL]*\pL[-,.'\s\pL]*$",   "saatjanimi")
);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启