doumei9832 2015-03-04 20:25
浏览 48

HTML PHP数组索引长度错误

I have an html form where I am grouping inputs using name="array[]" then just looping through the array with PHP when submitted. Well I am using array[] to store the question, but when the question (array index) is longer than 64 characters then It will not pass that array key to my PHP.

HTML

<textarea name="corporate[CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?]"></textarea>

When I do:

var_dump($_POST['array']);

I get array(0) But when I use a shorter index, it works.

Now if I manually create an associative array it works fine:

$array = array("CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?"=>"0");

What am I doing wrong?

I think it has to be a problem with going from the html form to the PHP. I am trying to loop through the inputs with my PHP so that I can loop through and display each question and corresponding answer with:

    foreach ($array as $key=>$value) {
        if ($value != NULL) {
            echo '<strong>' . $key . '</strong><br/>';
            echo $value . '<br/><br/>';
        }
    }

Which gives me:

Question

Answer

Question

Answer

etc.

How else could I do this without giving each input their own name to pass the question, or hard coding the question in my HTML?

  • 写回答

2条回答 默认 最新

  • douerlin4366 2015-03-04 20:37
    关注

    You can use a hidden input with the question and match up the indexes with the answer:

    <input type="hidden" name="question[1]" value="CAN YOU SHOW US BLAH?">
    <textarea name="answer[1]"></textarea>
    

    I assume you are dynamically adding the question text? If so you should probably use htmlentities on it to avoid issues.

    Then just loop one and access the other:

    foreach ($_POST['answer'] as $key => $value) {
        if (!empty($value)) {
            echo '<strong>' . $_POST['question'][$key] . '</strong><br/>';
            echo $value . '<br/><br/>';
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c