dongqian5639 2014-01-09 15:22
浏览 39
已采纳

每个php的多个帖子值

So I am working with some checkboxes that are populated from a db table. Which looks like this.

<form action="" method="post">
<?php
foreach ($chores as $retrieved_search){ 
?>
<tr>
<td>
<?php echo "<input type='checkbox' id=".$retrieved_search->id." value=".$retrieved_search->image." name='chores[]'>$retrieved_search->name";?>
<?php echo "<input type='text' value='$retrieved_search->name' id='optionName' name='optionName[]' />"?>
</td>
</tr>
<?php
}
?>
</form>

I am trying to figure out a way that I can get that value from the checkbox which I am currently doing and works fine. The value from the checkbox is an image URL.

I also need to get the name associated with the checkbox so I added another input field that is populating the name of the checkbox which I will then hide.

So this is my php statement that is getting the post value from the checkbox but how can I also get the value from the input field for the same insert statement?

<?php
if(isset($_POST['saveOptions'])) {

        global $wpdb;        
        $table_name = $wpdb->prefix . "chartUsersOptions";
        $user_ID = get_current_user_id();
        $typeChore = "chore";
        $typeBehavior = "behavior";

if(isset($_POST['chores'])){
        foreach ($_POST['chores'] as $chores) {

            $wpdb->insert( $table_name, array( 
                'userId' => $user_ID,
                'optionImage' => $chores, 
                'type' => $typeChore,
                ));
         }
        }

        $msg = "Saved now redirect to 3rd step"; 

        echo $msg;
    }
    else{
.............
    }
?>

EDIT:

Based on a suggestion This worked.

<?php
foreach ($chores as $retrieved_search){ 
echo "<input type='checkbox' value='{$retrieved_search->image}' name='chores[{$retrieved_search->id}]'>{$retrieved_search->name}";
echo "<input type='text' value='{$retrieved_search->name}' name='optionName[{$retrieved_search->id}]'>"
}
?>

And This

if(isset($_POST['chores'])){
        foreach ($_POST['chores'] as $chores_key => $chores) {
            $text_input_value = $_POST['optionName'][$chores_key];  
            $wpdb->insert( $table_name, array( 
                'userId' => $user_ID,
                'optionImage' => $chores, 
                'optionName' => $text_input_value,
                'type' => $typeChore,
                ));
         }
        }

But optionName is still inserting blank.

  • 写回答

2条回答 默认 最新

  • doupao1530 2014-01-09 15:33
    关注

    I'm not sure what the exact problem is, but my guess is that your checkboxes don't match your input boxes. That is because unchecked checkboxes do not get sent to the server, so you probably end up with an checkboxes array that is smaller than the input boxes array.

    You should change your html so that a checkbox-array-key always matches an input-array-key, so something like:

    echo "<input type='checkbox' value='{$retrieved_search->image}' name='chores[{$retrieved_search->id}]'>{$retrieved_search->name}";
    echo "<input type='text' value='{$retrieved_search->name}' name='optionName[{$retrieved_search->id}]'>"
    

    Also note that ID's need to be unique so I removed them but that is probably not related to your problem.

    Edit: To get the value of the text box after making the above changed, you can do:

    if (isset($_POST['chores'])) {
        foreach ($_POST['chores'] as $chores_key => $chores) {
            $text_input_value = $_POST['optionName'][$chores_key];
            // and the rest of your code
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动